Saturday 31 January 2009

Count-GDShows.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     Counts the Grateful Dead shows in my archives 
  4. .DESCRIPTION 
  5.     This script looks at my GD archive, and uses folder names to 
  6.     determine show type (aud, sbd, etc), and checks to see what 
  7.     shows have checked MD5s 
  8. .NOTES 
  9.     File Name  : count-gdshows.ps1 
  10.     Author     : Thomas Lee - tfl@psp.co.uk 
  11.     Requires   : PowerShell V2 CTP3 
  12. .LINK 
  13.     http://pshscripts.blogspot.com/2009/01/count-gdshowsps1.html 
  14. .EXAMPLE 
  15.     PS C:\foo> count-gdshows.ps1 
  16.     Count.ps1 - v 2.0.1 
  17.     +---------------------------+ 
  18.     ! Dead Show Base  :  M:\gd  ! 
  19.     +---------------------------+ 
  20.  
  21.     Grateful Dead Show Summary 
  22.     -------------------------- 
  23.     Total shows:   1146 
  24.     Soundboards:   800 
  25.     Auds       :   70 
  26.     Unknown    :   29 
  27.     Partial    :   9 
  28.     Broken     :   4 
  29.     MD5's check:   489 (42.67 %) 
  30. #> 
  31. ### 
  32. # Start of Archive 
  33. ## 
  34.  
  35. # Constants: 
  36. # $GDDiskRoot    - where to find shows 
  37. # $DeadShowBase  - folder at top of gd shows 
  38.  
  39. $GDDiskRoot     = "M:" 
  40. $DeadShowBase   = $GDDiskRoot + "\gd" 
  41.  
  42. # Announce Ourselves 
  43. "Count.ps1 - v 2.0.1" 
  44. "+---------------------------+" 
  45. "! Dead Show Base  :  $DeadShowBase  !" 
  46. "+---------------------------+" 
  47. "" 
  48.  
  49. # Count the Dead shows 
  50.  
  51. $Dir= ls $DeadShowBase  | where {$_.psiscontainer} 
  52. $DeadShows=$Dir.count 
  53. if ($DeadSHows -le 0) {"no shows found - check constants"} 
  54.  
  55. #Create subsets based on names of the folders 
  56. $deadsbds=$dir | where {$_.name -match ".sbd" } 
  57. $deadbrkn=$dir | where {$_.name -match "broken" } 
  58. $deadpart=$dir | where {$_.name -match "partial" } 
  59. $deadauds=$dir | where {$_.name -match ".aud" } 
  60. $deadunkw=$dir | where {$_.name -Match ".unk"} 
  61.  
  62. #and see how many have the md5ok's file? 
  63.  
  64. $DeadMD5Checked=0 
  65. foreach ($d in $dir) 
  66. {  
  67.   $sn=$d.fullname + "\md5check_ok" 
  68.   $md5ok= ls $sn -ea silentlycontinue 
  69.   if ($md5ok )  
  70.      {$DeadMD5Checked++} 
  71.  
  72. #Display results 
  73.  
  74. "Grateful Dead Show Summary" 
  75. "--------------------------" 
  76. "Total shows:   $deadshows" 
  77. "Soundboards:   $($deadsbds.count)" 
  78. "Auds       :   $($deadauds.count)" 
  79. "Unknown    :   $($deadunkw.count)" 
  80. "Partial    :   $($deadpart.count)" 
  81. "Broken     :   $($deadbrkn.count)" 
  82. $DeadPctChecked=($DeadMD5checked/$DeadShows).tostring("P"
  83. "MD5's check:   $DeadMD5checked ($DeadPctChecked)" 
  84. "" 

Thursday 29 January 2009

Get-IPAddress.ps1


  1. <# 
  2. .SYNOPSIS 
  3.     Gets and displays the IP address of a computer 
  4. .DESCRIPTION 
  5.     This script uses Win32_NetworkAdapterConfiguration to 
  6.     obtain, then display, a system's IP addresses. 
  7.     NB: this only works on XP or later versions of Windows. 
  8. .NOTES 
  9.     File Name : Get-IPAddress.ps1 
  10.     Author : Thomas Lee - tfl@psp.co.uk 
  11.     Requires : PowerShell V2 
  12. .LINK 
  13.     Script postesd to: 
  14.     http://www.pshscripts.blogspot.com 
  15.     MSDN Sample at: 
  16.     http://msdn.microsoft.com/en-us/library/aa394590(VS.85).aspx 
  17. .EXAMPLE 
  18.     [ps] c:\foo> .\Get-IPAddress.ps1 
  19.     IP Address   : 10.10.1.115 
  20.     IP Address   : fe80::3953:f67b:2f1c:1323 
  21.     IP Address   : 10.10.1.120 
  22.     IP Address   : fe80::d8ed:afe2:2a97:a596
  23.     4 IP addresses found on this system
  24. #> 
  25.    
  26. ## 
  27. # Start of Script 
  28. ## 
  29.    
  30. # Get Networking Adapter Configuration 
  31. $Computer = "." 
  32. $IPconfigset = Get-WmiObject Win32_NetworkAdapterConfiguration 
  33.    
  34. # Iterate and get IP address 
  35. $count = 0 
  36. foreach ($IPConfig in $IPConfigSet) { 
  37.    if ($Ipconfig.IPaddress) { 
  38.       foreach ($addr in $Ipconfig.Ipaddress) { 
  39.       "IP Address   : {0}" -f  $addr
  40.       $count++  
  41.       } 
  42.    } 
  43. if ($count -eq 0) {"No IP addresses found"
  44. else {"$Count IP addresses found on this system"
  45. #End of Script 

Wednesday 28 January 2009

Get-TimeZoneInfo.ps1

  1. <#
  2. .SYNOPSIS
  3. Gets and displays time zone information via WMI
  4. .DESCRIPTION
  5. This script uses the Win32_TimeZone class to obtain, then
  6. display time zone information for a computer.
  7. This script is a MSDN sample recoded in PowerShell
  8. .NOTES
  9. File Name : Get-TimeZoneInfo.ps1
  10. Author : Thomas Lee - tfl@psp.co.uk
  11. Requires : PowerShell V2 CTP3
  12. .LINK
  13. Script posted to:
  14. http://www.pshscripts.blogspot.com/2009/01/get-timezoneinfops1.html
  15. MSDN Sample at:
  16. http://msdn.microsoft.com/en-us/library/aa394590(VS.85).aspx
  17. .EXAMPLE
  18. [ps] c:\foo> .\Get-TimeZoneInfo.ps1
  19. Time zone information on computer "Win7"
  20. Time Zone Description : (UTC) Dublin, Edinburgh, Lisbon, London
  21. Daylight Name : GMT Daylight Time
  22. Standard Name : GMT Standard Time
  23. #>

  24. ###
  25. # Start of Script
  26. ##

  27. # Get Time Zone on a computer
  28. $Computer = "."
  29. $timezone = Get-WMIObject -class Win32_TimeZone -ComputerName $computer

  30. # Display details
  31. if ($computer -eq ".") {$computer = Hostname}
  32. "Time zone information on computer `"{0}`"" -f $computer
  33. "Time Zone Description : {0}" -f $timezone.Description
  34. "Daylight Name : {0}" -f $timezone.DaylightName
  35. "Standard Name : {0}" -f $timezone.StandardName
  36. #End of Script

Monday 26 January 2009

Get-WMILocalTime.ps1

  1. <# 
  2. .SYNOPSIS 
  3.   Gets time from WMI and displays it   
  4. .DESCRIPTION 
  5.    This script gets Win32_LocalTime and then displays  
  6.    the details. 
  7. .NOTES 
  8.     File Name  : Get-WMILocalTime.ps1 
  9.     Author     : Thomas Lee - tfl@psp.co.uk 
  10.     Requires   : PowerShell V2 CTP3 
  11. .LINK 
  12.     Script posted to:
  13.     http://pshscripts.blogspot.com/2009/01/get-wmilocaltimeps1.html
  14.     MSDN Sample at:
  15.     http://msdn.microsoft.com/en-us/library/aa394590(VS.85).aspx
  16. .EXAMPLE 
  17.     [ps] c:\foo> .\Get-WMILocaltimeps1 
  18.     Day          : 26 
  19.     Day Of Week  : 1 
  20.     Hour         : 20 
  21.     Minute       : 59 
  22.     Month        : 1 
  23.     Quarter      : 1 
  24.     Second       : 53 
  25.     Week In Month: 5 
  26.     Year         : 2009 
  27. #> 
  28.  
  29. ### 
  30. # Start of Script 
  31. ## 
  32.  
  33. # Speficy computer and get Local Time 
  34. $Computer = "." 
  35. $times = Get-WmiObject Win32_LocalTime -computer $computer 
  36.  
  37. # Now display the result 
  38.  
  39. Foreach ($time in $times) { 
  40. "Day          : {0}"  -f $Time.Day 
  41. "Day Of Week  : {0}"  -f $Time.DayOfWeek 
  42. "Hour         : {0}"  -f $Time.Hour 
  43. "Minute       : {0}"  -f $Time.Minute 
  44. "Month        : {0}"  -f $Time.Month 
  45. "Quarter      : {0}"  -f $Time.Quarter 
  46. "Second       : {0}"  -f $time.Second  
  47. "Week In Month: {0}"  -f $Time.WeekInMonth  
  48. "Year         : {0}"  -f $Time.Year  
  49. # End of Script 

Sunday 25 January 2009

Show-TryCatchFinally2.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     MSDN Sample of Try/Catch/Finally in PowerShell 
  4. .DESCRIPTION 
  5.     This script opens a file gets 1st 10 characters. The idea 
  6.     is to show more detail on try/catch/finally with PowerShell 
  7. .NOTES 
  8.     File Name  : show-trycatchfinally2.ps1 
  9.     Author     : Thomas Lee - tfl@psp.co.uk 
  10.     Requires   : PowerShell V2 CTP3 
  11.                  Also needs c:\foo\gd.txt to exist. 
  12. .LINK 
  13.     Script posted at:
  14.     http://pshscripts.blogspot.com/2009/01/show-trycatchfinally2ps1.html
  15.     MSDN Sample
  16.     http://msdn.microsoft.com/en-us/library/dszsf989.aspx
  17. .EXAMPLE 
  18.     PSH [C:\foo]: .Show-TryCatchFinally2.PS1' 
  19.     First 10 characters: 
  20.     N 
  21.     a 
  22.     m 
  23.     e 
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.     -  
  31.     - 
  32.     - 
  33. #> 
  34.  
  35. ### 
  36. # Start of Script 
  37. ## 
  38.  
  39. # Setup and open a file 
  40. $path = "c:\foo\gd.txt" 
  41. $file = new-object System.IO.StreamReader $path 
  42. $buffer = new-object char[] 10 
  43.  
  44. # now try and read the file 
  45. try { 
  46.   [void] $file.ReadBlock($buffer, $index, $buffer.Length) 
  47.   "First {0} characters:" -f $buffer.length 
  48.   $buffer 
  49. # catch IO exceptions 
  50. catch [System.IO.IOException] { 
  51. "Error reading from {0}. Message = {1}" -f $path, $Error[0].Message 
  52. #cleanup 
  53. finally { 
  54.  if ($file) { 
  55.   $file.Close() 

Friday 23 January 2009

Get-ErrorDetails.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     Shows detals of an $Error record for an exception 
  4. .DESCRIPTION 
  5.     Creates an exception then shows how to find out details.     
  6. .NOTES 
  7.     File Name  : Get-ErrorDetails.ps1 
  8.     Author     : Thomas Lee - tfl@psp.co.uk 
  9.     Requires   : PowerShell V2 CTP3 
  10. .LINK 
  11.     http://pshscripts.blogspot.com/2009/01/get-errordetailsps1.html
  12. .EXAMPLE 
  13.     PS C:\foo> .\Get-Errordetails.ps1' 
  14.     Output - left as an exercise for the reader 
  15. #> 
  16.  
  17. ### 
  18. # Start of Script 
  19. ### 
  20.  
  21. # Create divide by zero error 
  22.  
  23. try { 
  24. $zero = 0 
  25. 1/$zero   # will throw an exception 
  26. catch [System.DivideByZeroException] { 
  27. "Exception caught in catch block" 
  28. $err = $Error[0] 
  29.  
  30. # display $err contents ($error[0]) 
  31. "Contents of Error[0]" 
  32. $Err | fl * -Force 
  33. "" 
  34.  
  35. # now - show exception 
  36. "Exception details:" 
  37. $err.Exception | fl * -Force 
  38. "" 
  39.  
  40. # To get exception try: 
  41. "Exception:" 
  42. $err.Exception.InnerException | fl * -Force 
  43. "" 
  44.  
  45. # To get actual exception in this case: 
  46. "Specific exception name:" 
  47. $err.Exception.InnerException.tostring().split(":")[0] 
  48. catch { 
  49. "Some other error occurred" 
  50. $Error[0] 
  51. # End of script 
Technorati Tags: ,,

Thursday 22 January 2009

Throw-Exception.ps1

  1. <# 
  2. .SYNOPSIS 
  3.    Shows throwing an exception in a function, caught in caller.   
  4. .DESCRIPTION 
  5.     This is a re-written MSDN Sample 
  6. .NOTES 
  7.     File Name  : throw-exception.ps1 
  8.     Author     : Thomas Lee - tfl@psp.co.uk 
  9.     Requires   : PowerShell V2 CTP3 
  10. .LINK 
  11.     Original script at: 
  12.     http://pshscripts.blogspot.com/2009/01/throw-exceptionps1.html
  13.     MSDN Sample at: 
  14.     http://msdn.microsoft.com/en-us/library/1ah5wsex.aspx  
  15. .EXAMPLE 
  16.     PS C:\foo> .\Throw-Exception.ps1 
  17.     Num[0] - Result = 300 
  18.     Trying to get Num[4] 
  19.     In catch block 
  20.     Error caught: System.IndexOutOfRangeException 
  21. #> 
  22.  
  23. ### 
  24. #  Start of script 
  25. ### 
  26.  
  27.  
  28. # Helper function 
  29. function GetNumber{ 
  30.  param ($index
  31.  $nums = 300, 600, 900 
  32.  if ($index -gt $nums.Length) { 
  33. Throw [system.IndexOutOfRangeException] 
  34.  else
  35.  $nums[$index
  36.  
  37. # Start of main script 
  38.  
  39. # Get one that works... 
  40. $result = GetNumber 0 
  41. "Num[0] - Result = {0}" -f $result 
  42.  
  43. # Now try and catch the following 
  44. try { 
  45. "Trying to get Num[4]" 
  46. $result = GetNumber 4 
  47. Catch { 
  48. "In catch block" 
  49. "Error caught: {0}" -f $Error[0] 

Wednesday 21 January 2009

Get-TryCatchFinally.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     Shows Try/Catch/Finally using Powershell 
  4. .DESCRIPTION 
  5.     This is an MSDN Sample, re-written in PowerShell 
  6. .NOTES 
  7.     File Name  : get-trycatchfinally.ps1 
  8.     Author     : Thomas Lee - tfl@psp.co.uk 
  9.     Requires   : PowerShell V2 CTP3 
  10. .LINK 
  11.     Original script posted to: 
  12.     http://pshscripts.blogspot.com/2009/01/get-trycatchfinallyps1.html
  13.     MSDN Sample at: 
  14.     http://msdn.microsoft.com/en-us/library/zwc8s4fz.aspx 
  15. .EXAMPLE 
  16.     PS C:\foo> .\Get-TryCatchFinally.ps1 
  17.     Error in conversion 
  18.     Error record: Cannot convert value "Some string" to type "System.Int32". Error: "Input string was not in a correct format." 
  19.     $i = 123 
  20.     $i = System.Int32 
  21. #> 
  22.  
  23. ### 
  24. # Start of script 
  25. ### 
  26.  
  27. # Create some explicitly typed values 
  28. [int]    $i = 123 
  29. [string] $s = "Some string" 
  30. [object] $o = $s 
  31.  
  32. # Now try to convert an object into an integer (which will fail) 
  33.  
  34. try { 
  35. # Invalid conversion; $o contains a string not an int 
  36. $i = [int] $o 
  37.  
  38. # catch the error and display 
  39. catch { 
  40. "Error in conversion" 
  41. "Error record: {0}" -f $Error[0] 
  42. # Clean up 
  43. finally { 
  44. "`$i = {0}" -f $i 
  45. "`$i = {0}" -f $i.gettype() 
  46. # End of Script 

Tuesday 20 January 2009

100 Scripts So Far

I’ve been posting PowerShell scripts for just over 6 months. Traffic to this blog has been growing slowly – and for a niche blog rather nicely.

Yesterday, I hit 100 scripts in the archive. You can download these scripts from here. Enjoy!

Technorati Tags:

Monday 19 January 2009

Get-LeapYear.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     Demonstrates use of System.Datetime to determine leap year  
  4. .DESCRIPTION 
  5.     This script creates four date objects and checks to see if 
  6.     the date is a leap year. The last object is today's date.  
  7. .NOTES 
  8.     File Name  : Get-LeapYear.ps1 
  9.     Author     : Thomas Lee - tfl@psp.co.uk 
  10.     Requires   : PowerShell V2 CTP3 
  11. .LINK 
  12.     http://www.pshscripts.blogspot.com 
  13. .EXAMPLE 
  14.     PS c:\foo> .\Get-LeapYear.ps1 
  15.     2000 is a leap year: True 
  16.     2002 is a leap year: False 
  17.     2004 is a leap year: True 
  18.     2009 is a leap year: False 
  19. #> 
  20.  
  21. ## 
  22. #  Start of script 
  23. ## 
  24.  
  25. # Create three specific date objects, plus today 
  26. $d1 = [system.datetime]  "Jan 1 2000" 
  27. $d2 = [System.datetime]  "Jan 1 2002" 
  28. $d3 = [System.datetime]  "Jan 1 2004" 
  29. $d4 = get-date 
  30.  
  31. # Are they leap years?
  32. "{0} is a leap year: {1}" -f $d1.year,([system.datetime]::isleapyear($d1.year)) 
  33. "{0} is a leap year: {1}" -f $d2.year,([system.datetime]::isleapyear($d2.year)) 
  34. "{0} is a leap year: {1}" -f $d3.year,([system.datetime]::isleapyear($d3.year)) 
  35. "{0} is a leap year: {1}" -f $d4.year,([system.datetime]::isleapyear($d4.year)) 
  36. # End Script 

Sunday 18 January 2009

Get-CountOfJerryShows.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     Counts Jerry Garcia live shows on q: drive   
  4. .DESCRIPTION 
  5.     I store Jerry Garcia live shows on my Q: drive. This 
  6.     script counts the shows by year, and gives a total. 
  7. .NOTES 
  8.     File Name  : get-countofjerryshows.ps1 
  9.     Author     : Thomas Lee - tfl@psp.co.uk 
  10.     Requires   : PowerShell V2 CTP3 
  11. .LINK 
  12.     http://www.pshscripts.blogspot.com 
  13. .EXAMPLE 
  14.     PS C:\foo> .\get-countofjerryshows 
  15.     in: jg_1972_project,    9 shows 
  16.     in: jg_1973_project,   39 shows 
  17.     in: jg_1974_project,   40 shows 
  18.     in: jg_1975_project,   34 shows 
  19.     in: jg_1976_project,   52 shows 
  20.     in: jg_1977_project,   53 shows 
  21.     in: jg_1978_project,   27 shows 
  22.     in: jg_1979_project,   31 shows 
  23.     in: jg_1980_project,   56 shows 
  24.     in: jg_1982_project,   84 shows 
  25.     in: jg_1983_project,    3 shows 
  26.     in: jg_1984_project,   76 shows 
  27.     in: jg_1985_project,   22 shows 
  28.     in: jg_1986_project,   25 shows 
  29.     in: jg_1989_project,   32 shows 
  30.     in: jg_1990_project,    1 shows 
  31.     in: jg_1991_project,   64 shows 
  32.     in: jg_1992_project,   26 shows 
  33.     in: jg_1993_project,    5 shows 
  34.     in: jg_1994_project,   36 shows 
  35.     in: jg_1995_project,   19 shows 
  36.     734 shows in total 
  37. #> 
  38.  
  39. ## 
  40. # Start of Script 
  41. ## 
  42.  
  43. # Get starting point and move there 
  44.  
  45. $jerry = "q:\Jerry Garcia" 
  46. cd $jerry 
  47.  
  48. # Get sub-dirs that contain shows 
  49. $dirs = ls | where {$_.Name -match "jg_"
  50.  
  51. # Now iterate and count 
  52. $total = 0 
  53. foreach ($dir in $dirs) { 
  54.   $shows = ls $dir.FullName 
  55.   if ($shows.count) {$count=$shows.count} else {$count = 1} 
  56.   "In: {0,-15}, {1,4} shows" -f $dir.Name, $count 
  57.   $total += $count 
  58. "{0} shows in total" -f $total 

Send-BCCMail.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     Send mail to BCC using PowerShell 
  4. .DESCRIPTION 
  5.     This script is a re-developed MSDN Sample using PowerShell. It creates 
  6.     an email message then sends it with a BCC. 
  7. .NOTES 
  8.     File Name  : Send-BCCMail.ps1 
  9.     Author     : Thomas Lee - tfl@psp.co.uk 
  10.     Requires   : PowerShell V2 CTP3 
  11. .LINK 
  12.     Original Sample Posted to 
  13.     hthttp://pshscripts.blogspot.com/2009/01/send-bccmailps1.html 
  14.     MSDN Sample and details at: 
  15.     http://msdn.microsoft.com/en-us/library/system.net.mail.mailaddresscollection.aspx 
  16. .EXAMPLE 
  17.     PS C:\foo> .\Send-BCCMail.ps1 
  18.     Sending an e-mail message to The PowerShell Doctor and "Thomas Lee" <tfl@reskit.net> 
  19. #> 
  20.  
  21. ### 
  22. # Start Script 
  23. ### 
  24.  
  25. # Create from, to, bcc and the message strucures 
  26. $From    = New-Object system.net.Mail.MailAddress "tfl@cookham.net", "Thomas Lee" 
  27. $To      = new-object system.net.mail.Mailaddress "doctordns@gmail.com", "The PowerShell Doctor" 
  28. $Bcc     = New-Object system.Net.Mail.Mailaddress "tfl@reskit.net", "Thomas Lee" 
  29. $Message = New-Object system.Net.Mail.MailMessage $From, $To 
  30.  
  31. # Populate message 
  32. $Message.Subject = "Using the SmtpClient class and PowerShell." 
  33. $Message.Body    = "Using this feature, you can send an e-mail message from an" 
  34. $Message.Body   += "application very easily. `nEven better, you do it with PowerShell!" 
  35.  
  36. # Add BCC 
  37. $Message.Bcc.Add($bcc); 
  38.  
  39. # Create SMTP Client 
  40. $Server = "localhost" 
  41. $Client = New-Object system.Net.Mail.SmtpClient $server 
  42. $Client.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials 
  43. "Sending an e-mail message to {0} and {1}" -f $to.DisplayName, $Message.Bcc.ToString() 
  44.  
  45. # send the message 
  46. try { 
  47.     $Client.Send($message); 
  48. }   
  49. catch { 
  50. "Exception caught in Send-BCCMail.ps1: {0}" -f $Error[0] 
  51.        

Tuesday 13 January 2009

Get-UpTime.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     Demonstrates uptime using WMI  
  4. .DESCRIPTION 
  5.     This script used Win32_ComputerSystem to determine how long your system 
  6.     has been running. This is a rewrite/improvement of sample 3 at 
  7.     http://msdn.microsoft.com/en-us/library/aa394591(VS.85).aspx.  
  8. .NOTES 
  9.     File Name : Get-UpTime.ps1 
  10.     Author : Thomas Lee - tfl@psp.co.uk 
  11.     Requires : PowerShell V2 CTP3 
  12. .LINK 
  13.     Script Posted to:  
  14.     http://www.pshscripts.blogspot.com 
  15.     Original sample posted at: 
  16.     http://msdn.microsoft.com/en-us/library/aa394591(VS.85).aspx 
  17. .EXAMPLE 
  18.     PS c:\foo> .\Get-UpTime.Ps1 
  19.     System Up for: 1 days, 8 hours, 40.781 minutes 
  20.  
  21. #> 
  22.  
  23. ## 
  24. #  Start of script 
  25. ## 
  26.  
  27. # Helper Function - convert WMI date to TimeDate object 
  28. function WMIDateStringToDate($Bootup) { 
  29.     [System.Management.ManagementDateTimeconverter]::ToDateTime($Bootup
  30.  
  31. # Main script 
  32. $Computer = "." # adjust as needed 
  33. $computers = Get-WMIObject -class Win32_OperatingSystem -computer $computer 
  34.  
  35. foreach ($system in $computers) { 
  36.     $Bootup = $system.LastBootUpTime 
  37.     $LastBootUpTime = WMIDateStringToDate($Bootup
  38.     $now = Get-Date 
  39.     $Uptime = $now - $lastBootUpTime 
  40.     $d = $Uptime.Days 
  41.     $h = $Uptime.Hours 
  42.     $m = $uptime.Minutes 
  43.     $ms= $uptime.Milliseconds 
  44.     "System Up for: {0} days, {1} hours, {2}.{3} minutes" -f $d,$h,$m,$ms 
  45. }  
  46. # End script 

Sunday 11 January 2009

Get-Screensaver.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     Determine if a computer screen saver requires a password. 
  4. .DESCRIPTION 
  5.     This script is a re-write of script 2 on the MSDN site (see 
  6.     below for link). This script also displays the user name for 
  7.     each desktop and the screen saver executable. 
  8. .NOTES 
  9.     File Name  : Get-Screensaver.ps1 
  10.     Author     : Thomas Lee - tfl@psp.co.uk 
  11.     Requires   : PowerShell V2 CTP3 
  12. .LINK 
  13.     Script published to: 
  14.     http://www.pshscripts.blogspot.com 
  15.     Adapted from MSDN: 
  16.     http://msdn.microsoft.com/en-us/library/aa394591(VS.85).aspx 
  17. .EXAMPLE 
  18.     PS c:\foo> .\Get-Screensaver.ps1 
  19.     5 desktops found as follows 
  20.     Desktop      : NT AUTHORITY\SYSTEM 
  21.     Screen Saver : logon.scr 
  22.     Secure       : False 
  23.  
  24.     Desktop      : NT AUTHORITY\LOCAL SERVICE 
  25.     Screen Saver : %SystemRoot%\System32\logon.scr 
  26.     Secure       : False 
  27.  
  28.     Desktop      : NT AUTHORITY\NETWORK SERVICE 
  29.     Screen Saver : %SystemRoot%\System32\logon.scr 
  30.     Secure       : False 
  31.   Win
  32.     Desktop      : Cookham\tfl 
  33.     Screen Saver : %Systemroot%\tflscreensaver.scr 
  34.     Secure       : False 
  35.  
  36.     Desktop      : .DEFAULT 
  37.     Screen Saver : logon.scr 
  38.     Secure       : False 
  39. #> 
  40.  
  41. ## 
  42. #  Start of script 
  43. ## 
  44.  
  45. $Computer = "." 
  46. $Desktops = Get-WMIObject -class Win32_Desktop -ComputerName $computer 
  47. "{0} desktops found as follows" -f $desktops.count 
  48. foreach ($desktop in $desktops) { 
  49. "Desktop      : {0}"  -f $Desktop.Name 
  50. "Screen Saver : {0}"  -f $desktop.ScreensaverExecutable 
  51. "Secure       : {0} " -f $desktop.ScreenSaverSecure 
  52. "" 

Saturday 10 January 2009

Get-Hash2.ps1

  1. <# 
  2. .SYNOPSIS 
  3.    Creates a Hash of a file and returns the hash   
  4. .DESCRIPTION 
  5.     Uses System.Security.Cryptography.HashAlgorithm and members to create
  6.     the hash. This script improves on:
  7.     http://pshscripts.blogspot.com/2009/01/get-hashpsm1.html by using 
  8. .NOTES 
  9.     File Name  : Get-Hash1.PSM1 
  10.     Author     : Thomas Lee - tfl@psp.co.uk 
  11.     Requires   : PowerShell V2 CTP3 
  12.     Thanks to the #PowerShell Twitter Posse (PTP) for help figuring out
  13.     the –verbose parameter. And thanks to the PTP for comments on the
  14.     earlier version of this script, which now uses a file stream as 
  15.     input to the hash alghorithm. 
  16. .LINK 
  17.     Posted to         :  http://pshscripts.blogspot.com/2009/01/get-hash2ps1.html
  18.     Based on          :  http://tinyurl.com/aycszb written by Bart De Smet 
  19.     An improvement of :  http://pshscripts.blogspot.com/2009/01/get-hashpsm1.html 
  20. .PARAMETER Algorithm 
  21.     The name of one of the hash Algorithms defined at 
  22.     http://msdn.microsoft.com/en-us/library/system.security.cryptography.hashalgorithm.aspx 
  23. .PARAMETER File 
  24.     The name of a file to provide a hash for
  25. .PARAMETER Verbose 
  26.     if specified, this script will produce chattier output. 
  27. .EXAMPLE 
  28.     PS C:\foo> ls *.txt | where {!$_.psiscontainer}| c:\foo\Get-Hash2.ps1 sha1 -verbose 
  29.     OK, I'll be chatty 
  30.     The sha1 hash of file C:\foo\asciifile.txt is: "55055a5c8eeb3af7fa6d426314578ee1d56df016" 
  31.  
  32.     The sha1 hash of file C:\foo\log.txt is: "575f4b35e3cadb9b273095fc463bd43e9a3f5774" 
  33.  
  34.     The sha1 hash of file C:\foo\sites.txt is: "8ce6663cd2b64a513cf18a831843afd98e190764" 
  35.  
  36.     The sha1 hash of file C:\foo\test.txt is: "a2f26abbeeb4e6846e159ba506e07cae5496d458" 
  37.  
  38.     The sha1 hash of file C:\foo\test2.txt is: "9b1baaa9077a3691f8e2685d81ffa24cdd73f71d" 
  39.  
  40.     The sha1 hash of file C:\foo\unicodefile.txt is: "094ef2696d9eb3374e657eb7c227ff4c36cd0cb9" 
  41. .EXAMPLE 
  42.     PS C:\foo> ls *.txt | where {!$_.psiscontainer}| c:\foo\Get-Hash2.ps1 sha1 
  43.     55055a5c8eeb3af7fa6d426314578ee1d56df016 
  44.     575f4b35e3cadb9b273095fc463bd43e9a3f5774 
  45.     8ce6663cd2b64a513cf18a831843afd98e190764 
  46.     a2f26abbeeb4e6846e159ba506e07cae5496d458 
  47.     9b1baaa9077a3691f8e2685d81ffa24cdd73f71d 
  48.     094ef2696d9eb3374e657eb7c227ff4c36cd0cb9 
  49. .EXAMPLE 
  50.     PS C:\foo> Get-Hash  md5 asciifile.txt -verbose 
  51.     OK, I'll be chatty 
  52.     The md5 hash of file c:\foo\asciifile.txt is: "06f51e7bfced5c0623eec5f72e0999d6" 
  53. .EXAMPLE 
  54.     PS C:\foo> .\get-hash2 md5 c:\foo\asciifile.txt 
  55.     06f51e7bfced5c0623eec5f72e0999d6 
  56. #> 
  57. #[CmdletBinding()] 
  58. param ( 
  59. [Parameter(Position=0, mandatory=$true)] 
  60. [string] $Algorithm, 
  61. [Parameter(Position=1, mandatory=$true, valuefrompipeline=$true)] 
  62. [string] $File 
  63.  
  64. Begin {  
  65. if ($VerbosePreference.Value__ -eq 0) {$verbose=$false} else {$verbose=$true
  66. if ($Verbose) {"OK, I'll be chatty"
  67.  
  68. Process { 
  69.  
  70. if ($VerbosePreference.Value__ -eq 0) {$verbose=$false} else {$verbose=$true
  71.  
  72.     # Get the alghorthm object 
  73.     $Algo=[System.Security.Cryptography.HashAlgorithm]::Create($algorithm) 
  74.     if ($Algo){ 
  75.         # Open the file 
  76.         $Filemode = [System.IO.FileMode]::Open 
  77.         $Fs = New-Object System.Io.Filestream $File, $Filemode 
  78.         if ($fs.length -gt 0) { 
  79.             # Now compute hash 
  80.             $Hash = $Algo.ComputeHash($Fs)    
  81.             $Hashstring ="" 
  82.             foreach ($byte in $hash) {$hashstring += $byte.tostring("x2")} 
  83.             # pass hash string on 
  84.             if ($verbose){ 
  85.               "The {0} hash of file {1} is: `"{2}`"" -f $algorithm, $file, $hashstring 
  86.               "" 
  87.             } 
  88.             else
  89.              $Hashstring 
  90.             } 
  91.         } 
  92.         else
  93.              if ($verbose) {"File {0} can not be hashed" -f $file ; ""}      
  94.         } 
  95.         $fs.close() 
  96.         } 
  97.     else {"Algorithm {0} not found" -f $algorithm} 

Share this post :