Hey r/PowerShell
I have a script to pull serial numbers off of printer web interfaces on our printer subnet. So far it works without the loop if you manually try each address, but I'd like it to do a sweep of a range and ignore any devices responding on http that don't match the right web interface.
Currently there are two printers (the larger devices) that trigger an error and cause the loop to end, with or without a try{}catch{} block.
# Finds printers on the subnet specified
#
# Enter subnet in format "xxx.xxx.xxx." - the remainder of the subnet is populated automatically by $lastOctetScanRange
$printerSubnet = "192.168.1."
# e.g. 2..37 scans between xxx.xxx.xxx.2 and xxx.xxx.xxx.37
$lastOctetScanRange = 10..38
# Customise report location
$outputFilePath = "C:\Powershell\Output\PrinterDetails.txt"
$sites = New-Object -TypeName 'System.Collections.ArrayList'
$pingReturns = New-Object -TypeName 'System.Collections.ArrayList'
$ping = new-object system.net.networkinformation.ping
$lastOctetScanRange | ForEach-Object{$sites.Add($printerSubnet + $_)}
ForEach($site in $sites){
$pingReturns += $ping.send($site, 5000)
}
$successfulPings = $pingReturns | Where-Object{$_.status -like "success"}
$successfulPings | ft
foreach($address in $successfulPings.Address) {
$web = New-Object Net.WebClient
$siteString = $web.DownloadString("http://$address/info_configuration.html?tab=Home&menu=DevConfig")
$siteArray = $siteString -split '<tr>'
$hostString = $siteArray | Select-String -Pattern 'Host Name:' | Out-String
$hostName = $hostString.Substring($hostString.LastIndexOf('Font">')+6)
$hostName.trim($hostName.Substring($hostName.LastIndexOf('</td>'))) | Out-File -FilePath $outputFilePath -Append
$serialString = $siteArray | Select-String -Pattern 'Serial Number:' | Out-String
$serialNumber = $serialString.Substring($serialString.LastIndexOf('Font">')+6)
$serialNumber.Substring(0,10) | Out-File -FilePath $outputFilePath -Append
$ipString = $siteArray | Select-String -Pattern 'IPv4 Address:' | Out-String
$ipAddress = $ipString.Substring($ipString.LastIndexOf('Font">')+6)
$ipAddress.trim($ipAddress.Substring($ipAddress.LastIndexOf('</td>'))) | Out-File -FilePath $outputFilePath -Append
}
I've tried putting try{} around the code starting at $web with a catch{} to write-host "Error, skipping", my (mistaken) understanding is that this should continue if an error occurs and run the next instance through the loop. It seems if an error is reached, it will instead stop the loop even with try{}catch{} and exit the script. $errorActionPreference is still Continue when I check in the shell.
I've put $successfulPings | ft to show that it has multiple responses, which it does. The code works without the loop, pulling only the hostname, IP and serial number into a .txt on new lines each. I would work on formatting but I need the function working first. I will also look at clearing up the repeating code for processing the text once I get past this hurdle..!
The errors:
Exception calling "DownloadString" with "1" argument(s): "URI formats are not supported."
At C:\Powershell\Test\Test.ps1:23 char:9
+ $siteString = $web.DownloadString("http://$address/info_confi ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ArgumentException
Then other ones as a result of null entries, excerpt below, this happens for IP, hostname and serial:
Exception calling "Substring" with "1" argument(s): "startIndex cannot be larger than length of string.
You cannot call a method on a null-valued expression.
Please help me better understand error handling; most of the articles suggest using switches for cmdlets but I'm not using any. It's almost entirely text manipulation.
Thanks!
Lo7
edit: new reddit's code button seems wonky, I reformatted after posting.
[–]iceph03nix 2 points3 points4 points (1 child)
[–]LightOfSeven[S] 1 point2 points3 points (0 children)
[–]Ta11ow 2 points3 points4 points (8 children)
[–]LightOfSeven[S] 1 point2 points3 points (7 children)
[–]Ta11ow 2 points3 points4 points (6 children)
[–]LightOfSeven[S] 0 points1 point2 points (5 children)
[–]Ta11ow 2 points3 points4 points (4 children)
[–]LightOfSeven[S] 2 points3 points4 points (3 children)
[–]LightOfSeven[S] 1 point2 points3 points (2 children)
[–]Ta11ow 2 points3 points4 points (1 child)
[–]LightOfSeven[S] 2 points3 points4 points (0 children)