Question

$to="\\SERVER\Share\Folder"
if(!(test-path $to))
{
    ...
}   

If the server is offline (resolved name but no pings), the call from test-path never returns (in my narrow minded understanding of script time). It simply takes too long to fail. Finally I get the much anticipated result False after making toasts with butter, finding no coffee, running out to buy some, waiting for the water to boil again and finding my toasts cold. Returning to the screen and seeing it appear right there...

Same path with classic DOS dir fails as expected after some realistic time:

dir "\\SERVER\Share\Folder"

Says "The network path was not found." in less than 3 minutes!

This behavior of PS really breaks my script.

To be precise

$t0 = get-date; test-path "\\SERVER\Share\Folder"; $t1 = get-date; ($t1-$t0).TotalSeconds

Gave me

False
595.2214875

Hint: replace SERVER with a resolving local net offline PC name

Ah the question is: 1) How do you deal with this? 2) What the heck is the trouble there?

Update: The timings are the same when using an offline IP address. DNS is out of the game.

Update2: I wonder, is anyone experiencing the same wasted time? Tried on 64bit W2k8R2 server and there are no issues there. The call returns in 30s max. My problem is on a 32bit Windows XP PC.

Thanks, Rob

Was it helpful?

Solution

I can't simulate this scenario, but I have a suggestion. Try pinging the server first then attempt to make the connection.

 $(Test-Connection -ComputerName SERVER -ErrorAction SilentlyContinue -Count 1).StatusCode

This command will return 0 if the ping is successful, so you can use it in a conditional statement.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top