我遇到以下脚本的问题。 下面的脚本是为了检测目录$ P1中的任何更改而编写的,一旦检测到这些更改,它就会在目录$ P2(它应该是$ P1的相同副本)上写自己。
你问的问题是什么? 该脚本在PowerShell ISE中完美运行。一旦检测到某些内容,它就会更新$ P2文件夹。 但是当使用PowerShell正常运行(作为本地管理员)时,它什么都不做。我认为这是因为窗户立即关闭。但是我通过使PowerShell始终在-NoExit模式下运行来调整此值。 Window确实现在保持打开状态,但是当对$ P1进行更新时仍然没有任何反应。我对它在ISE中的工作原理一无所知,但在基本的PowerShell中却没有。
Function RunPrimaryScript {
# Update $P1 & $P2 to $P1 the actually Rdir and $P2 to the new location.
$DT = Get-Date
Out-File -FilePath C:\BHR\Rmap.log -Append -InputObject "[INFO] $DT - Script has started successfully."
## Set the paths.
$P1 = "C:\Tmp\TESTFILES\R-default"
$P2 = "C:\BHR\Rmap\"
## Check if the R-Folder excists, if not: end the script without closing.
if (!(Test-Path $P1)) {
$DT = Get-Date
Out-File -FilePath C:\BHR\Rmap.log -Append -InputObject "[ERROR] $DT - The R-directory cannot be found, the script has been stopped."
Break
}
## Checkif there's a copy already. Create it if there isn't.
if (!(Test-Path $P2)) {
Copy-Item -Path $P1 -Destination $P2 -Force -Recurse
$DT = Get-Date
Out-File -FilePath C:\BHR\Rmap.log -Append -InputObject "[WARNING] $DT - There was no copy of the R-directory. So it has been created."
}
## Index all subdirectories and files.
$GCP1 = Get-Childitem -Recurse -Path $P1
$GCP2 = Get-Childitem -Recurse -Path $P2
## Check for an excisting copy, if so: remove it.
if (Test-Path -Path "$P2") {
$DT = Get-Date
Remove-Item $P2 -Force -Recurse
Out-File -FilePath C:\BHR\Rmap.log -Append -InputObject "[INFO] $DT - The R-directory copy on $P2 has been removed."
}
## Write new files.
Copy-Item -Path $P1 -Destination $P2 -Force -Recurse
## Log adjustements.
$DT = Get-Date
Out-File -FilePath C:\BHR\Rmap.log -Append -InputObject "[INFO] $DT - The following changes have been appended:"
Compare-Object -DifferenceObject $GCP2 -ReferenceObject $GCP1 -CaseSensitive | Out-File -FilePath C:\BHR\Rmap.log -Append
if (!(Compare-Object -DifferenceObject $GCP2 -ReferenceObject $GCP1 -CaseSensitive)) {
$DT = Get-Date
Out-File -FilePath C:\BHR\Rmap.log -Append -InputObject "[INFO] $DT - Files have had their content adjusted."
}
$DT = Get-Date
Out-File -FilePath C:\BHR\Rmap.log -Append -InputObject "[INFO] $DT - End of change list."
if ($Error) {
$DT = Get-Date
Out-File -FilePath C:\BHR\Rmap.log -Append -InputObject "[WARNING] $DT - While executing the script, the following error has been detected: $error"
}
$DT = Get-Date
Out-File -FilePath C:\BHR\Rmap.log -Append -InputObject "[INFO] $DT - The script has been successfully ended."
Out-File -FilePath C:\BHR\Rmap.log -Append -InputObject "---------------------------------------------------------------------------------------------------"
}
## Live check for updates to the Rdir.
$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = "C:\Tmp\TESTFILES\R-default"
$watcher.IncludeSubdirectories = $true
$watcher.EnableRaisingEvents = $true
$changed = Register-ObjectEvent $watcher "Changed" -Action {
RunPrimaryScript
}
$created = Register-ObjectEvent $watcher "Created" -Action {
RunPrimaryScript
}
$deleted = Register-ObjectEvent $watcher "Deleted" -Action {
RunPrimaryScript
}
$renamed = Register-ObjectEvent $watcher "Renamed" -Action {
RunPrimaryScript
}
我的脚本部分基于this script。
答案 0 :(得分:0)
如果我不得不猜测,我会说这是一个范围问题。基本上,FileSystemWatcher
不知道RunPrimaryScript
是什么,因为它在触发时没有定义。关于ISE的一些事情使它保持在范围内。尝试用函数体替换函数调用。
[从评论中复制,因为这是明显的解决方案。]
您可以尝试在定义函数时指定范围,或者通过引用范围来调用函数。请参阅about_Scopes页面。