• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1531
  • Last Modified:

Script to Check to see if a UNC path is Reachable

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
compdigit44
Asked:
compdigit44
  • 24
  • 21
1 Solution
 
Joshua GrantomSenior EngineerCommented:
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
 
compdigit44Author Commented:
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
 
Joshua GrantomSenior EngineerCommented:
I tried this on my system and it worked perfectly fine, are these directory UNC paths or printers like your previous post?
0
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

 
Joshua GrantomSenior EngineerCommented:
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
 
Joshua GrantomSenior EngineerCommented:
compdigit44,

I also verified this works with UNC paths with spaces.
0
 
compdigit44Author Commented:
I am running powershell verison 3.0 if that makes a difference
0
 
Joshua GrantomSenior EngineerCommented:
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
 
compdigit44Author Commented:
Same result... :(
0
 
compdigit44Author Commented:
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
 
Joshua GrantomSenior EngineerCommented:
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
 
compdigit44Author Commented:
If I run test-path again a single UNC path manully it works
0
 
compdigit44Author Commented:
Here is a sample Text file as requested...
Sample.txt
0
 
Joshua GrantomSenior EngineerCommented:
Those are all printers, test-path does not work on printers, only directories. Let me look around
0
 
compdigit44Author Commented:
but when I use test-path on one UNC printer mapping it works
0
 
Joshua GrantomSenior EngineerCommented:
is that printer by chance the printer you have connected to your workstation?
0
 
compdigit44Author Commented:
Yes it is connected
0
 
compdigit44Author Commented:
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
 
Joshua GrantomSenior EngineerCommented:
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
 
Joshua GrantomSenior EngineerCommented:
It also applies to Powershell 3.0 even though it says 4.0
0
 
compdigit44Author Commented:
so there is no way to do this via script???? This is going to take a while with 500 printers
0
 
Joshua GrantomSenior EngineerCommented:
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
 
compdigit44Author Commented:
no I have to check by UNC path because as printer move in our environment there names change but keep the same IP
0
 
compdigit44Author Commented:
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
 
Joshua GrantomSenior EngineerCommented:
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
 
compdigit44Author Commented:
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
 
Joshua GrantomSenior EngineerCommented:
Normally printers are managed by their IP address not their UNC paths.
0
 
compdigit44Author Commented:
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
 
Joshua GrantomSenior EngineerCommented:
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
 
Joshua GrantomSenior EngineerCommented:
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
 
Joshua GrantomSenior EngineerCommented:
Any luck with the above version?
0
 
compdigit44Author Commented:
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
 
compdigit44Author Commented:
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
 
Joshua GrantomSenior EngineerCommented:
What version of powershell are you running? Is this one the 3.0 or 2.0 workstation
0
 
Joshua GrantomSenior EngineerCommented:
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
 
compdigit44Author Commented:
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
 
Joshua GrantomSenior EngineerCommented:
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
 
compdigit44Author Commented:
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
 
Joshua GrantomSenior EngineerCommented:
It should show Error Connecting if it could not pull up the printer queue like below

snip.PNG
0
 
Joshua GrantomSenior EngineerCommented:
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
 
compdigit44Author Commented:
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
 
Joshua GrantomSenior EngineerCommented:
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
 
compdigit44Author Commented:
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
 
Joshua GrantomSenior EngineerCommented:
try upgrading to WMF 4.0 as stated in above post.
0
 
compdigit44Author Commented:
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
 
Joshua GrantomSenior EngineerCommented:
Sweet! Why thank you very much :)

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

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

  • 24
  • 21
Tackle projects and never again get stuck behind a technical roadblock.
Join Now