我工作的PowerShell模块表现得非常奇怪...
我在RestoreTestFiles.psm1
文件(位于%USERPROFILE%\My Documents\WindowsPowerShell\Modules\RestoreTestFiles\
)中有以下代码:
Function Restore-TestFiles([string]$backupDir, [string]$destinationDir, [bool]$overwrite)
{
if (!(Test-Path $backupDir))
{
Write-Host "Error, $backupDir does not exist."
return
}
if ($backupDir.EndsWith("\*"))
{
Write-Host "$backupDir ends with \*!"
}
else
{
Write-Host "$backupDir does not end with \*."
}
if ($overwrite)
{
Copy-Item -Path $backupDir -Destination $destinationDir -Recurse -Force
}
else
{
Copy-Item -Path $backupDir -Destination $destinationDir -Recurse
}
Write-Host "Files sucessfully copied from $backupDir to $destinationDir."
}
我将这个函数称为:
Import-Module RestoreTestFiles
Restore-TestFiles "C:\some\path\*" "C:\someOther\path\"
由于某种原因,输出只是
C:\ some \ path \ *以\ *!
结尾
或
C:\ some \ path不以\ *。
结尾
但它永远不会运行Copy-Item
或最终Write-Host
。如果我设置了一个断点,执行就搞砸了:
- 在
上设置的断点处停止if (!(Test-Path $backupDir))
- 我走进去,进入
if ($backupDir.EndsWith("\*"))
- 我走进去,进入
Write-Host "$backupDir ends with \*!"
- 我踏入并停止跑步。
醇>
为什么脚本应该在它之前终止?
编辑:我尝试将该函数移动到常规.ps1
脚本并从同一个脚本调用该函数,它工作正常...它只会在函数所在的情况下提前终止一个模块,从模块外部调用。是什么给了什么?
答案 0 :(得分:0)
我能够通过完全关闭PowerShell ISE的所有打开实例并重新打开我的模块来解决这个问题。