The script works fine and will toggle through the printers on the servers and determine if the printers are shared (TRUE). But the problem arises if the last printer on the server happens to not be shared, it then changes the shared to (False). which skews the results in the spreadsheet stating that the server is not a print server although it is. I need to some how create a loop stating that if we find one shared printer on a server, stop the search and place the server in the spread sheet flagged as a print server.
I would like to put in something like
$Printers = gwmi Win32_Printer -computername $StrComputer
if ($printers.shared -eq "True){# then stop searching that machine
and place it in the speadsheet as a print server}
Here is my code :
foreach ($StrComputer in $colComputers){
$Printers = gwmi Win32_Printer -computername $StrComputer
$GenItems1 = gwmi Win32_OperatingSystem -Comp $StrComputer
# Populate Printer Sheet with information
foreach ($objItem in $GenItems1){
$Sheet1.Cells.Item($intRow, 1) = $StrComputer
$Sheet1.Cells.Item($intRow, 2) = $objItem.Caption
$Sheet1.Cells.Item($intRow, 3) = $objItem.CSDVersion
}
foreach ($objItem in $Printers){
$Sheet1.Cells.Item($intRow, 4) = $objItem.Shared
}
$de = New-Object System.DirectoryServices.DirectoryEntry
$ds = New-Object System.DirectoryServices.DirectorySearcher
$ds.SearchRoot = $de
$ds.Filter = "(&(objectCategory=computer)(objectClass=computer)(samAccountName=$($StrComputer)$))"
$ds.SearchScope = "SubTree"
$r = $ds.FindOne()
$r.Path
$Sheet1.Cells.Item($intRow, 5) = $r.Path
$intRow = $intRow + 1}
}
change this:
with this: