Link to home
Start Free TrialLog in
Avatar of compdigit44
compdigit44

asked on

Script to Check to see if a UNC path is Reachable

I opened the following question originally

https://www.experts-exchange.com/questions/28534659/Script-to-See-If-URL-is-Accessable.html 

In finding a script that would check to see if a UNC path is readable. I have 500 UNC path's to check and have created a CSV file. Please note my UNC paths do contain space and the suggested scripts have been tried both with and with out quotes.  Even though I have had great help from the EE experts nothing has worked so far. Even the powershell script runs but always return False for the UNC path...

Thoughts or suggestions
Avatar of Joshua Grantom
Joshua Grantom
Flag of United States of America image

Change your file from a csv to a txt and make sure there is only one path per line.

\\Test unc\number 2
\\Test unc\number 3
\\Test unc\number 4


$paths = Get-Content c:\filename.txt
foreach ($path in $paths) {
$exists = test-path $path
"$path,$exists" >> C:\file.txt
}

Open in new window


This will output

\\Test unc\number 2,true
\\Test unc\number 3,true
\\Test unc\number 4,true

Then you can import into excel and it will separate the column
Avatar of compdigit44
compdigit44

ASKER

Thanks but the script ran but listed all Paths as false and I hope this is not correct since my sample UNC path's are know good paths
I tried this on my system and it worked perfectly fine, are these directory UNC paths or printers like your previous post?
Also for anyone else looking for this, here is another example that actually exports to csv using tab delimiter

$paths = Get-Content c:\filename.txt
foreach ($path in $paths) {
$exists = test-path $path
"$path`t$exists" | Out-File -FilePath C:\file.csv -append
}

Open in new window

compdigit44,

I also verified this works with UNC paths with spaces.
I am running powershell verison 3.0 if that makes a difference
It should work as far as I know...

try this

$paths = Get-Content c:\filename.txt
foreach ($path in $paths) {
$exists = test-path "'$path'"
"$path`t$exists" | Out-File -FilePath C:\file.csv -append
}

Open in new window

