我正在研究的一个 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 不以 \* 结尾。
但它永远不会运行 theCopy-Item
或 final Write-Host
。如果我设置一个断点,执行就会一团糟:
- 在设置的断点处停止
if (!(Test-Path $backupDir))
- 我走进去
if ($backupDir.EndsWith("\*"))
- 我走进去
Write-Host "$backupDir ends with \*!"
- 我走进去,它停止运行。
为什么脚本在它应该终止之前终止?
编辑:我尝试将函数移动到常规.ps1
脚本并从同一个脚本调用函数并且它工作正常......它只会在函数位于模块中并从模块外部调用时过早终止。是什么赋予了?