Tuesday 21 April 2009

Get-PrinterTestPage.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script uses WMI to print a test page 
  4.     all printers on this system 
  5. .DESCRIPTION 
  6.     This script is an MSDN sample,using PowerShell. It first 
  7.     gets all the printrs installed, prints out details then 
  8.     tries to print a test page. 
  9.      
  10.     The script also checks for printers known to not work well, and avoids using them 
  11. .NOTES 
  12.     File Name  : Get-PrinterTestPage.ps1 
  13.     Author     : Thomas Lee - tfl@psp.co.uk 
  14.     Requires   : PowerShell V2 CTP3 
  15. .LINK 
  16.     Sample posted to: 
  17.         http://pshscripts.blogspot.com/2009/04/get-printertestpageps1.html
  18.     Original MSDN sample at: 
  19.         http://msdn.microsoft.com/en-us/library/aa392757(VS.85).aspx  
  20. .EXAMPLE 
  21.     PSH [C:\foo]: .Get-PrinterTestPage.ps1' 
  22.     4 Printers defined on this system 
  23.     Printer share name: \ 
  24.     Printer Port      : C:\ProgramData\TechSmith\SnagIt 9\PrinterPortFile 
  25.     Printer share name: \Phaser PS 
  26.     Printer Port      : X_10.10.1.117 
  27.     Printer share name: \ 
  28.     Printer Port      : XPSPort: 
  29.     Printer share name: \\SLT-PC\officejet 
  30.     Printer Port      : USB002 
  31.      
  32.     Printing test page for printer: SnagIt 9 
  33.     Not printing a test page for this printer 
  34.     Printing test page for printer: Phaser PS 
  35.     Result                        : 0 
  36.     Printing test page for printer: Microsoft XPS Document Writer 
  37.     Not printing a test page for this printer 
  38.     Printing test page for printer: \\JerryGarcia\HP Officejet Pro L7400 Series 
  39.     Result                        : 0 
  40.     #> 
  41.  
  42. ### 
  43. # Start of Script 
  44. ### 
  45.   
  46. # Get Printer Objects for this computer from WMI 
  47. $printers = Get-WmiObject -Class Win32_Printer 
  48. # Display printers 
  49. "{0} Printers defined on this system" -f $printers.count 
  50.  
  51. # For printers, display printer details 
  52. foreach ($printer in $printers) { 
  53. "Printer share name: {0}\{1}" -f $printer.servername, $printer.sharename 
  54. "Printer Port      : {0}    " -f $printer.PortName   
  55. "" 
  56.  
  57. # Now Print a test page for each printer 
  58. foreach ($printer in $printers) { 
  59. "Printing test page for printer: {0}" -f $printer.name 
  60.  
  61. # avoid issue with Known bad printers
  62. if ($printer.DriverName -match "XPS" -or $printer.DriverName -match "SnagIt")  { 
  63. "Not printing a test page for this printer" 
  64. else
  65. $Result = $printer.PrintTestPage() 
  66. "Result                        : {0}" -f $Result.ReturnValue 
  67. # End of script 
Technorati Tags: ,,,

Win32_Fan Sample using PowerShell

  1. <# 
  2. .SYNOPSIS 
  3.     Uses Win32_Fan class to return information about fans in a system.     
  4. .DESCRIPTION 
  5.     This script first defines some functions to decode various  
  6.     WMI attributed from binary to text. Then the script calls  
  7.     Get-WmiObject to retrieve fan details, then formats that 
  8.     information (using the functions defined at the top of the script. 
  9.      
  10. .NOTES 
  11.     File Name  : Get-Fan.ps1 
  12.     Author     : Thomas Lee - tfl@psp.co.uk 
  13.     Requires   : PowerShell V2 CTP3 
  14. .LINK 
  15.     Script posted to: 
  16.     http://pshscripts.blogspot.com/2009/04/win32fan-sample-using-powershell.html
  17.     Original MSDN Page 
  18.     http://msdn.microsoft.com/en-us/library/aa394146(VS.85).aspx 
  19. .EXAMPLE 
  20.     Left as an exercise for the viewer.     
  21. #> 
  22.  
  23. ### 
  24. # Start of Script 
  25. ### 
  26.  
  27. # Functions to decode details 
  28.  
  29. function FanAvailability { 
  30. param ($value
  31. switch ($value) { 
  32. 1    {"Other"
  33. 2    {"Unknown"
  34. 3    {"Running on full power"
  35. 4    {"Warning"
  36. 5    {"In Test"
  37. 6    {"Not Applicable"
  38. 7    {"Power Off"
  39. 8    {"Off Line"
  40. 9    {"Off Duty"
  41. 10   {"Degraded"
  42. 11   {"Not Installed"
  43. 12   {"Install Error"
  44. 13   {"Power Save - Unknown"
  45. 14   {"Power Save - Low Power Mode"
  46. 15   {"Power Save - Standby"
  47. 16   {"Power Cycle"
  48. 17   {"Power Save - Warning"
  49. default {"NOT SET"
  50.  
  51. function ConfigManagerErrorCode { 
  52. param ($value
  53. switch ($value) { 
  54. 0 {"Device Is Working Properly"
  55. 1 {"Device is not configured correctly"
  56. 2 {"Windows Cannot load the driver for this device"
  57. 3 {"Driver for this device might be corrupted, or the system may be low on memory or other resources"
  58. 4 {"Device is not working properly. One of its drivers or the registry might be corrupted"
  59. 5 {"Driver for the device requires a resource that Windows cannot manage"
  60. 6 {"Boot configuration for the device conflicts with other devices"
  61. 7 {"Cannot filter"
  62. 8 {"Driver loader for the device is missing"
  63. 9 {"Device is not working properly. The controlling firmware is incorrectly reporting the resources for the device"
  64. 10 {"Device cannot start"
  65. 11 {"Device failed"
  66. 12 {"Device cannot fine enough free resources to run"
  67. 13 {"Windows cannot verify the devices's resources"
  68. 14 {"Device cannot work propertly until the computer is restarted"
  69. 15 {"Device is not working properly due to a possible re-enumeration problem"
  70. 16 {"Windows cannot identify all of the resources that the device uses"
  71. 17 {"Device is requesting an unknown resource type"
  72. 18 {"Device drivers must be reinstalled"
  73. 19 {"Failure using the VxD loader"
  74. 20 {"Registry might be corrupted"
  75. 21 {"System failure. if changing the device driver is ineffective, see the hardware documentation. Windows is removing the device"
  76. 22 {"Device is disabled"
  77. 23 {"System failure. if changing the device driver is ineffective, see the hardware documentation"
  78. 24 {"Device is not present, not working properly, or does not have all of its drivers installed"
  79. 25 {"Windows is still setting up the device"
  80. 26 {"Windows is still setting up the device"}  
  81. 27 {"Device does not have valid log configuration"
  82. 28 {"Device drivers are not installed"
  83. 29 {"Device is disabled. The device firmware did not provide the required resources"
  84. 30 {"Device is using an IRQ resource that another device is using"
  85. 31 {"Device is not working properly. Windows cannot load the required device drivers."
  86. default {"NOT SET"
  87.  
  88. function PowerManagementCapabilities { 
  89. param ($value
  90. switch ($value) { 
  91. 0 {"Unknown"
  92. 1 {"Not Supported"
  93. 2 {"Disabled"
  94. 3 {"Enabled"
  95. 4 {"Power Saving Modes Entered Automatically"
  96. 5 {"Power State Settable"
  97. 6 {"Power Cycling Supported"
  98. 7 {"Timed Power-On Supported"
  99. default {"NOT SET"
  100. }  
  101.  
  102. function StatusInfo { 
  103. param ($value
  104. switch ($value) { 
  105. 0 {"Other"
  106. 2 {"Unknown"
  107. 3 {"Enabled"
  108. 4 {"Disabled"
  109. 5 {"Not Applicable"
  110. default {"NOT SET"
  111.  
  112. ### 
  113. # Start of Script 
  114. ## 
  115.  
  116. # Get fan information 
  117. $fans = Get-WmiObject -Class Win32_Fan 
  118.  
  119. # Display information 
  120. $hostnm=hostname 
  121. "Fan Information on System: {0} ({1} fans in total" -f $hostnm, $fans.count 
  122. $i=1 
  123. # display details of each fan 
  124. foreach ($fan in $fans) { 
  125. # Display details 
  126. "Fan: {0}"  -f $i 
  127. "Active Cooling                : {0}" -f $fan.ActiveCooling 
  128. "Availability                  : {0}" -f (fanavailability($fan.availability)) 
  129. "Caption                       : {0}" -f $fan.caption 
  130. "Config Manager Error code     : {0}" -f (ConfigManagerErrorCode($fan.ConfigManagerErrorCode)) 
  131. "Config Manager User Config    : {0}" -f $fan.ConfigManagerUserConfig 
  132. "Creation Class Name           : {0}" -f $fan.CreationClassName 
  133. "Description                   : {0}" -f $fan.Description 
  134. "Desired speed                 : {0}" -f $fan.DesiredSpeed 
  135. "Error Cleared                 : {0}" -f $fan.ErrorCleared 
  136. "Error Description             : {0}" -f $fan.ErrorDescription 
  137. "Install Date                  : {0}" -f $fan.InstallDate 
  138. "Last Error code               : {0}" -f $fan.LastErrorCode 
  139. "Name                          : {0}" -f $fan.name 
  140. "PNP Device Id                 : {0}" -f $fan.PNPDeviceID 
  141. "Power Management Capabilities : {0}" -f (PowerManagementCapabilities($fan.PowerManagementCapabilities)) 
  142. "Status                        : {0}" -f $fan.Status 
  143. "Status Information            : {0}" -f (statusinfo($fan.StatusInfo)) 
  144. "System Creation Class Name    : {0}" -f $fan.syu 
  145. "System Name                   : {0}" -f $fan.SystemName 
  146. "Variable Speed                : {0}" -f $fan.VariableSpeed 
  147.  
  148. $i++;"" 
Technorati Tags: ,,

Sunday 19 April 2009

Get-HostEntry.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     Demonstrates use of the GetHostEntry method of System.Net.DNS Class 
  4. .DESCRIPTION 
  5.     This script is a an MSDN sample, using PowerShell 
  6. .NOTES 
  7.     File Name  : Get-HostEntry.ps1 
  8.     Author     : Thomas Lee - tfl@psp.co.uk 
  9.     Requires   : PowerShell V2 CTP3 
  10. .LINK 
  11.     Sample posted to: 
  12.     http://pshscripts.blogspot.com/2009/04/get-hostentryps1.html 
  13.     Original MSDN sample at: 
  14.     http://msdn.microsoft.com/en-us/library/system.net.dns.gethostentry.aspx  
  15. .EXAMPLE 
  16.     PSH [C:\foo]: .\Get-HostEntry.ps1 Cookham8
  17.     Host Name    : Cookham8.cookham.net 
  18.     Alias        : 
  19.     Address      : fe80::d8ed:afe2:2a97:a596%14 
  20.     Address      : fe80::3953:f67b:2f1c:1323%10 
  21.     Address      : 10.10.1.120 
  22.     Address      : 10.10.1.115 
  23. .PARAM HostName 
  24.     The name of the host to search for - the default is "localhost" 
  25. #> 
  26.  
  27. param
  28. [string] $HostName = "localhost" 
  29.  
  30. ### 
  31. # Start of Script 
  32. ### 
  33.   
  34. #Get Host details 
  35.  
  36. $hostentrydetails = [System.Net.Dns]::GetHostEntry($hostname
  37.  
  38. # Print details: 
  39. "Host Name    : {0}" -f $hostentrydetails.HostName 
  40. foreach ($alias in $hostentrydetails.alises) { 
  41. "Alias        : {0}" -f $alias 
  42. foreach ($addr in $hostentrydetails.addresslist) { 
  43. "Address      : {0}" -f $Addr.ipaddresstostring 
  44.   
  45. # End of script