Same result... :(
I tried it on another workstation using powershell 2.0 and got the same result.

The script runs but all UNC paths return as false
There is something wrong with your files or permissions then because I am running these just fine.
Can you past a few of the paths you are checking?? you can blur out the name but I need to see how the file looks.

if you run this single line

test-path "\\my uncpath\folder" does it come back true?
If I run test-path again a single UNC path manully it works
Here is a sample Text file as requested...
Sample.txt
Those are all printers, test-path does not work on printers, only directories. Let me look around
but when I use test-path on one UNC printer mapping it works
is that printer by chance the printer you have connected to your workstation?
Yes it is connected
So how can i get this script to work for printer UNC path's. I am still confused as to why the script with not with with printers
It does not work because it is only meant to check file or directory paths only. Thats just how it was designed.

See here
http://technet.microsoft.com/en-us/library/hh849776.aspx
It also applies to Powershell 3.0 even though it says 4.0
so there is no way to do this via script???? This is going to take a while with 500 printers
Do you have to check by UNC path or do you also have the IP addresses of all of the printers?

I'm still thinking if there is a way
no I have to check by UNC path because as printer move in our environment there names change but keep the same IP
tomorrow I am going to create a file that list all UNC path and on each line have test-path,,

For Example
test-path  \\serverA\Test 1
test-path  \\serverB\Test 2

I will let you know how I make out
using a modification of that script, you can create the entire file. I would just check a few lines to make sure the output is right. Im still looking

$paths = Get-Content c:\filename.txt
foreach ($path in $paths) {
Write-Output "test-path '$path' >> C:\testpathresults.txt"| Out-File -FilePath C:\testpathfile.txt -append
}

Open in new window


You can also leave the ">> C:\testpathresults.txt" out if you want to see the results on screen
Thanks I tried to use test-path listing it for each line of my UNC path and it returned false ... Even doing one printer had the same result.

I am surprised there isn't an easier way to do this.
Normally printers are managed by their IP address not their UNC paths.
Here is the background. I am in the process of doing a large print server migration from our Window 2008 cluster to 2008 R2.

Some queue names have been renamed on the new cluster and to make sure users printer are mapped to the correct file on the new server we have created a reference file that list all old queue names on one column an the new name in the next column.

We have a total of 653 printers. And I need to know which printer get renamed so I can be me spreadsheet correct. This is why I need to check to see if the UNC patch is reachable instead of typing in each one line by line.
I'm working on something to help, it may be a backwards way of doing it but I think it will at least get the job done. Trying to find a way to return errors
OK, I dont know if this is the right way to do it, I am not an expert in powershell but this works.

It tries to open each printers queue and if it does, it writes success, if not, writes error.
All other windows need to be closed except Powershell because I use Main window titles to query if successful or not and use a command to send key combination ALT+F4 to the active window to close each print queue and error message.

If this doesn't work, I am out of options :(

# Script Author: Joshua Grantom 
# Version: 1.0
# Purpose: Checks a list of Printers by UNC path to see if they are connectable

#Printer List (1 UNC Printer path per line)
$printers = Get-Content "c:\PrinterList.txt"
#CSV File to Output to
$outputfile = "c:\filename.csv"
#Adds Header to CSV File
"Printer UNC Path `t Connection Status" | Out-File $outputfile -append
#Tries to connect to each printers queue (does not require drivers to be installed)
foreach ($printer in $printers) {
Invoke-Expression -command "rundll32 printui,PrintUIEntry /o /n '$printer'"
#Sleep time in seconds until script resumes (can increase if it takes longer to query printer)
Start-Sleep -s 2
$window = Get-Process rundll32 | Where-Object {$_.MainWindowTitle -like "*"} | Select MainWindowTitle | ft -HideTableHeaders | Out-String
IF([string]::IsNullOrWhiteSpace($window)){
    #If rundll32 window title is blank, usually means error, write error
    "$printer`t Error Connecting" | Out-File $outputfile -append
}else{
    #If rundll32 window title is not blank, usually means queue is opened, write success
    "$printer`t Successfully Connected" | Out-File $outputfile -append
}
# Closes active window (print queue or error message)
[System.Windows.Forms.SendKeys]::SendWait("%{F4}") 
}

Open in new window

Any luck with the above version?
I have not had a chance to try this today since I was out sick but will try it first thing tomorrow...

sorry for the delay but regardless THANK YOU!!!
Here are the errors on got when I ran it is powershell...

$printer`t Successfully Connected" | Out-File $outputfile -append

Unexpected token '`t' in expression or statement.

The string is missing the terminator:

Missing closing '}' in statement block.
What version of powershell are you running? Is this one the 3.0 or 2.0 workstation
ASKER CERTIFIED SOLUTION
Avatar of Joshua Grantom
Joshua Grantom
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
IT running!!!!

THe script is walking though 300 printers now and I should not the results shortly..

I see the print queue pop-up for each printer. When the script finishes will I have 300 mapped printers now???

You are a genius
the printers shouldnt map, its just opening the queues. I tested that also.

Glad its working for you! remember, if you see an error box when trying to open a queue, dont worry, the script will close it and mark that printer as not connectable.
It seems to be working...

I did get some error message about not being able to connect or pull the drive but did not see any messages in the output file stating it wasn't able to connect.

Would these print not be listed on the output file??
It should show Error Connecting if it could not pull up the printer queue like below

User generated image
I wrote this script on Windows 7 x64 with WMF 4.0 which includes Powershell 4.0.

Update your Powershell on your workstation to 4.0 (it is backwards compatible) and try again if you want.

http://social.technet.microsoft.com/wiki/contents/articles/21016.how-to-install-windows-powershell-4-0.aspx
You are a genius!!!

I did get a lot of unable to connect to server message in my old cluster for all message in the output file listed it as successfull
I'm sorry, I don't understand your last comment. Well, except the Genius part! lol

So are they all showing up as Successful?
The script would run the it a queue it could not connect yet in the output file I stil see it listed as successfull
try upgrading to WMF 4.0 as stated in above post.
Upgrading to WMF 4.0 did the trick...

You are a scripting genius... I wish I could award more than 500 points thought!!!!


Thanks Again
Sweet! Why thank you very much :)

Glad to help, it was definitely a "think outside of the box" solution.