Thursday, 8 January 2009

Get-Hash.psm1

  1. function Get-Hash { 
  2. <# 
  3. .SYNOPSIS 
  4.    Creates a Hash of a file and prints the hash   
  5. .DESCRIPTION 
  6.     Uses System.Security.Cryptography.HashAlgorithm and members to create the hash 
  7.     Also uses System.Text.AsciiEncoding to convert string to byte array. 
  8.     Created as a Module. 
  9. .NOTES 
  10.     File Name  : Get-Hash.PSM1 
  11.     Author     : Thomas Lee - tfl@psp.co.uk 
  12.     Requires   : PowerShell V2 CTP3 
  13.     Thanks to the #PowerShell Twitter Posse for help figuring out -verbose. 
  14.     http://www.pshscripts.blogspot.com 
  15.     Based on  
  16.     http://tinyurl.com/aycszb written by Bart De Smet 
  17. .PARAMETER Algorithm 
  18.     The name of one of the hash Algorithms defined at 
  19.     http://msdn.microsoft.com/en-us/library/system.security.cryptography.hashalgorithm.aspx 
  20. .PARAMETER File 
  21.     The name of a file to provide a hash for
  22. .EXAMPLE 
  23.     PS C:\foo> ls *.txt | where {!$_.psiscontainer}| Get-Hash sha1 -verbose:$true 
  24.     OK - I'll be chatty 
  25.  
  26.     Processing C:\foo\asciifile.txt 
  27.     sha1 hash of file C:\foo\asciifile.txt is "3529f51d2dd9c3c45e539eee5b42b07a8b74f9f5" 
  28.  
  29.     Processing C:\foo\log.txt 
  30.     sha1 hash of file C:\foo\log.txt is "89dd3b94d3cb7f645498925e77346aa9218d7ffe" 
  31.  
  32.     Processing C:\foo\sites.txt 
  33.     sha1 hash of file C:\foo\sites.txt is "2e434f1c2c16fc1060cd9f2e0226e142ea450ce4" 
  34.  
  35.     Processing C:\foo\test.txt 
  36.     File C:\foo\test.txt can not be hashed 
  37.  
  38.     Processing C:\foo\test.txt.txt 
  39.     File C:\foo\test.txt.txt can not be hashed 
  40.  
  41.     Processing C:\foo\test2.txt 
  42.     File C:\foo\test2.txt can not be hashed 
  43.  
  44.     Processing C:\foo\unicodefile.txt 
  45.     sha1 hash of file C:\foo\unicodefile.txt is "3529f51d2dd9c3c45e539eee5b42b07a8b74f9f5" 
  46. .EXAMPLE 
  47.     PS C:\foo> ls *.txt | where {!$_.psiscontainer}| Get-Hash sha1 
  48.     3529f51d2dd9c3c45e539eee5b42b07a8b74f9f5 
  49.     89dd3b94d3cb7f645498925e77346aa9218d7ffe 
  50.     2e434f1c2c16fc1060cd9f2e0226e142ea450ce4 
  51.     3529f51d2dd9c3c45e539eee5b42b07a8b74f9f5 
  52. .EXAMPLE 
  53.     PS C:\foo> Get-Hash  md5 asciifile.txt -verbose:$true 
  54.     OK - I'll be chatty 
  55.  
  56.     Processing asciifile.txt 
  57.     md5 hash of file asciifile.txt is "b5fe789f9d497f8022d47f620de25499" 
  58. .EXAMPLE 
  59.     PS C:\foo> Get-Hash  md5 asciifile.txt 
  60.     b5fe789f9d497f8022d47f620de25499 
  61. #> 
  62. [CmdletBinding()] 
  63. param ( 
  64. [Parameter(Position=0, mandatory=$true)] 
  65. [string] $Algorithm, 
  66. [Parameter(Position=1, mandatory=$true, valuefrompipeline=$true)] 
  67. [string] $File 
  68.  
  69. begin   { 
  70. if ($VerbosePreference.Value__ -eq 0) {$verbose=$false} else {$verbose=$true
  71. if ($verbose) {"OK - I'll be chatty";""
  72. process { 
  73.     if ($verbose) {"Processing $file"
  74.     $Algo=[System.Security.Cryptography.HashAlgorithm]::Create($algorithm) 
  75.     if ($Algo){ 
  76.         $Fc = Get-Content $File 
  77.         if ($fc.count -gt 0) { 
  78.             $Encoding = New-Object System.Text.ASCIIEncoding 
  79.             $Bytes = $Encoding.GetBytes($fc)     
  80.             # Now compute hash 
  81.             $Hash = $Algo.ComputeHash($bytes)    
  82.             $Hashstring ="" 
  83.             foreach ($byte in $hash) {$hashstring += $byte.tostring("x2")} 
  84.             # pass hash string on 
  85.             if ($verbose){ 
  86.               "{0} hash of file {1} is `"{2}`"" -f $algorithm, $file, $hashstring 
  87.               "" 
  88.             } 
  89.             else
  90.              $Hashstring 
  91.             } 
  92.         } 
  93.         else
  94.              if ($verbose) {"File {0} can not be hashed" -f $file ; ""}      
  95.         }} 
  96.     else
  97.     "Algorithm {0} not found" -f $algorithm 
  98.     } 
  99. } # end process block 
  100. } # End Function Definition 
  101. Export-ModuleMember Get-hash 

Share this post :

Wednesday, 7 January 2009

Get-JulianEraField.ps1

  1. <# 
  2. .Synopsis 
  3.     Displays the value of the [System.Globalzation.JulianCalendar}::JulianEra field 
  4. .Description 
  5.    This script retreives the JulianEra field and displays it, using the .NET class 
  6.    System.Globalization.JulianCalendar. 
  7. .NOTES 
  8.     File Name  : Get-JulianEraField.ps1 
  9.     Author     : Thomas Lee - tfl@psp.co.uk 
  10.     Requires   : PowerShell V2 CTP3 
  11. .LINK 
  12.     Script Posted to: 
  13.     http://www.pshscripts.blogspot.com 
  14.     MSDN Sample at: 
  15.     http://msdn.microsoft.com/en-us/library/system.globalization.juliancalendar.julianera.aspx 
  16. .Example 
  17.     PS C:\foo> .\Get-JulianEraField.ps1 
  18.     The JulianEra Field has a value of: 1 
  19. #> 
  20.  
  21. ### 
  22. # Start of Script 
  23. ## 
  24.  
  25. # Get the value 
  26. $era = [system.Globalization.JulianCalendar]::JulianEra 
  27.  
  28. # Format it 
  29. "The JulianEra field has a value of: {0}" -f $era 

Tuesday, 6 January 2009

Get-Decimal1.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     Demonstrates Decimal arithmetic. 
  4. .DESCRIPTION 
  5.     This script Creates two decimals, then multiplys them and displays them.  
  6. .NOTES 
  7.     File Name  : get-decimal1.ps1 
  8.     Author     : Thomas Lee - tfl@psp.co.uk 
  9.     Requires   : PowerShell V2 CTP3 
  10. .LINK 
  11.     http://www.pshscripts.blogspot.com 
  12.     Posted on msdn: 
  13.     http://msdn.microsoft.com/en-us/library/system.decimal.aspx 
  14. .EXAMPLE 
  15.     PS c:\foo> .\get-decimal1.ps1 
  16.     $d1 is of type: Decimal 
  17.     $d2 is of type: Decimal 
  18.     $d1 (12.1) multiplied by $d2 (12.2) equals: 146.41 
  19.     $d3 is of type: Decimal 
  20. #> 
  21.  
  22. ## 
  23. #  Start of script 
  24. ## 
  25.   
  26. # Create decimal numbers ($d1, $d2) 
  27. [decimal] $d1 = 12.1 
  28. [decimal] $d2 = 12.2 
  29.  
  30. # Create product 
  31. $d3 = $d1*$d1 
  32.  
  33. # print details 
  34. "`$d1 is of type: {0}" -f $d1.gettype().name 
  35. "`$d2 is of type: {0}" -f $d2.gettype().name 
  36. "`$d1 ({0}) multiplied by `$d2 ({1}) equals: {2}" -f $d1,$d2,$d3 
  37. "`$d3 is of type: {0}" -f $d3.gettype().name 
  38. # End of script 

Monday, 5 January 2009

Get-System.EnvironmentsStaticProperties1.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     Demonstrates the static properties on System.Environment 
  4. .DESCRIPTION 
  5.     This script uses the System.Environment class to retrieve interesting 
  6.     information about your system. The fields returned are static fields  
  7.     on the System.Environment class. 
  8. .NOTES 
  9.     File Name  : Get-System.EnvironmentStaticProperties1.ps1 
  10.     Author     : Thomas Lee - tfl@psp.co.uk 
  11.     Requires   : PowerShell V2 CTP3 
  12. .LINK 
  13.     http://www.pshscripts.blogspot.com 
  14. .EXAMPLE 
  15.     PS c:\Foo> .\Get-System.EnvironmentStaticProperties1.ps1 
  16.     Static properties in [System.Environment] 
  17.        TypeName: System.Environment 
  18.  
  19.     Name               MemberType Definition 
  20.     ----               ---------- ---------- 
  21.     CommandLine        Property   static System.String CommandLine {get;} 
  22.     CurrentDirectory   Property   static System.String CurrentDirectory {get;set;} 
  23.     ExitCode           Property   static System.Int32 ExitCode {get;set;} 
  24.     HasShutdownStarted Property   static System.Boolean HasShutdownStarted {get;} 
  25.     MachineName        Property   static System.String MachineName {get;} 
  26.     NewLine            Property   static System.String NewLine {get;} 
  27.     OSVersion          Property   static System.OperatingSystem OSVersion {get;} 
  28.     ProcessorCount     Property   static System.Int32 ProcessorCount {get;} 
  29.     StackTrace         Property   static System.String StackTrace {get;} 
  30.     SystemDirectory    Property   static System.String SystemDirectory {get;} 
  31.     TickCount          Property   static System.Int32 TickCount {get;} 
  32.     UserDomainName     Property   static System.String UserDomainName {get;} 
  33.     UserInteractive    Property   static System.Boolean UserInteractive {get;} 
  34.     UserName           Property   static System.String UserName {get;} 
  35.     Version            Property   static System.Version Version {get;} 
  36.     WorkingSet         Property   static System.Int64 WorkingSet {get;} 
  37.      
  38.     Static Property values for [System.Environment] 
  39.     CommandLine:       : "C:\Users\tfl\Desktop\psp\PowerShellPlus.exe" 
  40.     CurrentDirectory   : C:\Users\tfl\Desktop\psp 
  41.     ExitCode           : 0 
  42.     HasShutdownStarted : False 
  43.     MchineName         : 
  44.     NewLine            : 
  45.  
  46.  
  47.     OSVersion          : Microsoft Windows NT 6.0.6001 Service Pack 1 
  48.     ProcessorCount     : 8 
  49.     SystemDirctory     : C:\Windows\system32 
  50.     TickCount          : 261135752 
  51.     UserDomainName     : COOKHAM 
  52.     UserInteractive    : True 
  53.     UserName           : tfl 
  54.     Version            : 2.0.50727.1434 
  55.  
  56.     Stack Trace 
  57.             { Snipped - Left as an exercise for the reader...} 
  58. #> 
  59.  
  60. ### 
  61. #  Start Script 
  62. ## 
  63.  
  64. # Get and display statics in system.environment class 
  65. "Static properties in [System.Environment]" 
  66. [System.Environment] | Get-Member -Static -MemberType properties 
  67.  
  68. # Now display the values of the simple static properties 
  69. "";"Static Property values for [System.Environment]" 
  70. "CommandLine:       : {0}" -f [System.Environment]::CommandLine 
  71. "CurrentDirectory   : {0}" -f [System.Environment]::CurrentDirectory 
  72. "ExitCode           : {0}" -f [System.Environment]::ExitCOde 
  73. "HasShutdownStarted : {0}" -f [System.Environment]::HasShutdownStarted 
  74. "MchineName         : {0}" -f [System.Environment]::MachineNam 
  75. "NewLine            : {0}" -f [System.Environment]::NewLine 
  76. "OSVersion          : {0}" -f [System.Environment]::OSVersion 
  77. "ProcessorCount     : {0}" -f [System.Environment]::ProcessorCount 
  78. "SystemDirctory     : {0}" -f [System.Environment]::SystemDirectory 
  79. "TickCount          : {0}" -f [System.Environment]::TickCount 
  80. "UserDomainName     : {0}" -f [System.Environment]::UserDomainName 
  81. "UserInteractive    : {0}" -f [System.Environment]::UserInteractive 
  82. "UserName           : {0}" -f [System.Environment]::UserName 
  83. "Version            : {0}" -f [System.Environment]::Version 
  84. "WorkingSet         : {0}" -f [System.Environment]::WorkingSet   
  85.  
  86. # Here is the stack trace: 
  87. "";"Stack Trace:" 
  88. [System.Environment]::StackTrace 
  89. # End of Script 

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 

Saturday, 3 January 2009

Get-MouseInfo.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     Uses Win32_PointingDevice WMI class to return Mouse details.     
  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 Mouse details, then formats that 
  8.     information (using the functions defined at the top of the script. 
  9.      
  10.     While on most systems, there is only one mouse, WMI can return
  11.     multiple devices. Hence the foreach loop. Thanks to Jeffrey
  12.     Snover for the tip-off!  Also note that not all properties
  13.     are returned – although mileage may vary.
  14.      
  15.     This is also sample 7 on http://msdn.microsoft.com/en-us/library/aa394587(VS.85).aspx 
  16.     recoded with PowerShell. 
  17. .NOTES 
  18.     File Name  : Get-Mouseinfo.ps1 
  19.     Author     : Thomas Lee - tfl@psp.co.uk 
  20.     Requires   : PowerShell V2 CTP3 
  21. .LINK 
  22.     Script posted to: 
  23.     http://www.pshscripts.blogspot.com 
  24.     Original MSDN Page 
  25.     http://msdn.microsoft.com/en-us/library/aa394587(VS.85).aspx 
  26. .EXAMPLE 
  27.     PS C:\foo> Get-Mouseinfo.ps1 
  28.     Mouse Information on System: COOKHAM8  (device 1)
  29.     Description            : PS/2 Compatible Mouse 
  30.     Device ID              : ACPI\PNP0F13\4&33DCE2F0&0 
  31.     Device Interface       : PS/2 
  32.     Double Speed Threshold : 
  33.     Handedness             : 
  34.     Hardware Type          : PS/2 Compatible Mouse 
  35.     INF File Name          : msmouse.inf 
  36.     Inf Section            : PS2_Inst 
  37.     Manufacturer           : Microsoft 
  38.     Name                   : PS/2 Compatible Mouse 
  39.     Number of buttons      : 0 
  40.     PNP Device ID          : ACPI\PNP0F13\4&33DCE2F0&0 
  41.     Pointing Type          : Unknown 
  42.     Quad Speed Threshold   : 
  43.     Resolution             : 
  44.     Sample Rate            : 
  45.     Synch                  : 
  46. #> 
  47.  
  48. ### 
  49. # Start of Script 
  50. ### 
  51.  
  52. # Functions to decode details 
  53.  
  54. function Deviceinterface { 
  55. param ($value) 
  56. switch ($value) { 
  57. 0    {"Other"
  58. 1    {"Unknown"
  59. 3    {"Serial"
  60. 4    {"PS/2"
  61. 5    {"Infrared"
  62. 6    {"HP-HIL"
  63. 7    {"Bus Mouse"
  64. 8    {"ADP (Apple Desktop Bus)"
  65. 160  {"Bus Mouse DB-9"
  66. 161  {"Bus Mouse Micro-DIN"
  67. 162  {"USB"
  68.  
  69. function Handedness { 
  70. param ($value) 
  71. switch ($value) { 
  72. 0 {"Unknown"
  73. 1 {"Not Applicable"
  74. 2 {"Right-Handed Operation"
  75. 3 {"Left-Handed Operation"
  76.  
  77. function Pointingtype { 
  78. param ($value) 
  79. switch ($value) { 
  80. 1 {"Other"
  81. 2 {"Unknown"
  82. 3 {"Mouse"
  83. 4 {"Track Ball"
  84. 5 {"Track Point"
  85. 6 {"Glide Point"
  86. 7 {"Touch Pad"
  87. 8 {"Touch Screen"
  88. 9 {"Mouse - Optical Sensor"
  89.  
  90. # Now do script stuff 
  91. # Get Mouse information 
  92. $mice = Get-WmiObject -Class Win32_PointingDevice 
  93. $i=1 
  94.  
  95. foreach ($mouse in $mice) { 
  96.  
  97. # Display details 
  98. "Mouse Information on System: {0} (device {1})" -f $mouse.systemname, $i 
  99. "Description            : {0}" -f $mouse.Description 
  100. "Device ID              : {0}" -f $mouse.DeviceID 
  101. "Device Interface       : {0}" -f (Deviceinterface($mouse.DeviceInterface)) 
  102. "Double Speed Threshold : {0}" -f $mouse.DoubleSpeedThreshold 
  103. "Handedness             : {0}" -f (Handedness($mouse.handedness)) 
  104. "Hardware Type          : {0}" -f $mouse.Hardwaretype 
  105. "INF File Name          : {0}" -f $mouse.InfFileName 
  106. "Inf Section            : {0}" -f $mouse.InfSection 
  107. "Manufacturer           : {0}" -f $mouse.Manufacturer 
  108. "Name                   : {0}" -f $mouse.Name 
  109. "Number of buttons      : {0}" -f $mouse.NumberOfButtons 
  110. "PNP Device ID          : {0}" -f $mouse.PNPDeviceID 
  111. "Pointing Type          : {0}" -f (Pointingtype ($mouse.PointingType)) 
  112. "Quad Speed Threshold   : {0}" -f $mouse.QuadSpeedThreshold 
  113. "Resolution             : {0}" -f $mouse.Resolution 
  114. "Sample Rate            : {0}" -f $mouse.SampleRate 
  115. "Synch                  : {0}" -f $mouse.Synch 
  116. $i++ 
  117. # End of Script 

Friday, 2 January 2009

Get-DateTime.ps1

  1. <# 
  2. .SYNOPSIS 
  3.   Uses .NET formatting strings to format date/time values   
  4. .DESCRIPTION 
  5.   Recreates an MSDN sample for displaying Date/TIme Format. See 
  6.   links section for pointer to original Sample. 
  7. .NOTES 
  8.     File Name  : Get-DateTime.ps1 
  9.     Author     : Thomas Lee - tfl@psp.co.uk 
  10.     Requires   : PowerShell V2 CTP3 
  11. .EXAMPLE 
  12.     PS C:\foo> .\Get-DateTime.ps1 
  13.     FORMAT  en-gb EXAMPLE 
  14.     CHAR    VALUE OF ASSOCIATED PROPERTY, if ANY 
  15.       d     12/28/2008 
  16.             dd/MM/yyyy (ShortDatePattern) 
  17.       D     28 December 2008 
  18.             dd MMMM yyyy (LongDatePattern) 
  19.       f     28 December 2008 16:37 
  20.       F     28 December 2008 16:37:00 
  21.             dd MMMM yyyy HH:mm:ss (FullDateTimePattern) 
  22.       g     28/12/2008 16:37 
  23.       G     28/12/2008 16:37:00 
  24.       m     28 December 
  25.             dd MMMM (MonthDayPattern) 
  26.       M     28 December 
  27.             dd MMMM (MonthDayPattern) 
  28.       o     2008-12-28T16:37:00.4017900+00:00 
  29.       r     Sun, 28 Dec 2008 16:37:00 GMT 
  30.             ddd, dd MMM yyyy HH':'mm':'ss 'GMT' (RFC1123Pattern) 
  31.       R     Sun, 28 Dec 2008 16:37:00 GMT 
  32.             ddd, dd MMM yyyy HH':'mm':'ss 'GMT' (RFC1123Pattern) 
  33.       s     2008-12-28T16:37:00 
  34.             yyyy'-'MM'-'dd'T'HH':'mm':'ss (SortableDateTimePattern) 
  35.       t     16:37 
  36.             HH:mm (ShortTimePattern) 
  37.       T     16:37:00 
  38.             HH:mm:ss (LongTimePattern) 
  39.       u     2008-12-28 16:37:00Z 
  40.             yyyy'-'MM'-'dd HH':'mm':'ss'Z' (UniversalSortableDateTimePattern) 
  41.       U     28 December 2008 16:37:00\n 
  42.       y     December 2008 
  43.             MMMM yyyy (YearMonthPattern) 
  44.       Y     December 2008 
  45.             MMMM yyyy (YearMonthPattern) 
  46. .EXAMPLE 
  47.     PS C:\foo> Get-Help Get-DateTime.ps1 
  48.     Left as an exercise for the reader! 
  49. .LINK 
  50.     Script posted to: 
  51.     http://www.pshscripts.blogspot.com 
  52.     MSDN Sample Page showing C# original 
  53.     http://msdn.microsoft.com/en-us/library/system.globalization.datetimeformatinfo.aspx 
  54. #> 
  55.  
  56. ## 
  57. # Start of Script 
  58. ## 
  59.  
  60. # Create and initialise a DateTimeFormatInfo associated with the en-US culture. 
  61. $culture = [system.Globalization.CultureInfo]::CreateSpecificCulture("en-gb"
  62. $dtfi   = $culture.DateTimeFormat 
  63.  
  64. # Create a DateTime with the Gregorian date January 3, 2002 (year=2002, month=1, day=3). 
  65. # The Gregorian calendar is the default calendar for the en-GB culture. 
  66. $dt = get-date 
  67. "FORMAT  en-gb EXAMPLE" 
  68. "CHAR    VALUE OF ASSOCIATED PROPERTY, IF ANY"  
  69. "  d     {0}       "   -f $DT.ToString("d", $DI)  
  70. "        {0} {1}   "   -f $dtfi.ShortDatePattern, "(ShortDatePattern)"  
  71. "  D     {0}       "   -f $dt.ToString("D", $DTFI)  
  72. "        {0} {1}   "   -f $dtfi.LongDatePattern,  "(LongDatePattern)"  
  73. "  f     {0}       "   -f $dt.ToString("f", $DTFI)  
  74. "  F     {0}       "   -f $dt.ToString("F", $DTFI)  
  75. "        {0} {1}   "   -f $dtfi.FullDateTimePattern, "(FullDateTimePattern)"  
  76. "  g     {0}       "   -f $dt.ToString("g", $DTFI)  
  77. "  G     {0}       "   -f $dt.ToString("G", $DTFI)  
  78. "  m     {0}       "   -f $dt.ToString("m", $DTFI)  
  79. "        {0} {1}   "   -f $dtfi.MonthDayPattern, "(MonthDayPattern)"  
  80. "  M     {0}       "   -f $dt.ToString("M", $DTFI)  
  81. "        {0} {1}   "   -f $dtfi.MonthDayPattern, "(MonthDayPattern)" 
  82. "  o     {0}       "   -f $dt.ToString("o", $DTFI)  
  83. "  r     {0}       "   -f $dt.ToString("r", $DTFI)  
  84. "        {0} {1}   "   -f $dtfi.RFC1123Pattern, "(RFC1123Pattern)"  
  85. "  R     {0}       "   -f $dt.ToString("R", $DTFI)  
  86. "        {0} {1}   "   -f $dtfi.RFC1123Pattern, "(RFC1123Pattern)"  
  87. "  s     {0}       "   -f $dt.ToString("s", $DTFI)  
  88. "        {0} {1}   "   -f $dtfi.SortableDateTimePattern, "(SortableDateTimePattern)"  
  89. "  t     {0}       "   -f $dt.ToString("t", $DTFI)  
  90. "        {0} {1}   "   -f $dtfi.ShortTimePattern, "(ShortTimePattern)"  
  91. "  T     {0}       "   -f $dt.ToString("T", $DTFI)  
  92. "        {0} {1}   "   -f $dtfi.LongTimePattern, "(LongTimePattern)"  
  93. "  u     {0}       "   -f $dt.ToString("u", $DTFI)  
  94. "        {0} {1}   "   -f $dtfi.UniversalSortableDateTimePattern, "(UniversalSortableDateTimePattern)"  
  95. "  U     {0}\n     "   -f $dt.ToString("U", $DTFI)  
  96. "  y     {0}       "   -f $dt.ToString("y", $DTFI)  
  97. "        {0} {1}   "   -f $dtfi.YearMonthPattern, "(YearMonthPattern)"  
  98. "  Y     {0}       "   -f $dt.ToString("Y", $DTFI)  
  99. "        {0} {1}   "   -f $dtfi.YearMonthPattern, "(YearMonthPattern)"  
  100. # End of Script 

Thursday, 1 January 2009

Get-Domain


  1. <# 
  2. .SYNOPSIS 
  3.     Shows use of GetCurrentDomain to return information about the domain 
  4. .DESCRIPTION 
  5.     Uses system.directoryservices.activedirectory.domain and GetCurrentDomain 
  6.     to return domain information of the currently logged on user. Note that this 
  7.     domain may be different to the domain of the computer itself. 
  8. .NOTES 
  9.     File Name  : get-domain.ps1 
  10.     Author     : Thomas Lee - tfl@psp.co.uk 
  11.     Requires   : PowerShell V2 CTP3 
  12. .LINK 
  13.     http://www.pshscripts.blogspot.com 
  14. .EXAMPLE 
  15.     PS c:\foo> .\get-domain.ps1 
  16.     You are connected to the cookham.net domain 
  17.     Role Holders in this domain: 
  18.       PDC Role Holder     :  Cookham1.cookham.net 
  19.       RID Role Holder     :  Cookham1.cookham.net 
  20.       InfraM Role Holder  :  Cookham1.cookham.net 
  21. #> 
  22.  
  23. ### 
  24. # Start of script 
  25. ### 
  26.  
  27. # Get Domain information 
  28. $domain = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()  
  29.  
  30. # Display information    
  31.    "You are connected to the {0} domain" -f $domain.name  
  32.    "Role Holders in this domain:" 
  33.    "  PDC Role Holder     :  {0}" -f $domain.PdcRoleOwner 
  34.    "  RID Role Holder     :  {0}" -f $domain.RIDRoleOwner 
  35.    "  InfraM Role Holder  :  {0}" -f $domain.InfrastructureRoleOwner 
  36. # End of script    

Tuesday, 30 December 2008

Get-StockQuote.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     Gets stock quotes using a web servoice 
  4. .DESCRIPTION 
  5.     This function uses the New-WebServiceProxy cmdlet to 
  6.     create a web service proxy. Then it calls that proxy 
  7.     to get stock information.  
  8. .NOTES 
  9.     File Name  : get-stockquote.ps1 
  10.     Author     : Thomas Lee - tfl@psp.co.uk 
  11.     Requires   : PowerShell V2 CTP3 
  12. .LINK 
  13.    Original post:   
  14.    http://PoshCode.org/embed/752 
  15.    Updated script posted: 
  16.    http://www.pshscripts.blogspot.com 
  17. .INPUTTYPE 
  18.    String(s) representing stock tickers to find 
  19. .RETURNVALUE 
  20.    XML Element, holding stock details 
  21. .EXAMPLE 
  22.     Run from PowerShell Prompt: 
  23.     Get-StockQuote "MSFT" 
  24.     Output - left as an exercise for the reader 
  25. .EXAMPLE 
  26.     Run from Pipeline 
  27.     "IBM","INTC" | Get-StockQuote 
  28.     Output - left as an exercise for the reader 
  29. .PARAMETER TICKER 
  30.     A string, or array of strings representing stock tickers 
  31.     The function gets stock details for each stock ticker provided 
  32. #> 
  33.  
  34. function Get-StockQuote { 
  35. param
  36. [Parameter(Position=0, Mandatory=$FALSE, ValueFromPipeline=$TRUE)]  
  37. [String] $TICKER="MSFT"
  38. process { 
  39.    $ticker 
  40.    $s = new-webserviceproxy -uri http://www.webservicex.net/stockquote.asmx 
  41.    foreach ($symbol in $ticker) { 
  42.       $result = [xml]$s.GetQuote($symbol
  43.       $result.StockQuotes.Stock 
  44.     } # end foreach 
  45. } #end process block 
  46. } # end function 
  47.  
  48.  
  49. "Example 1:" 
  50. "==========" 
  51. Get-StockQuote MSFT 
  52. "" 
  53. "Example 2:" 
  54. "==========" 
  55. "IBM","INTC" | Get-StockQuote 

Monday, 29 December 2008

Validate-EmailAddress.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     Validates an email address using a web service 
  4. .DESCRIPTION 
  5.     This script uses the New-WebServiceProxy cmdlet to 
  6.     create a web service proxy. Then it calls that proxy 
  7.     to Validate an email address.  
  8. .NOTES 
  9.     File Name  : validate-emailaddress.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. .INPUTTYPE 
  16.    String(s) representing email address to validate 
  17. .RETURNVALUE 
  18.    [Boolean] - whether email address was valid 
  19. .EXAMPLE 
  20.     Run from PowerShell Prompt: 
  21.     PS C:\foo> .\Validate-EmailAddress "tfl@psp.co.uk” 
  22.     True 
  23. .EXAMPLE 
  24.     Run from Pipeline: 
  25.     PS C:\foo> "tfl@psp.co.uk", "foo@foo.bar" | .\Validate-EmailAddress 
  26.     True 
  27.     False 
  28. .PARAMETER addr 
  29.     A string, or array of strings representing email addresses to check 
  30. #> 
  31.  
  32. param( 
  33. [Parameter(Position=0, Mandatory=$FALSE, ValueFromPipeline=$TRUE)]  
  34. [String] $Addr = "doctordns@gmail.com" ) 
  35. process { 
  36.    $s = new-webserviceproxy -uri http://www.webservicex.net/validateEmail.asmx 
  37.    foreach ($a in $addr) { 
  38.       $result = $s.IsValidEmail($a
  39.       $result 
  40.     } # end foreach 
  41. } #end process block