Search code examples
xmlpowershellunc

Powershell Get-childitem returns "Cannot find path" for UNC value from XML


I know this is a simple thing, but for the life of me I can't seem to make it work. We have a script that loads values from an XML config file formatted as such:

<configuration>
<global>
    <rootBuildPath>\\devint1\d`$\Builds\</rootBuildPath>
</global>
</configuration>

#Load the xml file
$xmlfile = [xml](get-content "C:\project\config.xml")
# get the root path
$rootBuildPath = $xmlfile.configuration.global.rootBuildPath

$currentRelease = Get-ChildItem $rootBuildPath -Exclude "Latest" | Sort -Descending LastWriteTime | select -First 1

# do some stuff with the result, etc.

Now what happens is get-childitem throws a

Get-ChildItem : Cannot find path '\\devint1\d`$\Builds' because it does not exist.

If I run the command in the shell it works but for some reason if I try and use the value from the XML file it fails. I've tried escaping the backtick and removing the backtick to no avail.

I can't use a share to make this happen.

Thoughts?


Solution

  • The reason you are getting an error is because the type of $rootBuildPath when you get it from the xml file is string. This results in the equivalent of calling

    Get-ChildItem '\\devint1\d`$\Builds\' -Exclude "Latest" | ...
    

    which will throw the exception you are seeing. The reason it doesn't throw an error when you run

    Get-ChildItem \\devint1\d`$\Builds\ -Exclude "Latest" | ...
    

    from the command line is powershell is parsing the path as a path first before handing it to the Get-ChildItem commandlet.

    In order to make your code work, you must remove the errant '`' from the path before calling Get-ChildItem.