Showing posts with label wmi. Show all posts
Showing posts with label wmi. Show all posts

Tuesday, 11 October 2011

Get-OSInstallDate.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script gets/displays the Sysgtem Uptime after 
  4.     converting it from WEBM time/date format  
  5. .DESCRIPTION 
  6.     This script creates an instance of a SWbemDateTime object,  
  7.     gets the OS install date and converts the date to a more 
  8.     useful format. 
  9. .NOTES 
  10.     File Name  : Get-OSInstallDate.ps1 
  11.     Author     : Thomas Lee - tfl@psp.co.uk 
  12.     Requires   : PowerShell Version 2.0 
  13. .LINK 
  14.     This script posted to: 
  15.         http://www.pshscripts.blogspot.com 
  16.     MSDN sample posted to: 
  17.         http://msdn.microsoft.com/en-us/library/aa393687%28VS.85%29.aspx 
  18. .EXAMPLE 
  19.     Psh [C:\foo]> .\Get-OsInstallDate.ps1 
  20.     This OS was installed in the year 2008 
  21.     Full installation date (VT_DATE format) is 4/9/2008 8:17:23 PM 
  22.     Full installation date (FILETIME format) is 128522458430000000 
  23.  
  24. #> 
  25.  
  26. # Create swbemdatetime object 
  27. $datetime = New-Object -ComObject WbemScripting.SWbemDateTime 
  28.  
  29. #  Get OS installation time and assign to datetime object 
  30. $os = Get-WmiObject -Class Win32_OperatingSystem 
  31. $dateTime.Value = $os.InstallDate 
  32.  
  33. # Now display the time 
  34. "This OS was installed in the year {0}"           -f $dateTime.Year 
  35. "Full installation date (VT_DATE format) is {0}"  -f $dateTime.GetVarDate() 
  36. "Full installation date (FILETIME format) is {0}" -f $dateTime.GetFileTime()  
  37. ## 

Friday, 7 October 2011

Register-Event1.ps1


  1. <# 
  2. .SYNOPSIS 
  3.     This script registers for a WMI event 
  4. .DESCRIPTION 
  5.     This script used PowerShell to register for then display 
  6.     an event. This script is a re-write on an MSDN Sample. 
  7. .NOTES 
  8.     File Name  : Register-Event1.ps1 
  9.     Author     : Thomas Lee - tfl@psp.co.uk 
  10.     Requires   : PowerShell Version 2.0 
  11. .LINK 
  12.     This script posted to: 
  13.         http://www.pshscripts.blogspot.com 
  14.     MSDN sample posted tot: 
  15.         http://msdn.microsoft.com/en-us/library/aa393013%28VS.85%29.aspx 
  16. .EXAMPLE 
  17.     [PSH] C:\FOO> Register-Event1.ps1 
  18.     Waiting for events 
  19.     Log Event Occured 
  20.     EVENT MESSAGE 
  21.     A logon was attempted using explicit credentials. 
  22.  
  23.     Subject: 
  24.     Security ID:        S-1-5-21-2824006062-479960714-4144511058-1105 
  25.     Account Name:       tfl 
  26.     ...  -> Reminder snipped for brevity 
  27.     
  28. #> 
  29.  
  30. # Define event Query 
  31. $query = "SELECT * FROM __InstanceCreationEvent  
  32.           WHERE TargetInstance ISA 'Win32_NTLogEvent' " 
  33.  
  34. # Register for event - also specify an action that 
  35. # displays the log event when the event fires. 
  36. Register-WmiEvent -Source Demo1 -Query $query -Action { 
  37.                 Write-Host "Log Event occured" 
  38.                 $global:myevent = $event 
  39.                 Write-Host "EVENT MESSAGE" 
  40.                 Write-Host $event.SourceEventArgs.NewEvent.TargetInstance.Message} 
  41. # So wait 
  42. "Waiting for events" 
Technorati Tags: ,,,

Friday, 30 September 2011

