Scripts/Test-PrinterStatus.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#requires -Version 3.0 -Modules PrintManagement
function Test-PrinterStatus
{
  <#
    .SYNOPSIS
    Tests the status of the printers on the network.
 
    .DESCRIPTION
    What started as a oneline script to find out which printers are erroring out has turned into this module. It creates two files, one that has a list of all of the printers and one that has a list of the printers that are not in a "Normal" status. It also finds the port IP Address and attempts to "ping" it. It returns those results to the screen.
 
 
    .PARAMETER PrintServer
    Assigns the print server name to the variable.
 
    .PARAMETER PingReportFolder
    Assigns a location to where the files will be stored.
 
    .EXAMPLE
    Test-PrinterStatus -PrintServer Value -PingReportFolder Value
    The simple form of this returns the staus of the printers.
 
    .LINK
    https://knarrstudio.github.io/ITPS.OMCS.Tools/
    The first link is opened by Get-Help -Online Test-PrinterStatus
 
    .INPUTS
    Print server Name
    Report location
 
    .OUTPUTS
    Screen and Files
  #>



  param
  (
    [Parameter(Mandatory = $true,HelpMessage = 'Add PrintServer name', Position = 0)]
    [string]$PrintServer,
    
    [Parameter(Mandatory = $true,HelpMessage = '\\NetworkShare\Reports\PrinterStatus or c:\temp',Position = 1)]
    [string]$PingReportFolder
  )
  
  $BadCount = 0
  $DateStamp = Get-Date -UFormat %Y%m%d-%H%M%S
  $ReportFile = (('{0}\{1}-PrinterReport.csv' -f $PingReportFolder, $DateStamp))
  $PrinterSiteList = (('{0}\{1}-FullPrinterList.csv' -f $PingReportFolder, $DateStamp))
  $i = 0
  
  if(!(Test-Path -Path $PingReportFolder))
  {
    New-Item -Path $PingReportFolder -ItemType Directory
  }
  
  Get-Printer -ComputerName $PrintServer |
  Select-Object -Property Name, PrinterStatus, DriverName, PortName, Published |
  Export-Csv -Path $PrinterSiteList -NoTypeInformation
  
  $PrinterList = Import-Csv -Path $PrinterSiteList # -Header Name
  $TotalPrinters = $PrinterList.count -1
  
  if($TotalPrinters -gt 0)
  {
    foreach($OnePrinter in $PrinterList)
    {
      $PortName = $OnePrinter.PortName
      $PrinterName = $OnePrinter.Name
      if ($PrinterName -ne 'Name')
      {
        $PortIpAddress = (Get-PrinterPort -ComputerName $PrintServer -Name $PortName).PrinterHostAddress 
        #[String]$PortIpAddress = (Get-PrinterPort -ComputerName $PrintServer -Name $PrinterName -ErrorAction SilentlyContinue).PrinterHostAddress | Select-Object -Property PrinterHostAddress -ErrorAction SilentlyContinue
        if ($PortIpAddress)
        {
          $PingPortResult = Test-Connection -ComputerName $PortIpAddress -Count 1 -Quiet 
          if($PingPortResult -eq $false)
          {
            Write-Host ('The printer {0} failed to respond to a ping! ' -f $PrinterName) -f Red
          }
          elseif($PingPortResult -eq $true)
          {
            Write-Host ('The printer {0} responded to a ping! ' -f $PrinterName) -f Green
          }
        }
      }
      
      #Start-Sleep -Seconds .5
      Write-Progress -Activity ('Testing {0}' -f $PrinterName) -PercentComplete ($i / $TotalPrinters*100)
      $i++
      if($OnePrinter.PrinterStatus -ne 'Normal')
      {
        $BadCount ++
        $PrinterProperties = $OnePrinter
        if($BadCount -eq 1)
        {
          $PrinterProperties | Export-Csv -Path $ReportFile -NoClobber -NoTypeInformation
        }
        else
        {
          $PrinterProperties | Export-Csv -Path $ReportFile -NoTypeInformation -Append
        }
      }
    }
  }
  
  Write-Verbose -Message ('Total Printers found: {0}' -f $TotalPrinters)
  Write-Verbose -Message ('Total Printers not in a Normal Status: {0}' -f $BadCount)
  Write-Verbose -Message "This test was run by $env:USERNAME from $env:COMPUTERNAME"
  Write-Verbose -Message ('You can find the full report at: {0}' -f $ReportFile)
}




# SI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