PowerShell-脚本在ISE中可以完美地工作,但在普通控制台中却无法工作

PowerShell-脚本在ISE中可以完美地工作,但在普通控制台中却无法工作,powershell,Powershell,我对下面的脚本有问题。 下面的脚本是为了检测目录$P1中的任何更改而编写的,一旦检测到这些更改,它将在目录$P2(应该是$P1的相同副本)上写入自身 你问的问题是什么? 该脚本在PowerShell ISE中工作得非常好。一旦它检测到什么,它将更新$P2文件夹。 但是,当使用powershell正常运行(作为本地管理员)时,它不会执行任何操作。我想那是因为窗户马上就关上了。但是,我通过使PowerShell始终在-NoExit模式下运行来调整这一点。 该窗口现在确实保持打开状态,但在对$P1进行

我对下面的脚本有问题。 下面的脚本是为了检测目录$P1中的任何更改而编写的,一旦检测到这些更改,它将在目录$P2(应该是$P1的相同副本)上写入自身

你问的问题是什么? 该脚本在PowerShell ISE中工作得非常好。一旦它检测到什么,它将更新$P2文件夹。 但是,当使用powershell正常运行(作为本地管理员)时,它不会执行任何操作。我想那是因为窗户马上就关上了。但是,我通过使PowerShell始终在-NoExit模式下运行来调整这一点。 该窗口现在确实保持打开状态,但在对$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
}

我的脚本部分基于。

如果我不得不猜测,我会说这是一个范围问题。基本上,
FileSystemWatcher
不知道
RunPrimaryScript
是什么,因为它在触发触发器时没有定义。ISE的某些方面使其保持在范围内。尝试用函数体替换函数调用

[复制自评论,因为这是明显的解决方案。]


您可以尝试在定义函数时指定作用域,或者通过引用作用域来调用函数。请参阅该页。

似乎是权限问题。从控制台运行脚本时,您对脚本的调用是什么样子的?@rrirower从控制台运行时(作为本地管理员,所有内容都在本地文件夹中运行,没有特殊权限),控制台中没有显示任何内容。脚本不会生成任何输出,它应该写入日志并复制文件。但它在控制台中实际上什么也不做。在ISE中100%工作时。能否显示用于启动脚本的命令?你提到了诺西特。是否使用了任何其他命令行参数?@rrirower该脚本只是通过使用PowerShell启动来运行的,然后它会自动检测$P1目录中的更改以便运行。不需要运行任何参数。我只是在complete-NoExit模式下运行PowerShell。请尝试使用“Start Transcript”捕获任何输出。在运行脚本时,我还会尝试使用“-executionpolicy bypass”。