Get-FolderObjects.ps1


  1. <# 
  2. .SYNOPSIS 
  3.     This script displays the WMI objects associated with a folder 
  4. .DESCRIPTION 
  5.     This script uses the Associators Of query 
  6. .NOTES 
  7.     File Name  : get-folderobjects.ps1 
  8.     Author     : Thomas Lee - tfl@psp.co.uk 
  9.     Requires   : PowerShell Version 2.0 
  10. .LINK 
  11.     This script posted to: 
  12.         http://www.pshscripts.blogspot.com 
  13. .EXAMPLE 
  14.     Get-FolderObjects "C:\Foo" 
  15.         what's in C:\foo 
  16.     0 Volume objects 
  17.     0 Logical Disk objects 
  18.     10 Directory objects 
  19.     171 File objects 
  20.     0 Pagefile objects 
  21.     1 Security Settings 
  22.     0 Share objects 
  23.     0 other objects 
  24. .EXAMPLE 
  25.     Get-FolderObjects "C:\" 
  26.       what's in C:\ 
  27.     1 Volume objects 
  28.     1 Logical Disk objects 
  29.     18 Directory objects 
  30.     8 File objects 
  31.     1 Pagefile objects 
  32.     1 Security Settings 
  33.     1 Share objects 
  34.     0 other objects   
  35. #> 
  36. # Function to get folder WMI object associations 
  37. # Define function 
  38. Function Get-FolderObjects { 
  39. Param ( 
  40. $Folder  =   "c:\" 
  41. ) 
  42.  
  43. # Set query then get associations 
  44. $query  = "ASSOCIATORS OF {Win32_Directory.Name='$folder'}" 
  45. $objs = Gwmi -q $query 
  46. #    
  47. # Now group them 
  48. # Create empty arrays 
  49. $directory = @() 
  50. $datafile  = @() 
  51. $pagefile  = @() 
  52. $volume    = @() 
  53. $filesec   = @() 
  54. $share     = @() 
  55. $Logdisk   = @() 
  56. $unknown   = @() 
  57.  
  58. # Now fill the arrays 
  59. foreach ($obj in $objs) { 
  60.     switch  ($Obj.__class) { 
  61.       "Win32_Directory"   {$directory += $obj; break} 
  62.       "Cim_DataFile"      {$datafile  += $obj; break} 
  63.       "Win32_PageFile"    {$pagefile  += $obj; break} 
  64.       "Win32_Volume"      {$volume    += $obj; break} 
  65.       "Win32_LogicalDisk" {$logdisk   += $obj; break}  
  66.       "Win32_LogicalFileSecuritySetting" {$filesec += $obj; break} 
  67.       "Win32_share"       {$share     += $obj; break} 
  68.       default             {$unknown   += $obj; break} 
  69.     } 
  70. } 
  71.  
  72. # Display the output 
  73. " what's in $folder" 
  74. "{0} Volume objects"        -f $volume.count 
  75. "{0} Logical Disk objects"  -f $logdisk.count 
  76. "{0} Directory objects"     -f $directory.count 
  77. "{0} File objects"          -f $datafile.count 
  78. "{0} Pagefile objects"      -f $pagefile.count 
  79. "{0} Security Settings"     -f $filesec.count 
  80. "{0} Share objects"         -f $share.count 
  81. "{0} other objects"         -f $unknown.count 
  82. } 
  83.  
  84. # Here call function as an example 
  85. Get-FolderObjects "C:\" 
  86. "***" 
  87. Get-FolderObjects "C:\foo" 

Tuesday, 13 September 2011

Remove-WmiRegistryKey.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script creates removes registry key using WMI. 
  4. .DESCRIPTION 
  5.     This script uses WMI to get remove registry key.  
  6.     This script deletes the key and everything below 
  7.     it in the registry - use carefully! 
  8. .NOTES 
  9.     File Name  : Remove-WmiRegistryKey.ps1 
  10.     Author     : Thomas Lee - tfl@psp.co.uk 
  11.     Requires   : PowerShell Version 2.0 
  12. .LINK 
  13.     This script posted to: 
  14.         http://www.pshscripts.blogspot.com 
  15.     MSDN sample posted to: 
  16.         http://msdn.microsoft.com/en-us/library/aa394600%28VS.85%29.aspx 
  17. .EXAMPLE 
  18.     Psh[C:\foo]>New-WMIRegistryKey.ps1 
  19.     Key removed 
  20. #> 
  21.  
  22. # Define Constants 
  23. $HKEY_Local_Machine =2147483650  
  24. $computer ='.' 
  25.  
  26. # Get Class to call static methods on 
  27. $reg = [WMIClass]"ROOT\DEFAULT:StdRegProv" 
  28.  
  29. # Define key to create 
  30. $Key     = "SOFTWARE\NewKey" 
  31.  
  32. # Create key and display reslts 
  33. $results   = $reg.DeleteKey($HKEY_LOCAL_MACHINE, $Key) 
  34. If ($results.Returnvalue -eq 0) {"Key Removed"}  

