长时间收听者,首次呼叫者
我需要创建一个Powershell脚本(版本2或更低):
-持续监视一个特定目录中的新文件/更改文件 -将使用日期/时间戳创建的文件记录在以下日志文件中: -每天创建,名称为“ log Date / Time.txt” -重命名文件,附加日期/时间 -日志,将其重命名 -使用特定的用户名/密码组合来映射驱动器 -将其从dirA移至dirB(映射的驱动器为dirB) -它移动它的日志 -取消映射驱动器 -如果由于某种原因它停止运行并重新启动它,它将对其重命名,映射驱动器,移动,取消映射驱动器并将dirA中的所有文件记录到dirB
在当前形式下,我删除了目录的映射以对其进行故障排除以将其移动到本地驱动器上,只是为了避免对网络驱动器进行故障排除。
我已经盯着这个看了一个多星期,厌倦了把头撞在桌子上。有人可以让我摆脱痛苦,让我知道我做错了什么吗?
提前谢谢!
老实说,我尝试了很多组合,不同的例程,我什至都不知道在这里放什么。
在下面的框中,我放置了脚本的主要部分,该部分无法正常运行。它会在文件进入时对其进行重命名,但不会移动它们。
$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
}
答案 0 :(得分:0)
好吧,我发现了我的问题。如果我考虑一下,这是一个愚蠢的问题。这就是我如何调用包含文件的当前目录,然后将其重命名。
Move-Item "$_($_.Directory)$name" -destination $destination
上面的代码中的问题是“ $ ($ .Directory)。它必须是:
Move-Item -path $PathToMonitor$name -destination $destination
其他方法可能会起作用,并且可能会更好,但是至少可以解决我遇到的命名问题之后的问题。
现在要添加Mapping Drive和其他完全完成我需要的东西。
如果考虑到这一点,我会在完成后将我的整个代码发布给其他人,这对以后可能会有帮助。