Tuesday 31 August 2010

Get-Cultures.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script displays information of cultures on a system. 
  4. .DESCRIPTION 
  5.     This script reimplements an MSDN script using PowerShell. It first 
  6.     gets the cultures then displays them 
  7. .NOTES 
  8.     File Name  : Get-Cultures.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/08/get-cultures.html 
  14.     MSDN sample posted to: 
  15.         http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.getcultures.aspx  
  16. .EXAMPLE 
  17.     PSH [C:\foo]: .\Get-Cultures.ps1' 
  18.     CULTURE ISO ISO WIN DISPLAYNAME                             NATIVENAME 
  19.     af       af  afr AFK Afrikaans                               Afrikaans 
  20.     am       am  amh AMH Amharic                                 Amharic 
  21.     ar       ar  ara ARA Arabic                                  Arabic 
  22.     arn      arn arn MPD Mapudungun                              Mapudungun 
  23.     as       as  asm ASM Assamese                                Assamese 
  24.     az       az  aze AZE Azeri                                   Azeri 
  25.     az-Cyrl  az  aze AZC Azeri (Cyrillic)                        Azeri (Cyrillic) 
  26.     az-Latn  az  aze AZE Azeri (Latin)                           Azeri (Latin) 
  27.     ... snipped to save space! 
  28. #> 
  29.  
  30. # Get Cultures 
  31. $Cultures = [System.Globalization.CultureInfo]::GetCultures([System.Globalization.CultureTypes]::NeutralCultures) 
  32.  
  33. # Display header then details 
  34. "CULTURE ISO ISO WIN DISPLAYNAME                             NATIVENAME" 
  35. foreach ($Ci in $Cultures
  36.       { 
  37. "{0,-8} {1,-3} {2,-3} {3,-3} {4,-40}{4,-40}" -f $Ci.Name, 
  38.              $Ci.TwoLetterISOLanguageName, 
  39.              $Ci.ThreeLetterISOLanguageName,   
  40.              $Ci.ThreeLetterWindowsLanguageName, 
  41.              $Ci.DisplayName, 
  42.              $Ci.NativeName 

Sunday 29 August 2010

Get-Time.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script converts time to different time zones.  
  4. .DESCRIPTION 
  5.     This script re-implements an MSDN sample using PowerShell 
  6. .NOTES 
  7.     File Name  : Get-Time.ps1 
  8.     Author     : Thomas Lee - tfl@psp.co.uk 
  9.     Requires   : PowerShell Version 2.0 
  10. .LINK 
  11.     This script posted to: 
  12.         http://pshscripts.blogspot.com/2010/08/get-time.html 
  13.     MSDN Sample posted at: 
  14.         http://msdn.microsoft.com/en-us/library/system.timezoneinfo.findsystemtimezonebyid.aspx  
  15. .EXAMPLE 
  16.     PSH [C:\foo]: .\Get-Time.ps1 
  17.     Time in GMT Daylight Time zone: 8/29/2010 11:40:50 AM 
  18.        UTC Time: 8/29/2010 10:40:50 AM 
  19.     Time in Tokyo Daylight Time zone: 8/29/2010 07:40:50 PM 
  20.        UTC Time: 8/29/2010 10:40:50 AM 
  21. #> 
  22.  
  23. # Get local time 
  24. $thisTime = [system.DateTime]::Now 
  25.  
  26. if ([System.TimeZoneInfo]::Local.IsDaylightSavingTime($thisTime)) { 
  27.     $tzn = [System.TimeZoneInfo]::Local.DaylightName } 
  28. else
  29.     $tzn = [System.TimeZoneInfo]::Local.StandardName 
  30. # Display local Time 
  31. "Time in {0} zone: {1}" -f $tzn, $thisTime 
  32. "   UTC Time: {0}" -f [system.TimeZoneInfo]::ConvertTimeToUtc($thisTime, [TimeZoneInfo]::Local) 
  33.  
  34. # Get Tokyo Standard Time zone 
  35. $tst = [system.TimeZoneInfo]::FindSystemTimeZoneById("Tokyo Standard Time"
  36. $tstTime = [system.TimeZoneInfo]::ConvertTime($thisTime, [TimeZoneInfo]::local, $tst
  37. $tstzn = if ( [System.TimeZoneInfo]::Local.IsDaylightSavingTime($tstTime)) { 
  38.          $tst.DaylightName} else {$tst.StandardName} 
  39.  
  40. # Display Tokyo Time Zone 
  41. "Time in {0} zone: {1}" -f $tstzn,$tstTime 
  42. "   UTC Time: {0}" -f [System.TimeZoneInfo]::ConvertTimeToUtc($tstTime, $tst

Saturday 28 August 2010

Get-TimeZoneInfoInfo

  1. <# 
  2. .SYNOPSIS 
  3.     This script displays/Uses TimeZoneInfo properties  
  4. .DESCRIPTION 
  5.     This script displays the use of all the TimeZoneInfo Properties. 
  6. .NOTES 
  7.     File Name  : Get-TimeZoneInfoInfo.ps1 
  8.     Author     : Thomas Lee - tfl@psp.co.uk 
  9.     Requires   : PowerShell Version 2.0 
  10. .LINK 
  11.     This script posted to: 
  12.         http://pshscripts.blogspot.com/2010/08/get-timezoneinfoinfo.html 
  13.     MSDN Sample posted at: 
  14.         http://msdn.microsoft.com/en-us/library/system.timezoneinfo_properties.aspx    
  15. .EXAMPLE 
  16.     PSH [C:\foo]: .Get-TimeZoneInfoInfo.ps1' 
  17.     Time Zone: (GMT) Greenwich Mean Time : Dublin, Edinburgh, Lisbon, London 
  18.       Time zone is 0 hours 0 minutes later than Coordinated Universal Time. 
  19.       Current time is 12:22 PM on 8/28/2010 GMT Daylight Time 
  20.       Timezone id              : GMT Standard Time 
  21.       Supports Daylight Saving : True 
  22.       Daylight Saving Zone Name: GMT Daylight Time 
  23.   
  24.     Time Zone: (GMT+04:30) Kabul 
  25.       Time zone is 4 hours 30 minutes later than Coordinated Universal Time. 
  26.       Current time is 12:22 PM on 8/28/2010 Afghanistan Standard Time 
  27.       Timezone id              : Afghanistan Standard Time 
  28.       Supports Daylight Saving : False 
  29.       Daylight Saving Zone Name: Afghanistan Daylight Time 
  30.   
  31.     Time Zone: UTC 
  32.       Time zone is 0 hours 0 minutes later than Coordinated Universal Time. 
  33.       Current time is 12:22 PM on 8/28/2010 UTC 
  34.       Timezone id              : UTC 
  35.       Supports Daylight Saving : False 
  36.       Daylight Saving Zone Name: UTC 
  37. #> 
  38. # Display Information About Time Zones 
  39.  
  40. # Get current date 
  41. $Datenow = Get-Date 
  42.  
  43. # Display info re Local Time Zone 
  44. $LocalZone = [System.TimeZoneInfo]::Local 
  45. $t1 = [system.Math]::Abs($LocalZone.BaseUtcOffset.Hours) 
  46. $t2 = [System.Math]::Abs($LocalZone.BaseUtcOffset.Minutes)  
  47. $t3 = if ($LocalZone.BaseUtcOffset -ge  [System.Timespan]::Zero) {"later"} else {"earlier"
  48. $t4 = if ($LocalZone.IsdaylightSavingTime($datenow)) {$Localzone.DaylightName} else {$Localzone.Standardname} 
  49. "Time Zone: {0}" -f $Localzone.Displayname 
  50. "  Time zone is {0} hours {1} minutes {2} than Coordinated Universal Time." -f   $t1,$t2, $t3 
  51. "  Current time is {0:t} on {0:d} {1}" -f $datenow,$t4 
  52. "  Timezone id              : {0}" -f $LocalZone.Id 
  53. "  Supports Daylight Saving : {0}" -f $LocalZone.SupportsDaylightSavingTime 
  54. "  Daylight Saving Zone Name: {0}" -f $Localzone.DaylightName 
  55. "" 
  56.  
  57. # Get Kabul Time 
  58. $Kt = [system.TimeZoneInfo]::GetSystemTimeZones()  | ? {$_.id -match "Afghanistan Standard Time"
  59. $tz = $kt.Displayname 
  60. $t1 = [system.Math]::Abs($kt.BaseUtcOffset.Hours) 
  61. $t2 = [System.Math]::Abs($kt.BaseUtcOffset.Minutes)  
  62. $t3 = if ($kt.BaseUtcOffset -ge  [System.timespan]::Zero) {"later"} else {"earlier"
  63. $t4 = if ($Kt.IsdaylightSavingTime($datenow)) {$Kt.DaylightName} else {$Kt.Standardname} 
  64.  
  65. # Display information 
  66. "Time Zone: {0}" -f $Kt.Displayname 
  67. "  Time zone is {0} hours {1} minutes {2} than Coordinated Universal Time." -f   $t1,$t2,$t3 
  68. "  Current time is {0:t} on {0:d} {1}" -f $datenow,$t4 
  69. "  Timezone id              : {0}" -f $Kt.Id 
  70. "  Supports Daylight Saving : {0}" -f $Kt.SupportsDaylightSavingTime 
  71. "  Daylight Saving Zone Name: {0}" -f $Kt.DaylightName 
  72. "" 
  73.  
  74. # Display Information regarding UTC 
  75. $UtcZone = [System.TimeZoneInfo]::UTC 
  76. $t1 = [system.Math]::Abs($UtcZone.BaseUtcOffset.Hours) 
  77. $t2 = [System.Math]::Abs($UtcZone.BaseUtcOffset.Minutes)  
  78. $t3 = if ($UtcZone.BaseUtcOffset -ge  [System.Timespan]::Zero) {"later"} else {"earlier"
  79. $t4 = if ($UtcZone.IsdaylightSavingTime($datenow)) {$Utczone.DaylightName} else {$UtcZone.Standardname} 
  80.  
  81. # Display information 
  82. "Time Zone: {0}" -f $UtcZone.Displayname 
  83. "  Time zone is {0} hours {1} minutes {2} than Coordinated Universal Time." -f   $t1,$t2,$t3 
  84. "  Current time is {0:t} on {0:d} {1}" -f $Datenow,$t4 
  85. "  Timezone id              : {0}" -f $UtcZone.Id 
  86. "  Supports Daylight Saving : {0}" -f $UtcZone.SupportsDaylightSavingTime 
  87. "  Daylight Saving Zone Name: {0}" -f $UtcZone.DaylightName 

Wednesday 25 August 2010

Get-NoDstTimeZones

  1. <# 
  2. .SYNOPSIS 
  3.     This script displays those time zones that do NOT support 
  4.     daylight savings time. 
  5. .DESCRIPTION 
  6.     This script uses .NET to get the time zones defined on a system 
  7.     then displays those that do not suport daylight savings time. 
  8. .NOTES 
  9.     File Name  : Get-NoDstTimeZones.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 at: 
  16.        http://msdn.microsoft.com/en-us/library/system.timezoneinfo.supportsdaylightsavingtime.aspx 
  17. .EXAMPLE 
  18.     PSH [C:\foo]: .\Get-NoDstTimeZones.ps1' 
  19.     (UTC) Coordinated Universal Time 
  20.     (UTC) Monrovia, Reykjavik 
  21.     (UTC+01:00) West Central Africa 
  22.     ... {output snipped to save space} 
  23. #> 
  24.  
  25. # Get Time Zones 
  26. $zones = [System.TimeZoneInfo]::GetSystemTimeZones() 
  27.  
  28. # For each zone, display name if the zone does not support dst 
  29. foreach($zone in $zones) { 
  30.    if (! $zone.SupportsDaylightSavingTime) {$zone.DisplayName} 

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. "" 

Monday 16 August 2010

Get-Sha1HashFile.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script uses the SHA1 Crypto Provider to hash a file 
  4. .DESCRIPTION 
  5.     This script uses SHA1 Cryptoprovider to hash a file and print the hash. A second 
  6.     hash is created of the same file, but with a space added. The file I picked is a 
  7.     large text file I keep in c:\foo for uses like this! 
  8. .NOTES 
  9.     File Name  : Get-Sha1HashFile.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.     Posted to TechEd Sample Gallery at: 
  16.  
  17. .EXAMPLE 
  18.     PSH [C:\foo]: .\Get-Sha1HashFile.ps1 
  19.     Hashing     : C:\foo\gd.txt 
  20.     Results in  : 760bb67356560232 
  21.     Hashing     :  C:\foo\gd.txt + space 
  22.     Results in  : 4f7b4902168bba7d 
  23.      
  24. #> 
  25.  
  26. # Create Input Data 
  27. $file       = (ls c:\foo\*.txt | sort -descending length)[0] # get largest file 
  28. $filestr    = Get-Content $file 
  29. $filebytes1 = [System.Text.UnicodeEncoding]::Unicode.getbytes($files
  30. $filebytes2 = [System.Text.UnicodeEncoding]::Unicode.getbytes($files+" "
  31.  
  32. # Create a New SHA1 Crypto Provider 
  33. $sha = New-Object System.Security.Cryptography.SHA1CryptoServiceProvider 
  34.  
  35. # Now hash that 
  36. $result1 = $sha.ComputeHash($filebytes1
  37. "Hashing     : {0}" -f $file.FullName 
  38. "Results in  : {0}" -f [system.BitConverter]::ToUint64($result1,0).tostring("x"
  39.  
  40. $result2 = $sha.ComputeHash($filebytes2
  41. "Hashing     : {0}" -f " $($file.FullName) + space" 
  42. "Results in  : {0}" -f [system.BitConverter]::ToUint64($result2,0).tostring("x"

Friday 13 August 2010

Get-Sha1Hash.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script uses the SHA1 Crypto Provider to hash a string 
  4. .DESCRIPTION 
  5.     This script creates 2 strings, and a SHA1 Crypto Provider. 
  6.     The script then hashes the strings and displays the results.  
  7. .NOTES 
  8.     File Name  : Get-Sha1Hash.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 at: 
  15.         http://msdn.microsoft.com/en-us/library/system.security.cryptography.sha1cryptoserviceprovider.aspx 
  16.     Posted to TechEd Sample Gallery at: 
  17.      
  18. .EXAMPLE 
  19.     PSH [C:\foo]: . 'E:\PowerShellScriptLib\System.Security.Crpytography\Get-Sha1Hash.ps1' 
  20.     Hashing     : This is a string to hash 
  21.     Results in  : z→???_r♠W??o???]rv?? 
  22.   
  23.     Hashing     : this is a string to hash 
  24.     Results in  : ???☺$?Z??♀???????U?' 
  25. #> 
  26.  
  27. # Create Input Data 
  28. $enc = [system.Text.Encoding]::ASCII 
  29. $string1 = "This is a string to hash" 
  30. $string2 = "this is a string to hash" 
  31. $data1 = $enc.GetBytes($string1
  32. $data2 = $enc.GetBytes($string2
  33.  
  34. # Create a New SHA1 Crypto Provider 
  35. $sha = New-Object System.Security.Cryptography.SHA1CryptoServiceProvider 
  36.  
  37. # Now hash and display results 
  38. $result1 = $sha.ComputeHash($data1
  39. "Hashing     : {0}" -f $string1 
  40. "Results in  : {0}" -f $enc.Getstring($result1
  41. "" 
  42.  
  43. $result2 = $sha.ComputeHash($data2
  44. "Hashing     : {0}" -f $string2 
  45. "Results in  : {0}" -f $enc.Getstring($result2

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 

Wednesday 11 August 2010

Get-EncryptedAesString.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script encrypts then decrypts a string using AES 
  4. .DESCRIPTION 
  5.     This script re-implements an MSDN sample that first  
  6.     encrypts a string then decrypts it. The crypto is done 
  7.     using AES. Running this script multiple times will result 
  8.     in differently encrypted quotes since a new key/IV is  
  9.     generated each time you run the script. 
  10. .NOTES 
  11.     File Name  : Get-AesEnctyptedString.ps1 
  12.     Author     : Thomas Lee - tfl@psp.co.uk 
  13.     Requires   : PowerShell Version 2.0 
  14. .LINK 
  15.     This script posted to: 
  16.         http://pshscripts.blogspot.com/2010/08/get-encryptedaesstringps1.html 
  17.     MSDN Sample posted at: 
  18.         http://msdn.microsoft.com/en-us/library/09d0kyb3.aspx  
  19. .EXAMPLE 
  20.     PSH [C:\foo]: .\Get-AesEncryptedString.ps1 
  21.     Quote: 
  22.     Things may come to those who wait, but only the things left by those who hustle. -- Abraham Lincoln 
  23.   
  24.     Encrypted Quote: 
  25.     zp4gD/Mmu01Msr3V8d94Kvwut4ClaW+f1HyohT+QjpFN5FVNhyLcQIgia4iD2TcsK/SpLcm5cOi1/KM+d9ENeU2Lkn8fOZHpMnklUrABGDoM1BRQDfWNcVbMIOA4IUY 
  26.     zLoJ5huHKhnyPA2fobGouW33HOpONI0zR3KS4RdtD3M3QQsqHW2+QOkE4/Ls/5gCSbcTuP2FDkk3J9b+1XgqzJZx0jZIBl+moUcqA33xQdu5bGhwg1E8sk2oEU8AQyy 
  27.     XcRrS/h8vTLve8c0Tpj4f2Vg== 
  28.   
  29.     Decrypted Quote: 
  30.     Things may come to those who wait, but only the things left by those who hustle. -- Abraham Lincoln 
  31. #> 
  32.  
  33.  
  34. # Best Practice! 
  35. Set-StrictMode  -version 2 
  36.   
  37. # Two helper functions 
  38. function EncryptString{ 
  39. param( $SymAlg,  
  40.       [string] $inString 
  41. $inBlock = [System.Text.UnicodeEncoding]::Unicode.getbytes($instring
  42. $xfrm = $symAlg.CreateEncryptor() 
  43. $outBlock = $xfrm.TransformFinalBlock($inBlock, 0, $inBlock.Length); 
  44. return $outBlock
  45.   
  46. function DecryptBytes { 
  47. param ($SymAlg,  
  48. $inBytes 
  49. $xfrm = $symAlg.CreateDecryptor(); 
  50. $outBlock = $xfrm.TransformFinalBlock($inBytes, 0, $inBytes.Length) 
  51. return [System.Text.UnicodeEncoding]::Unicode.GetString($outBlock
  52.  
  53. # main script 
  54. $Quote = "Things may come to those who wait, but only the "
  55.          "things left by those who hustle. -- Abraham Lincoln" 
  56. "Quote:";$Quote;"" 
  57.   
  58. # Generate CSP, Key and IV 
  59. $AesCSP = New-Object System.Security.Cryptography.AesCryptoServiceProvider 
  60. $AesCSP.GenerateKey() 
  61. $AesCSP.GenerateIV() 
  62.  
  63. # Encrypt quote 
  64. $EncQuote = EncryptString $aesCSP $quote 
  65. "Encrypted Quote:" 
  66. [System.Convert]::ToBase64String($encQuote
  67. "" 
  68.   
  69. # Now Decrypt  
  70. "Decrypted Quote:" 
  71. DecryptBytes $AesCSP $EncQuote 

Tuesday 10 August 2010

Convert-Date.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script manipulates a time using TimeZone info methods. 
  4. .DESCRIPTION 
  5.     This script re-implements an MSDN sample using PowerShell. The script first 
  6.     creates a DateTime object then it converts it to Universal, UTC, Pacific and 'local' time.   
  7. .NOTES 
  8.     File Name  : Convert-Date.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/08/convert-dateps1.html 
  14.     MSDN Sample posted at: 
  15.         http://msdn.microsoft.com/en-us/library/system.timezoneinfo.local.aspx  
  16. .EXAMPLE 
  17.     PSH [C:\foo]: . 'E:\PowerShellScriptLib\System.TimeZoneINfo\Convert-date.ps 
  18.     Date           : 3/21/2006 02:00:00 AM 
  19.     Local Time zone: GMT Standard Time 
  20.      In Universal time: 3/21/2006 02:00:00 AM 
  21.      In UTC           : 3/21/2006 02:00:00 AM 
  22.      In Local TZ:     : 3/21/2006 02:00:00 AM 
  23.      In Pacific TZ    : 3/21/2006 10:00:00 AM 
  24. #> 
  25. # Create date object 
  26. $date1 = New-Object System.DateTime 2006, 3, 21, 2, 0, 0 
  27.  
  28. # Display date and local time zone then show that time in other time zones 
  29. "Date           : {0}" -f $date1 
  30. "Local Time zone: {0}" -f  ([System.TimeZoneInfo]::Local).id 
  31. " In Universal time: {0}" -f $date1.ToUniversalTime() 
  32. " In UTC           : {0}" -f [System.TimeZoneInfo]::ConvertTimeToUtc($date1
  33. " In Local TZ:     : {0}" -f [System.TimeZoneInfo]::ConvertTimeToUtc($date1, ([System.TimeZoneInfo]::Local)) 
  34. $tz = [System.TimeZoneInfo]::FindSystemTimeZoneById("Pacific Standard Time");   
  35. " In Pacific TZ    : {0}" -f [System.TimeZoneInfo]::ConvertTimeToUtc($date1, $tz

Monday 9 August 2010

Divide-BigInteger.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script divides big integers using .NET Framework. 
  4. .DESCRIPTION 
  5.     This script reimplements an MSDN Sample script using powershell. The 
  6.     script creates a dividor and an arry of dividends, then performs  
  7.     division operations several ways. 
  8. .NOTES 
  9.     File Name  : Divide-BigInteger.ps1 
  10.     Author     : Thomas Lee - tfl@psp.co.uk 
  11.     Requires   : PowerShell Version 2.0 
  12.                  .NET Framework 4.0 or higher 
  13. .LINK 
  14.     This script posted to: 
  15.         http://www.pshscripts.blogspot.com 
  16.     MSDN Sample posted at: 
  17.         http://msdn.microsoft.com/en-us/library/system.numerics.biginteger.divide.aspx 
  18. .EXAMPLE 
  19.     PSH [C:\foo]: . 'C:\Users\tfl\AppData\Local\Temp\Untitled7.ps1' 
  20.     Dividend: 680,564,693,277,057,719,623,408,366,969,033,850,880 
  21.     Divisor:  85,070,591,730,234,615,847,396,907,784,232,501,249 
  22.     Results: 
  23.        Using Divide method:     7 
  24.        Using Division operator: 7 
  25.        Using DivRem method:     7, remainder 85,070,551,165,415,408,691,630,012,479,406,342,137 
  26.  
  27.     Dividend: 90,612,345,123,875,509,091,827,560,007,100,099 
  28.     Divisor:  85,070,591,730,234,615,847,396,907,784,232,501,249 
  29.     Results: 
  30.        Using Divide method:     0 
  31.        Using Division operator: 0 
  32.        Using DivRem method:     0, remainder 90,612,345,123,875,509,091,827,560,007,100,099 
  33.      
  34.     Dividend: 1 
  35.     Divisor:  85,070,591,730,234,615,847,396,907,784,232,501,249 
  36.     Results: 
  37.        Using Divide method:     0 
  38.        Using Division operator: 0 
  39.        Using DivRem method:     0, remainder 1 
  40.      
  41.     Dividend: 19,807,040,619,342,712,359,383,728,129 
  42.     Divisor:  85,070,591,730,234,615,847,396,907,784,232,501,249 
  43.     Results: 
  44.        Using Divide method:     0 
  45.        Using Division operator: 0 
  46.        Using DivRem method:     0, remainder 19,807,040,619,342,712,359,383,728,129 
  47.   
  48.     Dividend: 85,070,591,730,234,615,847,396,907,784,232,501,250 
  49.     Divisor:  85,070,591,730,234,615,847,396,907,784,232,501,249 
  50.     Results:  
  51.        Using Divide method:     1 
  52.        Using Division operator: 1 
  53.        Using DivRem method:     1, remainder 1 
  54.   
  55. #> 
  56.  
  57. # Add namespace 
  58. $r = [system.Reflection.Assembly]::LoadWithPartialName("System.Numerics"
  59.  
  60. #Create a big integer divisor and an array of dividends 
  61. $divisor = [system.numerics.BigInteger]::pow([Int64]::MaxValue, 2) 
  62. $dividends = @() 
  63. $dividends += [system.numerics.BigInteger]::Multiply(([system.numerics.BigInteger] [system.Single]::MaxValue), 2) 
  64. $dividends += [system.numerics.BigInteger]::Parse("90612345123875509091827560007100099")  
  65. $dividends += [system.numerics.BigInteger]::One 
  66. $dividends += [system.numerics.BigInteger]::Multiply([Int32]::MaxValue, [Int64]::MaxValue) 
  67. $dividends += $divisor + [system.numerics.BigInteger]::One  
  68.   
  69. #Divide each dividend by divisor in three different ways 
  70. foreach ($dividend in $dividends) { 
  71.     "Dividend: {0:N0}" -f $dividend 
  72.     "Divisor:  {0:N0}" -f $divisor 
  73.     "Results:" 
  74.     "   Using Divide method:     {0:N0}" -f  [system.Numerics.BigInteger]::Divide($dividend, $divisor
  75.     "   Using Division operator: {0:N0}" -f ($dividend/$divisor
  76.     $remainder = [system.Numerics.BigInteger]::Zero 
  77.     $quotient = [system.numerics.BigInteger]::DivRem($dividend, $divisor, [ref] $remainder
  78.     "   Using DivRem method:     {0:N0}, remainder {1:N0}" -f $quotient, $remainder 
  79.     "" 
  80. }         

Sunday 8 August 2010

Get-DriveInfo.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script gets and displays basic information about drives on a local system.  
  4. .DESCRIPTION 
  5.     This script uses the System.Io.DriveInfo's GetDrives method to get drive 
  6.     info which is then displayed.   This is a re-implementation of an MSDN 
  7.     sample. 
  8. .NOTES 
  9.     File Name  : Get-DriveInfo.ps1 
  10.     Author     : Thomas Lee - tfl@psp.co.uk 
  11.     Requires   : PowerShell Version 2.0 
  12. .LINK 
  13.     This script posted to: 
  14.         http://pshscripts.blogspot.com/2010/07/get-driveinfops1.html  
  15.     MSDN Sample posted at: 
  16.         http://msdn.microsoft.com/en-us/library/system.io.driveinfo.aspx 
  17.         http://msdn.microsoft.com/en-us/library/system.io.driveinfo.getdrives.aspx 
  18. .EXAMPLE 
  19.    PSH [C:\foo]: .\Get-DriveInfo.ps1' 
  20.    Drive C:\ 
  21.       File type: Fixed 
  22.       Volume label: 
  23.       File system: NTFS 
  24.       Available space to current user:           6.75 gb 
  25.       Total available space:                     6.75 gb 
  26.       Total size of drive:                     48.828 gb 
  27.    Drive D:\ 
  28.       File type: Fixed 
  29.       Volume label: 
  30.       File system: FAT32 
  31.       Available space to current user:           1.81 gb 
  32.       Total available space:                     1.81 gb 
  33.       Total size of drive:                      2.003 gb 
  34.       <ETC...> 
  35. #> 
  36.  
  37. # Get Drive Info 
  38. $allDrives = [system.Io.DriveInfo]::GetDrives() 
  39.  
  40. # Now display details 
  41. foreach ($d in $allDrives)  { 
  42.     "Drive {0}"        -f $d.Name 
  43.     "  File type: {0}" -f $d.DriveType 
  44.     if ($d.IsReady)  { 
  45.          "  Volume label: {0}" -f $d.VolumeLabel 
  46.          "  File system: {0}"  -f $d.DriveFormat 
  47.          $fs  = $d.AvailableFreeSpace/1gb 
  48.          $tfs = $d.TotalFreeSpace/1gb 
  49.          $TS  = $d.TotalSize/1gb 
  50.          "  Available space to current user:{0, 15:n2} gb" -f $fs 
  51.          "  Total available space:          {0, 15:n2} gb" -f $tfs 
  52.          "  Total size of drive:            {0, 15:n3} gb" -f $ts 
  53.     } 

Wednesday 4 August 2010

Get-MultiplyBigInteger.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script re-implements this MSDN Sample of  
  4.     multiplying a big integer. 
  5. .DESCRIPTION 
  6.     This script first tries and fails to multiple a pair of large integers. The 
  7.     script catches the error and then used BigInteger.Multiply to multiply  
  8.     the two big itegers. 
  9. .NOTES 
  10.     File Name  : Get-MultiplyBigInteger.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 at: 
  17.         http://msdn.microsoft.com/en-us/library/system.numerics.biginteger.multiply.aspx 
  18. .EXAMPLE 
  19.     PSH [C:\foo]: .Get-MultiplyBigInteger.ps1 
  20.     Too big for long, try biginteger 
  21.     12,193,263,111,263,526,900 
  22. #> 
  23. # Add System.Numerics namespace 
  24. $r=[system.Reflection.Assembly]::LoadWithPartialName("System.Numerics"
  25.  
  26. # Two big numbers 
  27. $number1 = 1234567890 
  28. $number2 = 9876543210 
  29.  
  30. # Try normal [long] then catch error and do biginteger 
  31. try 
  32.    [long] $product = $number1 * $number2 
  33. catch  
  34. "Too big for long, try biginteger" 
  35.    $product = New-Object System.Numerics.BigInteger 
  36.    $product = [System.Numerics.BigInteger]::Multiply($number1, $number2
  37.    $product.ToString("N0"
  38. }