It turns out that Join-Path can't be used reliably for remote paths, allow me to explain
This will work fine:
PS C:\Users\John> Join-Path "\\SecretMachine\E$\SecretDirectory" "ReallySecretDirectory"
\\SecretMachine\E$\SecretDirectory\ReallySecretDirectory
But this won't:
PS C:\Users\John> Join-Path "E:\SecretDirectory" "ReallySecretDirectory"
Join-Path : Cannot find drive. A drive with the name 'e' does not exist.
At line:1 char:10
+ Join-Path <<<< "e:\SecretDirectory" "ReallySecretDirectory"
+ CategoryInfo : ObjectNotFound: (e:String) [Join-Path], DriveNotFoundException
+ FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.JoinPathCommand
Essentially, Join-Path can handle non-existent UNC paths but if it looks like a local path it expects it to be there.
You can force Join-Path to try to resolve the UNC path but it doesn't look like you can prevent it for local paths.
PS C:\Users\John> Join-Path "\\SecretMachine\F$" "SecretDirectory" -Resolve
Join-Path : Cannot find path '\\SecretMachine\F$\SecretDirectory' because it does not exist.
At line:1 char:10
+ Join-Path <<<< "\\SecretMachine\F$" "SecretDirectory" -Resolve
+ CategoryInfo : ObjectNotFound: (\\SecretMachine\F$\SecretDirectory:String) [Join-Path], ItemNotFoundExc
eption
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.JoinPathCommand
I've faced the same issue and this is ridiculous! I've always assumed that Join-Path is a handy way to glue paths and don't mess with slashes manually. I was wrong. Moreover, I hit this bug: http://connect.microsoft.com/PowerShell/feedback/details/781816/join-path-cannot-find-drive-a-drive-with-the-name-e-does-not-exist and it makes Join-Path completely unusable in my case. What a day!
ReplyDeleteSame issue in powershell 5.0
ReplyDelete