Monday, 12 September 2011

Get-WmiRegistryBinaryValue.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script Gets and displays a registry 
  4.     binary value using WMI. 
  5. .DESCRIPTION 
  6.     This script uses WMI to get, then display 
  7.     a binary registry Value. 
  8.     This is a re-write of a VB Sample Script. 
  9. .NOTES 
  10.     File Name  : Set-WmiRegistryBinaryValue.ps1 
  11.     Author     : Thomas Lee - tfl@psp.co.uk 
  12.     Requires   : PowerShell Version 2.0 
  13. .LINK 
  14.     This script posted to: 
  15.         http://www.pshscripts.blogspot.com 
  16.     MSDN sample posted to: 
  17.         http://msdn.microsoft.com/en-us/library/aa394600%28VS.85%29.aspx 
  18. .EXAMPLE 
  19.     Psh[C:\foo]>Get-WmiRegistryBinaryValue.ps1 
  20.     54 
  21.     46 
  22.     4c 
  23. #> 
  24.  
  25. # Define Constants 
  26. $HKEY_Local_Machine =2147483650  
  27. $computer ='.' 
  28.  
  29. # Get Class to call static methods on 
  30. $reg = [WMIClass]"ROOT\DEFAULT:StdRegProv" 
  31.  
  32. # Define key to create 
  33. $ValueName = "Example Binary Value" 
  34. $Values     = @(0x54, 0x46, 0x4C) 
  35. $Key       = "SOFTWARE\NewKey" 
  36.  
  37. # Create Value entry 
  38. $results   = $reg.GetBinaryValue($HKEY_LOCAL_MACHINE, $Key, $ValueName
  39. Foreach ($byte in $results.uvalue) {"{0}" -f $byte.tostring("x")} 

W

Sunday, 11 September 2011

Set-WmiRegistryBinaryValue

  1. <# 
  2. .SYNOPSIS 
  3.     This script sets a registry binary value 
  4.     using WMI. 
  5. .DESCRIPTION 
  6.     This script uses WMI to set a binary  
  7.     registry Value. 
  8.     This is a re-write of a VB Sample Script. 
  9. .NOTES 
  10.     File Name  : Set-WmiRegistryBinaryValue.ps1 
  11.     Author     : Thomas Lee - tfl@psp.co.uk 
  12.     Requires   : PowerShell Version 2.0 
  13. .LINK 
  14.     This script posted to: 
  15.         http://www.pshscripts.blogspot.com 
  16.     MSDN sample posted to: 
  17.         http://msdn.microsoft.com/en-us/library/aa394600%28VS.85%29.aspx 
  18. .EXAMPLE 
  19.     Psh[C:\foo]>New-WmiRegistryBinaryValue.ps1 
  20.     Value created 
  21. #> 
  22.  
  23. # Define Constants 
  24. $HKEY_Local_Machine =2147483650  
  25. $computer ='.' 
  26.  
  27. # Get Class to call static methods on 
  28. $reg = [WMIClass]"ROOT\DEFAULT:StdRegProv" 
  29.  
  30. # Define key to create 
  31. $ValueName = "Example Binary Value" 
  32. $Values     = @(0x54, 0x46, 0x4C) 
  33. $Key       = "SOFTWARE\NewKey" 
  34.  
  35. # Create Value entry 
  36. $results   = $reg.SetBinaryValue($HKEY_LOCAL_MACHINE, $Key, $ValueName, $Values
  37. If ($results.Returnvalue -eq 0) {"Value Set"}  

Saturday, 10 September 2011

New-WmiRegistryValue.ps1


  1. <# 
  2. .SYNOPSIS 
  3.     This script creates a new registry Value using WMI. 
  4. .DESCRIPTION 
  5.     This script uses WMI to get create a new registry Value. 
  6.     This is a re-write of a VB Sample Script 
  7. .NOTES 
  8.     File Name  : New-RegistryKey.ps1 
  9.     Author     : Thomas Lee - tfl@psp.co.uk 
  10.     Requires   : PowerShell Version 2.0 
  11. .LINK 
  12.     This script posted to: 
  13.         http://www.pshscripts.blogspot.com 
  14.     MSDN sample posted to: 
  15.         http://msdn.microsoft.com/en-us/library/aa394600%28VS.85%29.aspx 
  16. .EXAMPLE 
  17.     Psh[C:\foo]>New-WmiRegistryValue.ps1 
  18.     Value created 
  19. #> 
  20.  
  21. # Define Constants 
  22. $HKEY_Local_Machine =2147483650  
  23. $computer ='.' 
  24.  
  25. # Get Class to call static methods on 
  26. $reg = [WMIClass]"ROOT\DEFAULT:StdRegProv" 
  27.  
  28. # Define key to create 
  29. $ValueName = "Example_Expanded_String_Value" 
  30. $Value     = "%PATHEXT%" 
  31. $Key       = "SOFTWARE\NewKey" 
  32.  
  33. # Create Value entry 
  34. $results   = $reg.SetExpandedStringValue($HKEY_LOCAL_MACHINE, $Key, $ValueName, $Value
  35. If ($results.Returnvalue -eq 0) {"Value created"}  

Friday, 9 September 2011

New-WmiRegistryKey.ps1


  1. <# 
  2. .SYNOPSIS 
  3.     This script creates a new registry key using WMI. 
  4. .DESCRIPTION 
  5.     This script uses WMI to get create a new registry key. 
  6.     This is a re-write of a VB Sample Script 
  7. .NOTES 
  8.     File Name  : New-RegistryKey.ps1 
  9.     Author     : Thomas Lee - tfl@psp.co.uk 
  10.     Requires   : PowerShell Version 2.0 
  11. .LINK 
  12.     This script posted to: 
  13.         http://www.pshscripts.blogspot.com 
  14.     MSDN sample posted to: 
  15.         http://msdn.microsoft.com/en-us/library/aa394600%28VS.85%29.aspx 
  16. .EXAMPLE 
  17.     Psh[C:\foo]>New-WMIRegistryKey.ps1 
  18.     Key created 
  19. #> 
  20.  
  21. # Define Constants 
  22. $HKEY_Local_Machine =2147483650  
  23. $computer ='.' 
  24.  
  25. # Get Class to call static methods on 
  26. $reg = [WMIClass]"ROOT\DEFAULT:StdRegProv" 
  27.  
  28. # Define key to create 
  29. $Key     = "SOFTWARE\NewKey" 
  30.  
  31. # Create key and display reslts 
  32. $results   = $reg.CreateKey($HKEY_LOCAL_MACHINE, $Key
  33. If ($results.Returnvalue -eq 0) {"Key created"}  

Thursday, 8 September 2011

Get-WmiRegDword.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script gets a registry value using WMI. 
  4. .DESCRIPTION 
  5.     This script uses WMI to get then display a resistry value, using  
  6.     the SteRegProv class and the GetDWORDValue static method. 
  7.     This is a re-write of a VB Sample Script 
  8. .NOTES 
  9.     File Name  : Get-WMIRegDword.ps1 
  10.     Author     : Thomas Lee - tfl@psp.co.uk 
  11.     Requires   : PowerShell Version 2.0 
  12. .LINK 
  13.     This script posted to: 
  14.         http://www.pshscripts.blogspot.com 
  15.     MSDN sample posted to: 
  16.         http://msdn.microsoft.com/en-us/library/aa394600%28VS.85%29.aspx 
  17. .EXAMPLE 
  18.     Psh[C:\foo]>Get-WmiRegDword.ps1 
  19. Current History Buffer Size: 50 
  20.      
  21. #> 
  22.  
  23. # Define Constants 
  24. $HKEY_CURRENT_USER =2147483649   
  25. $computer ='.' 
  26.  
  27. # Get Class to call static methods on 
  28. $reg = [WMIClass]"ROOT\DEFAULT:StdRegProv" 
  29.  
  30. #defind key/value to get  
  31. $Key     = "Console" 
  32. $Value   = "HistoryBufferSize" 
  33.  
  34. # Get Value of buffer and display 
  35. $results   = $reg.GetDWORDValue($HKEY_CURRENT_USER, $Key, $value
  36. "Current History Buffer Size: {0}" -f $results.uValue 
Technorati Tags: ,,,

Tuesday, 4 January 2011

Get-ComputerDomain.ps1

<#
.SYNOPSIS
This script uses WMI to get the name of a computer's domain then displays the name.
.DESCRIPTION
This script is a re-write of an MSDN sample, using PowerShell.
.NOTES
File Name : Get-ComputerDomain.ps1
Author : Thomas Lee - tfl@psp.co.uk
Requires : PowerShell Version 2.0
.LINK
This script posted to:
http://www.pshscripts.blogspot.com
MSDN sample posted tot:
http://msdn.microsoft.com/en-us/library/aa394586%28VS.85%29.aspx/
.EXAMPLE
PSH [C:\foo]: .\Get-ComputerDomain.ps1
System Name: WIN7
Domain: cookham.net
.EXAMPLE
Psh[Cookham8]> .\Get-ComputerDomain.ps1 cookham2
System Name: COOKHAM2
Domain: cookham.net
.PARAMETER comp
The name of the computer whose domain name to be displayed. Default is localhost.
#>


# Parameter block
param (
$comp = "."
)

# Get WMI Object
$system = Get-WmiObject -class Win32_ComputerSystem -ComputerName $comp

# Display results
"System Name: {0}" -f $System.Name
"Domain: {0}" -f $System.Domain


Thursday, 7 October 2010

Get-WMINameSpace.ps1


  1. <# 
  2. .SYNOPSIS 
  3.     This script displays all the WMI namespaces within a Windows system 
  4. .DESCRIPTION 
  5.     This script uses Get-WMIObject to retrieve the names of all the namespaces 
  6.     within a system. 
  7. .NOTES 
  8.     File Name  : Get-WMINameSpace.ps1 
  9.     Author     : Thomas Lee - tfl@psp.co.uk 
  10.     Requires   : PowerShell Version 2.0 
  11. .LINK 
  12.     This script posted to:
  13.         http://pshscripts.blogspot.com/2010/10/get-wminamespaceps1.html
  14. .EXAMPLE 
  15.     PSH [C:\foo]: .\Get-WMINameSpace.ps1 
  16.     37 Namespaces on: Cookham8 
  17.  
  18.     Namespace 
  19.     --------- 
  20.     ROOT 
  21.     ROOT\aspnet 
  22.     ROOT\CIMV2 
  23.     ROOT\CIMV2\Security 
  24.     ROOT\CIMV2\Security\MicrosoftTpm 
  25.     ... {Remainder of list snipped to save space on this page} 
  26. #> 
  27.  
  28. # Set computer name
  29. $comp = "." 
  30. # Get the name spaces on the local computer, and the local computer name 
  31. $Namespace = get-wmiobject __namespace -namespace 'root' -list -recurse -computer $comp  
  32. $hostname = hostname 
  33.  
  34. # Display number of and names of the namespaces 
  35. "{0} Namespaces on: {1}" -f $namespace.count, $hostname 
  36. $NameSpace| sort __namespace  | Format-Table @{Expression = "__Namespace"; Label = "Namespace"
Technorati Tags: ,,,

Tuesday, 17 August 2010

Get-BrokenHardware.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script gets a list of non-working hardware using WMI. 
  4. .DESCRIPTION 
  5.     This script re-implements another TechNet Scripting 
  6.     Gallery script that was written in VB (see  
  7.     http://tinyurl.com/y4hmtbr).  
  8.     This script first uses WMI to get system details, then 
  9.     gets and displays hardware that has errored. 
  10. .NOTES 
  11.     File Name  : Get-BrokenHardware.ps1 
  12.     Author     : Thomas Lee - tfl@psp.co.uk 
  13.     Requires   : PowerShell Version 2.0 
  14. .LINK 
  15.     This script posted to: 
  16.         http://www.pshscripts.blogspot.com 
  17.     This script posted to TechNet Script Gallery at: 
  18.         http://gallery.technet.microsoft.com/ScriptCenter/en-us/dbb678f4-b95b-45c3-bc8b-2ae2d052448e     
  19. .EXAMPLE 
  20.     PSH [C:\foo]: Get-BrokenHardware.ps1 
  21.     Computer Details: 
  22.     Manufacturer: Dell Inc. 
  23.     Model:        Precision WorkStation T7400 
  24.     Service Tag:  6Y84C3J 
  25.  
  26.     Hardware that's not working list 
  27.     Description:  WD My Book Device USB Device 
  28.     Device ID:    USBSTOR\OTHER&VEN_WD&PROD_MY_BOOK_DEVICE&REV_1010\7&2A4E07C&0&575532513130303732383932&1 
  29.     Error ID:     28 
  30. #> 
  31.  
  32. # Display Computer details 
  33. "Computer Details:" 
  34. $comp = gwmi Win32_ComputerSystem 
  35. "Manufacturer: {0}" -f $comp.Manufacturer 
  36. "Model:        {0}" -f $comp.Model 
  37. $computer2 = Get-WmiObject Win32_ComputerSystemProduct 
  38. "Service Tag:  {0}" -f $computer2.IdentifyingNumber 
  39. "" 
  40.  
  41. #Get hardware that is errored 
  42. "Hardware that's not working list"  
  43. $broken = Get-WmiObject Win32_PnPEntity | where {$_.ConfigManagerErrorCode -ne 0} 
  44.  
  45. #Display broken hardware 
  46. foreach ($obj in $broken){   
  47. "Description:  {0}" -f  $obj.Description 
  48. "Device ID:    {0}" -f  $obj.DeviceID 
  49. "Error ID:     {0}" -f  $obj.ConfigManagerErrorCode 
  50. "" 

Thursday, 12 August 2010

Get-DnsRegisteredServers.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script gets the reverse lookup zone from a DNS Server and 
  4.     displays all the systems registered 
  5. .DESCRIPTION 
  6.     This script first gets the reverse lookup zone from a DNS Server (i.e. 
  7.     all the computers that have used the DNS server to register!). The 
  8.     script then displays the FQDN, IP Address and Timestamp. 
  9. .NOTES 
  10.     File Name  : Get-DnsRegisteredServers.ps1 
  11.     Author     : Thomas Lee - tfl@psp.co.uk 
  12.     Requires   : PowerShell Version 2.0 
  13. .LINK 
  14.     This script posted to: 
  15.         http://www.pshscripts.blogspot.com 
  16.     This script posted to TechNet Scripting Gallery 
  17.         http://gallery.technet.microsoft.com/ScriptCenter/en-us/28e8b98e-565b-40be-ba2c-1134341bb555     
  18. .EXAMPLE 
  19.     PSH [C:\foo]: .Get-DnsRegisteredNames.ps1 
  20.     Computers reverse registered on DNS Server: Cookham1 
  21.     10.10.1.42      superman.cookham.net.                     3590550 
  22.     10.10.1.105     batman.cookham.net.                       3582112 
  23.     10.10.1.109     jeeves.cookham.net.                       3586452 
  24.     10.10.1.111     supergirl.cookham.net.                    3590550 
  25.     10.10.1.114     future.cookham.net.                       3589209 
  26.     10.10.1.131     bladerunner.cookham.net.                  3587817 
  27.     10.10.1.142     wonderwoman.kapoho.net.                   3590551 
  28. #> 
  29.  
  30. $dns = "Cookham1"  
  31. $records = get-wmiobject -class MicrosoftDNS_PTRType -namespace root\MicrosoftDNS -computer $dns  
  32.  
  33. "Computers reverse registered on DNS Server: $DNS" 
  34.  
  35. # Loop through and display results 
  36. foreach ($record in $records) { 
  37.  
  38. # Get owner name and ip address string 
  39. $on = $record.ownerName.split("."
  40. $ownerip = $on[3] + "." + $on[2] + "." + $on[1] + "." + $on[0] 
  41.  
  42. # Display details 
  43. "{0, -15} {1,-40}  {2,-10} " -f $ownerip, $record.ptrdomainname, $record.timestamp 

Saturday, 31 July 2010

Get-WmiClassDescription.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script gets and displays the description of a WMI Class.  
  4. .DESCRIPTION 
  5.     This script takes a WMI Class name as a parameter. The script 
  6.     then gets the class's description from the CIM repository and 
  7.     displays it. Based on a PowerShell.Com tip of the day! 
  8. .NOTES 
  9.     File Name  : Get-WmiClassDescription.ps1 
  10.     Author     : Thomas Lee - tfl@psp.co.uk 
  11.     Requires   : PowerShell Version 2.0 
  12. .LINK 
  13.     This script posted to: 
  14.         http://www.pshscripts.blogspot.com  
  15. .EXAMPLE 
  16.     PSH [C:\foo]: .\Get-WmiClassDescription.ps1 
  17.     Description for WMI Class Win32_ComputerSystem: 
  18.     The Win32_ComputerSystem class represents a computer system operating in a Win32 environment. 
  19. .EXAMPLE 
  20.     PSH [C:\foo]: . \Get-WmiClassDescription.ps1 win32_bios 
  21.     Description for WMI Class win32_bios: 
  22.     The Win32_BIOS class represents the attributes of the computer system's basic input/output services 
  23.     (BIOS) that are installed on the computer. 
  24. #> 
  25.  
  26. param
  27. [string] $WMIClassName = "Win32_ComputerSystem" 
  28.  
  29. # Get WMI class from class name 
  30. $class = [wmiclass]$wmiclassname 
  31.  
  32. # Now get then print the class description 
  33. # First use ammended qualifiers to get description 
  34. $class.psbase.Options.UseAmendedQualifiers = $true 
  35. "Description for WMI Class {0}:"  -f $WmiClassName 
  36. ($class.psbase.qualifiers["description"]).Value 

Thursday, 29 October 2009

Restart-DNS.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script restarts the DNS Service on a Remote System 
  4. .DESCRIPTION 
  5.     This script uses WMI to reach out and restart the DNS 
  6.     DNS service on a remote machine. in my home environment, 
  7.     I find the DNS service on the home DC fails and needs  
  8.     to be restarted - this script works! 
  9. .NOTES 
  10.     File Name  : Restart-DNS.ps1 
  11.     Author     : Thomas Lee - tfl@psp.co.uk 
  12.     Requires   : PowerShell V2 
  13. .LINK 
  14.     This script posted to: 
  15.         http://pshscripts.blogspot.com/2009/10/restart-dnsps1.html
  16. .EXAMPLE 
  17.     PSH [C:\foo]: .\Restart-DNS.ps1 
  18.     Restarting DNS Service on: Cookham1 
  19.     DNS Service stopped 
  20.     DNS Service started 
  21. #> 
  22. ## 
  23. # Start Script 
  24. ## 
  25.  
  26. # Set server to reset and display our intention 
  27. $Server = "Cookham1" 
  28. "Restarting DNS Service on: $Server" 
  29.   
  30. # Get DNS service 
  31. $dns = gwmi win32_service -computer $Server  | where {$_.name -eq "DNS"
  32.   
  33. # Now stop it 
  34. $ret = $dns.stopservice() 
  35. if ($ret.returnvalue -eq 0) {"DNS Service stopped"
  36. else {"DNS Service not stopped"
  37.   
  38. # And now restart it 
  39. $ret = $dns.startservice() 
  40. if ($ret.returnvalue -eq 0) {"DNS Service started"
  41. else {"DNS Service not started"
  42.   
  43. # End of Script 
Technorati Tags: ,,,,

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: ,,