Solved

Script to Check to see if a UNC path is Reachable

Posted on 2014-10-16
45
646 Views
Last Modified: 2014-10-22
I opened the following question originally

http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28534659.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
0
Comment
Question by:compdigit44
  • 24
  • 21
45 Comments
 
LVL 16

Expert Comment

by:Joshua Grantom
ID: 40384383
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
0
 
LVL 19

Author Comment

by:compdigit44
ID: 40384488
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
0
 
LVL 16

Expert Comment

by:Joshua Grantom
ID: 40384497
I tried this on my system and it worked perfectly fine, are these directory UNC paths or printers like your previous post?
0
 
LVL 16

Expert Comment

by:Joshua Grantom
ID: 40384501
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

0
 
LVL 16

Expert Comment

by:Joshua Grantom
ID: 40384534
compdigit44,

I also verified this works with UNC paths with spaces.
0
 
LVL 19

Author Comment

by:compdigit44
ID: 40384544
I am running powershell verison 3.0 if that makes a difference
0
 
LVL 16

Expert Comment

by:Joshua Grantom
ID: 40384572
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

0
 
LVL 19

Author Comment

by:compdigit44
ID: 40384663
Same result... :(
0
 
LVL 19

Author Comment

by:compdigit44
ID: 40384779
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
0
 
LVL 16

Expert Comment

by:Joshua Grantom
ID: 40384798
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?
0
 
LVL 19

Author Comment

by:compdigit44
ID: 40384879
If I run test-path again a single UNC path manully it works
0
 
LVL 19

Author Comment

by:compdigit44
ID: 40384888
Here is a sample Text file as requested...
Sample.txt
0
 
LVL 16

Expert Comment

by:Joshua Grantom
ID: 40384897
Those are all printers, test-path does not work on printers, only directories. Let me look around
0
 
LVL 19

Author Comment

by:compdigit44
ID: 40384917
but when I use test-path on one UNC printer mapping it works
0
 
LVL 16

Expert Comment

by:Joshua Grantom
ID: 40384959
is that printer by chance the printer you have connected to your workstation?
0
 
LVL 19

Author Comment

by:compdigit44
ID: 40385011
Yes it is connected
0
 
LVL 19

Author Comment

by:compdigit44
ID: 40385016
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
0
 
LVL 16

Expert Comment

by:Joshua Grantom
ID: 40385032
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
0
 
LVL 16

Expert Comment

by:Joshua Grantom
ID: 40385041
It also applies to Powershell 3.0 even though it says 4.0
0
 
LVL 19

Author Comment

by:compdigit44
ID: 40385072
so there is no way to do this via script???? This is going to take a while with 500 printers
0
 
LVL 16

Expert Comment

by:Joshua Grantom
ID: 40385085
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
0
 
LVL 19

Author Comment

by:compdigit44
ID: 40385122
no I have to check by UNC path because as printer move in our environment there names change but keep the same IP
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 19

Author Comment

by:compdigit44
ID: 40385527
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
0
 
LVL 16

Expert Comment

by:Joshua Grantom
ID: 40385538
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
0
 
LVL 19

Author Comment

by:compdigit44
ID: 40386456
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.
0
 
LVL 16

Expert Comment

by:Joshua Grantom
ID: 40386949
Normally printers are managed by their IP address not their UNC paths.
0
 
LVL 19

Author Comment

by:compdigit44
ID: 40387000
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.
0
 
LVL 16

Expert Comment

by:Joshua Grantom
ID: 40387279
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
0
 
LVL 16

Expert Comment

by:Joshua Grantom
ID: 40387471
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

0
 
LVL 16

Expert Comment

by:Joshua Grantom
ID: 40392454
Any luck with the above version?
0
 
LVL 19

Author Comment

by:compdigit44
ID: 40393198
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!!!
0
 
LVL 19

Author Comment

by:compdigit44
ID: 40394404
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.
0
 
LVL 16

Expert Comment

by:Joshua Grantom
ID: 40394444
What version of powershell are you running? Is this one the 3.0 or 2.0 workstation
0
 
LVL 16

Accepted Solution

by:
Joshua Grantom earned 500 total points
ID: 40394508
This should work in version 3.0, I changed the 2 filenames, saved as a ps1 and ran it from Powershell command line.

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

#Loads Windows Forms Assembly
[Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
#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

0
 
LVL 19

Author Comment

by:compdigit44
ID: 40394703
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
0
 
LVL 16

Expert Comment

by:Joshua Grantom
ID: 40394727
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.
0
 
LVL 19

Author Comment

by:compdigit44
ID: 40395031
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??
0
 
LVL 16

Expert Comment

by:Joshua Grantom
ID: 40395048
It should show Error Connecting if it could not pull up the printer queue like below

snip.PNG
0
 
LVL 16

Expert Comment

by:Joshua Grantom
ID: 40395062
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
0
 
LVL 19

Author Comment

by:compdigit44
ID: 40395342
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
0
 
LVL 16

Expert Comment

by:Joshua Grantom
ID: 40395346
I'm sorry, I don't understand your last comment. Well, except the Genius part! lol

So are they all showing up as Successful?
0
 
LVL 19

Author Comment

by:compdigit44
ID: 40395554
The script would run the it a queue it could not connect yet in the output file I stil see it listed as successfull
0
 
LVL 16

Expert Comment

by:Joshua Grantom
ID: 40395561
try upgrading to WMF 4.0 as stated in above post.
0
 
LVL 19

Author Comment

by:compdigit44
ID: 40397627
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
0
 
LVL 16

Expert Comment

by:Joshua Grantom
ID: 40397630
Sweet! Why thank you very much :)

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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

"Migrate" an SMTP relay receive connector to a new server using info from an old server.
A procedure for exporting installed hotfix details of remote computers using powershell
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now