I have a few file servers that I want to create a path if it doesn't exist... Simple right? I thought so, BUT the path is along a hidden admin share.
Just when I think I am getting decent at scripting a simple problem trolls the $h#@$#@ out of me...
Here is the code I scratched out:
Cls
#check for fileshare
$servers = get-content C:\Temp\test.txt
forEach ($server in $servers) {
$shareTest = Test-Path "\\$Server\Share$\Test"
if($shareTest){
Write-Host "Path Exists on $server"
}
Else{
New-Item -Path "\\$Server\Share$\" -Name Test -ItemType Directory
Write-Host "Path Exists on $server"
}
}
I receive the error:
New-Item : The path is not of a legal form. At line:5 char:7
Presumably this is because there is a $ in the path. I am not sure how to escape it, or whatever voodoo I need to do to make it work...
This has cropped up a couple times in different scripts and I would like to clamp it off before I bleed out...
Thanks gang.
I've run into odd situations where the '$' was interpreted oddly via variable expansion. You can get around this where it's problematic via string tokenization:
$server = 'hiyo'
'\\{0}\{1}' -f $server, 'share$'