powershell-2.0 - 文件是自动创建的,脚本应该自动重命名并移动它,但它们不会移动
问题描述
长期倾听者,第一次来电者,
我需要创建一个 Powershell 脚本(版本 2 或更低版本):
- 持续监控一个特定目录中的新/更改文件 - 在日志文件中记录使用日期/时间戳创建的文件,该文件为: -- 每天创建,名称为“log Date/Time.txt” -重命名文件,附加日期/时间 - 重命名文件的日志 - 使用特定用户名/密码组合映射驱动器 - 将其从 dirA 移动到 dirB(映射的驱动器是 dirB) - 移动它的日志 - 取消映射驱动器-如果由于某种原因它停止运行并且我们重新启动它,它将重命名,映射驱动器,移动,取消映射驱动器,并将 dirA 中的所有文件记录到 dirB
在当前的形式中,我已经剥离了 dir 的映射以解决它在本地驱动器上移动文件的问题,只是为了避免对网络驱动器进行故障排除。
我已经盯着这个看了一个多星期,厌倦了用头撞桌子。有人可以让我摆脱痛苦,让我知道我做错了什么吗?
先感谢您!
老实说,我尝试了很多组合,不同的套路,我什至不知道该放什么。
在下面的框中,我已经放置了无法正常工作的脚本的主要部分。它会在文件进入时重命名它们,但不会移动它们。
$rename = $_.Name.Split(".")[0] + "_" + ($_.CreationTime | Get-Date -Format MM.dd.yyy) + "_" + ($_.CreationTime | Get-Date -Format hh.mm.ss) + ".log"
Write-Output "File: '$name' exists at: $source - renaming existing file first" >> $scriptlog
Rename-Item $_ -NewName $rename
Wait-Event -Timeout 3
Move-Item "$_($_.Directory)$name" -destination $destination
Write-Output "File: '$name' moved to $destination on $date" >> $scriptlog
完整代码如下:
#Log Rename/Move script
$userName = "copyuser"
$newpass = Read-Host -Prompt 'Type the new Password'
$password = ConvertTo-SecureString -String $newpass -AsPlainText -Force
$PathToMonitor = "C:\Users\Administrator\Desktop\FolderA"
$destination = "C:\Users\Administrator\Desktop\FolderB"
$scriptlog = "C:\Users\Administrator\Desktop\ScriptLogs\" + [datetime]::Today.ToString('MM-dd-yyy') + "_TransferLog.txt"
$FileSystemWatcher = New-Object System.IO.FileSystemWatcher
$FileSystemWatcher.Path = $PathToMonitor
$FileSystemWatcher.IncludeSubdirectories = $false
$FileSystemWatcher.EnableRaisingEvents = $true
$dateTime = [datetime]::Today.ToString('MM-dd-yyy') + " " + [datetime]::Now.ToString('HH:mm:ss')
Write-Output "*******************************************************************************************" >> $scriptLog
Write-Output "*********************Starting Log Move Script $dateTime**********************" >> $scriptLog
Write-Output "*******************************************************************************************" >> $scriptLog
$Action = {
$details = $event.SourceEventArgs
$Name = $details.Name
$FullPath = $details.FullPath
$OldFullPath = $details.OldFullPath
$OldName = $details.OldName
$ChangeType = $details.ChangeType
$Timestamp = $event.TimeGenerated
$text = "{0} was {1} at {2}" -f $FullPath, $ChangeType, $Timestamp
Write-Output "" >> $scriptlog
Write-Output $text >> $scriptlog
switch ($ChangeType)
{
'Changed' { "CHANGE"
Get-ChildItem -path $FullPath -Include *.log | % {
$rename = $_.Name.Split(".")[0] + "_" + ($_.CreationTime | Get-Date -Format MM.dd.yyy) + "_" + ($_.CreationTime | Get-Date -Format hh.mm.ss) + ".log"
Write-Output "File: '$name' exists at: $source - renaming existing file first" >> $scriptlog
Rename-Item $_ -NewName $rename
Wait-Event -Timeout 3
Move-Item "$_($_.Directory)$name" -destination $destination
Write-Output "File: '$name' moved to $destination on $date" >> $scriptlog
}
}
'Created' { "CREATED"
Get-ChildItem -path $FullPath -Include *.log | % {
$rename = $_.Name.Split(".")[0] + "_" + ($_.CreationTime | Get-Date -Format MM.dd.yyy) + "_" + ($_.CreationTime | Get-Date -Format hh.mm.ss) + ".log"
Write-Output "File: '$name' exists at: $source - renaming existing file first" >> $scriptlog
Rename-Item $_ -NewName $rename
Wait-Event -Timeout 3
Move-Item "$($_.Directory)$rename" -Destination $destination
Write-Output "File: '$name' moved to $destination on $date" >> $scriptlog
}
}
'Deleted' { "DELETED"
}
'Renamed' {
}
default { Write-Output $_ >> $scriptlog}
}
}
$handlers = . {
Register-ObjectEvent -InputObject $FileSystemWatcher -EventName Changed -Action $Action -SourceIdentifier FSChange
Register-ObjectEvent -InputObject $FileSystemWatcher -EventName Created -Action $Action -SourceIdentifier FSCreate
}
Write-Output "Watching for changes to $PathToMonitor" >> $scriptlog
try
{
do
{
Wait-Event -Timeout 1
Write-host "." -NoNewline
} while ($true)
}
finally
{
# EndScript
Unregister-Event -SourceIdentifier FSChange
Unregister-Event -SourceIdentifier FSCreate
$handlers | Remove-Job
$FileSystemWatcher.EnableRaisingEvents = $false
$FileSystemWatcher.Dispose()
write-output "Event Handler disabled." >> $scriptlog
}
解决方案
嗯,我发现了我的问题。如果我看着它并思考它,这是一个愚蠢的问题。这就是我在重命名后调用包含要移动的文件的当前目录的方式。
Move-Item "$_($_.Directory)$name" -destination $destination
上面代码中的问题是“$ ($ .Directory)。它需要是:
Move-Item -path $PathToMonitor$name -destination $destination
其他事情可能会起作用,并且可能会更好,但至少可以解决我遇到的命名问题后的移动。
现在添加映射驱动器和其他完全完成我需要的东西。
如果我考虑一下,我会在为其他任何人完成后发布我的整个代码,它可以在未来提供帮助。
推荐阅读
- typescript - typescript中函数声明的区别
- reactjs - 加载来自 api 的数据后如何加载虚拟 dom React
- java - togglz 库向其公开编辑选项的执行器端点是什么
- elasticsearch - 弹性script_score,只计算特定的数组元素
- postgresql - 如何从sequelize中的关联表中获取数据
- python - python处理来自用户的无效输入
- scorm - AICC/HACP 示例包存储库
- python - 颜色作为 seaborn 联合图中的第三个轴
- c# - C# appsettings 将对象数组绑定到类
- performance - 如何测试私有 ETH 区块链的 tps?