Sunday, 4 January 2009

Get-NonWorkingDevices.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     Uses Win32_PNPEntity to return information about non-working devices.
  4. .DESCRIPTION 
  5.     This script calls Get-WmiObject to retrieve plug and play details,  
  6.     then formats and displays non-working devices. The script also has to 
  7.     work around how WMI returns 0 and 1 object (i.e. no $obj.count). 
  8.      
  9.     This is also sample 6 on http://msdn.microsoft.com/en-us/library/aa394587(VS.85).aspx 
  10.     recoded with PowerShell. 
  11. .NOTES 
  12.     File Name  : Get-NonWorkingDevices.ps1 
  13.     Author     : Thomas Lee - tfl@psp.co.uk 
  14.     Requires   : PowerShell V2 CTP3 
  15. .LINK 
  16.     Script posted to: 
  17.     http://www.pshscripts.blogspot.com 
  18.     Original MSDN Page 
  19.     http://msdn.microsoft.com/en-us/library/aa394587(VS.85).aspx 
  20. .EXAMPLE 
  21.     PS C:\foo> Get-NonWorkingDevices.ps1 
  22.     No bad devices on Cookham8 
  23. .EXAMPLE 
  24.     PS C:\foo> Get-NonWorkingDevices.ps1 
  25.     Total Bad devices on Cookham8: 1 
  26.     Name           : NETGEAR FA311v2 PCI Adapter - Virtual Network 
  27.     Class Guid     : {4d36e972-e325-11ce-bfc1-08002be10318} 
  28.     Description    : Microsoft Virtual Network switch Adapter 
  29.     Device ID      : ROOT\VMS_MP\0001 
  30.     Manufacturer   : 
  31.     PNP Device Id  : ROOT\VMS_MP\0001 
  32.     Service Name   : VMSMP 
  33. #> 
  34.  
  35. ### 
  36. #   Start of Script 
  37. ### 
  38.  
  39. # Get non-working devices: 
  40. $BadDevices = Get-WmiObject Win32_PNPEntity | Where {$_.ConfigManagerErrorcode -ne 0} 
  41.  
  42. # Display bad devices  
  43. $Hostname = Hostname 
  44. if (!$BadDevices) { 
  45.    "No bad devices on {0}" -f $Hostname 
  46. # end if 
  47. else
  48. if (!$BadDevices.Count) {$Count=1} else {$Count=$BadDevices.count} 
  49. "Total Bad devices on {0}: {1}" -f $Hostname, $Count 
  50. foreach ($Device in $BadDevices) { 
  51. "Name           : {0}" -f $Device.Name 
  52. "Class Guid     : {0}" -f $Device.Classguid 
  53. "Description    : {0}" -f $Device.Description 
  54. "Device ID      : {0}" -f $Device.Deviceid 
  55. "Manufacturer   : {0}" -f $Device.Manufactuer 
  56.  
  57. "PNP Device Id  : {0}" -f $Device.PNPDeviceID 
  58. "Service Name   : {0}" -f $Device.Service 
  59. "" 
  60. } # End of ForEach 
  61. # End of Else 

2 comments:

IntrntPirate said...

Do you know how I would be able to have all the data for the bad devices sent out in an email? I know how to send an email with PowerShell, I just can't seem to get the information into the email.

Thomas Lee said...

Sure!

First, you'd need to adapt the script itself (to add or remove specific properties being displayed.

Next you would adapt the script for any networking - do you want to run the script centrally against remote systems, or do you want to remotely run teh script and have the invidual scripts run from one system and drag the data across the net. There are advantages and disavantages to both so you need to examine your options.

Finally, once you have the information, ou build an email message using the system.net.mail classes/methods. I have a couple on here - see http://pshscripts.blogspot.com/search/label/system.net.mail. I'm slowly getting around to writing a longer set of articles on mail, powerehell and the .NET related classes.