Rahul
Rahul

Reputation: 1876

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:

  1. Instead of echoing "Checking out from svn://somePath1 to C:/somePath2" it echoes "Checking out from svn://somePath1 C:/somePath2 to"
  2. 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: (:) [Push-Location], ParameterBindingException
        + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.PushLocationCommand>

  3. Finally, I get the error "The syntax of the command is incorrect" while executing Rmdir.

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"
}

Upvotes: 0

Views: 3487

Answers (1)

Keith Hill
Keith Hill

Reputation: 202012

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

Upvotes: 3

Related Questions