PowerShell模块意外终止

时间:2016-08-25 14:51:05

标签: powershell powershell-module

我工作的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。如果我设置了一个断点,执行就搞砸了:

  
      
  1. if (!(Test-Path $backupDir))
  2. 上设置的断点处停止   
  3. 我走进去,进入if ($backupDir.EndsWith("\*"))
  4.   
  5. 我走进去,进入Write-Host "$backupDir ends with \*!"
  6.   
  7. 我踏入并停止跑步。
  8.   

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

编辑:我尝试将该函数移动到常规.ps1脚本并从同一个脚本调用该函数,它工作正常...它只会在函数所在的情况下提前终止一个模块,从模块外部调用。是什么给了什么?

1 个答案:

答案 0 :(得分:0)

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