0

我正在研究的一个 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。如果我设置一个断点,执行就会一团糟:

  1. 在设置的断点处停止if (!(Test-Path $backupDir))
  2. 我走进去if ($backupDir.EndsWith("\*"))
  3. 我走进去Write-Host "$backupDir ends with \*!"
  4. 我走进去,它停止运行。

为什么脚本在它应该终止之前终止?

编辑:我尝试将函数移动到常规.ps1脚本并从同一个脚本调用函数并且它工作正常......它只会在函数位于模块中并从模块外部调用时过早终止。是什么赋予了?

4

1 回答 1

0

我能够通过完全关闭 PowerShell ISE 的所有打开实例并重新打开我的模块来解决此问题。

于 2016-08-25T17:39:20.337 回答