4
$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

Robert Cutajar
  • 3,181
  • 1
  • 30
  • 42
  • That last sample ran in 6 seconds when I tried it. If this is done in a business, your problem is most likely because of recursion(forwarders) on DNS servers(or WINS if you have that). If your dns server can't find the name, it will most likely ask it's forwarders. If you use a FQDN like "server.company.fqdn.domain", does work faster? By using FQDN you would find the correct zone fast, and it won't use the forwarders, which might speed it up – Frode F. Feb 18 '13 at 15:47
  • @Graimer providing FQDN does not help here – Loïc MICHEL Feb 18 '13 at 15:57
  • It was just an untested idea(no lab here atm), but can you explain why it is 100% impossible that it will improve the time? If he uses FQDN in his script(which is good practice anyway), he will find the correct DNS zone and get a not-exist error faster. I'm not saying it's better then `ping`, but it should still improve it – Frode F. Feb 18 '13 at 16:03
  • @Graimer, no explaination just made some tests with or without domain name...the latency is not due to dns query in this case – Loïc MICHEL Feb 18 '13 at 16:05
  • okey, because using IP is 66 sec, using netbios-name(like "server") is 7 sec slow and using fqdn to non-exisiting domain is 2 sec. At least where I am, and I'm not connected to a network with multiple local subnets. :-) – Frode F. Feb 18 '13 at 16:09
  • @Graimer thanks, unfortunately it's the same story with using an IP address. DNS is out of the game. – Robert Cutajar Feb 18 '13 at 16:15

1 Answers1

5

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.

Musaab Al-Okaidi
  • 3,734
  • 22
  • 21
  • Thanks, well it would give me the speed (+1), but complicate things much and remove the flexibility. It doesn't have to be a UNC path and I have like a hundred of servers in my configuration to work with. I could use regexp to extract the computer name though. Also, any answer to Q2 I added just a bit later? Let's see if there's more views. Kind regards – Robert Cutajar Feb 18 '13 at 16:04
  • 2
    You can also use this to extract servername: `$server = "\\SERVER\Share\Folder"; ([uri]$server).Host` returns `server` – Frode F. Feb 18 '13 at 16:06
  • Q2 is a difficult one. It would really be hard to tell what's the exact problem here. It might be something related to your specific environment setup. Although your tests suggests otherwise, I think @Graimer diagnostics sound logically correct. It would be very beneficial if you could run some tests outside of the network you're on and observe the times. – Musaab Al-Okaidi Feb 18 '13 at 16:17
  • @Graimer, cool tip! @Musaab any offline IP I try... `test-path "\\10.0.0.222\notthere"` makes me 10 minutes older, what else? – Robert Cutajar Feb 18 '13 at 16:23
  • Sorry I can't think of anything else. I can't test with offline machine names, but when I do test with random machine names that don't exist I get a quick reply. – Musaab Al-Okaidi Feb 18 '13 at 16:33
  • I just tried on a server (W2008R2, 64bit, `$Host.Version` is 2.0.-1.-1) rather than on my PC (WXP :D, 32bit, `$Host.Version` is 2.0.-1.-1) and there's some interesting observation: When the IP is out of range (unreachable, no route to host) it comes back instantly. When the IP is reachable (local network mask applies) but offline, the `test-path` returns after 30s! It might seem it's either the archaic OS I'm using, or an issue with routing... – Robert Cutajar Feb 18 '13 at 16:47