Weird behaviour in Powershell script
I check out a working copy of a project using Checkout-WorkingCopy and later delete the directory using Cleanup-WorkingCopy (the code for these functions is at the end of this post).
Problems I am facing:
- Instead of echoing
"Checking out from svn://somePath1 to C:/somePath2"
it echoes"Checking out from svn://somePath1 C:/somePath2 to"
In Cleanup-WorkingCopy, it throws the following error while trying to execute
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: (:)开发者_如何学JAVA [Push-Location], ParameterBindingException + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.PushLocationCommand>
Finally, I get the error
"The syntax of the command is incorrect"
while executingRmdir
.
The functions I use are given below. I have checked the arguments sent to the functions and they are correct.
#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"
}
It looks like $directory is geting passed an array of values. When you need to debug stuff like this try inspecting the value passed in. Also rather than shell out to cmd.exe just use PowerShell's Remove-Item cmdlet:
Function Cleanup-WorkingCopy($directory, $appFilePath) {
$directory | Foreach {"$($_.GetType().Fullname): $_" }
Pushd $directory
Remove-Item $appFilePath -Recurse -Force
}
Actually with this approach you don't need $directory at all and could replace the whole function with:
Remove-Item $appFilePath -Recurse -Force
精彩评论