Powershell脚本中的奇怪行为

时间:2010-09-08 13:30:20

标签: powershell

我使用Checkout-WorkingCopy查看项目的工作副本,然后使用Cleanup-WorkingCopy删除目录(这些函数的代码在本文末尾)。

我面临的问题:

  1. 不回显"Checking out from svn://somePath1 to C:/somePath2",而是回应"Checking out from svn://somePath1 C:/somePath2 to"
  2. 在Cleanup-WorkingCopy中,它在尝试执行Pushd时会抛出以下错误,

    <Push-Location : Cannot convert 'System.Object[]' to the type 'System.String' 
    required by parameter 'LiteralPath'. Specified method is not supported.
        + Pushd <<<<  $directory
        + CategoryInfo : InvalidArgument: (:) [Push-Location], ParameterBindingException
        + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.PushLocationCommand>

  3. 最后,我在执行"The syntax of the command is incorrect"时收到错误Rmdir

  4. 我使用的功能如下。我检查了发送给函数的参数,它们是正确的。

    #Checks out a copy from svnPath into appFilePath
    Function Checkout-WorkingCopy($svnPath, $appFilePath) {
    
        Echo "Checking out from $svnPath to $appFilePath"
    
        svn checkout $svnPath $appFilePath --quiet
    
        Echo "Checkout Done"
    }
    
    #Deletes the working copy previously checked out
    Function Cleanup-WorkingCopy($directory, $appFilePath) {
    
        Pushd $directory
        Cmd /C "Rmdir /S /Q $appFilePath"
    }
    

1 个答案:

答案 0 :(得分:3)

看起来$ directory正在传递一组值。当你需要调试这样的东西时,请尝试检查传入的值。而不是shell外向cmd.exe只使用PowerShell的Remove-Item cmdlet:

Function Cleanup-WorkingCopy($directory, $appFilePath) { 
    $directory | Foreach {"$($_.GetType().Fullname): $_" }
    Pushd $directory 
    Remove-Item $appFilePath -Recurse -Force
}

实际上,使用这种方法,您根本不需要$目录,可以用以下内容替换整个函数:

Remove-Item $appFilePath -Recurse -Force