Saturday, 31 July 2010

New-BigInteger.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script creates and displays a BigInteger.  
  4. .DESCRIPTION 
  5.     This script is a rewrite of an MSDN sample. 
  6. .NOTES 
  7.     File Name  : New-BigInteger.ps1 
  8.     Author     : Thomas Lee - tfl@psp.co.uk 
  9.     Requires   : PowerShell Version 2.0 
  10.                  .NET Framework 4    
  11. .LINK 
  12.     This script posted to: 
  13.         http://pshscripts.blogspot.com/2010/07/new-bigintegerps1.html 
  14.     MSDN Sample posted at: 
  15.         http://msdn.microsoft.com/en-us/library/system.numerics.biginteger.aspx  
  16. .EXAMPLE 
  17.     PSH [c:\foo]: .\Get-BigInteger.ps1 
  18.     Big Integer from 179032.6541: 
  19.     179,032 
  20.     Big Integer from 1234934157136952: 
  21.     1,234,934,157,136,952 
  22. #> 
  23.  
  24. # Add the .NET Version 4 System.Numerics.DLL 
  25. Add-Type -Path "C:\Windows\Microsoft.NET\Framework\v4.0.30319\System.Numerics.dll" 
  26.  
  27. # Create first big integer then display it nicely 
  28. $BigIntFromDouble = New-Object System.Numerics.BigInteger 179032.6541 
  29. "Big Integer from 179032.6541:" 
  30. $BigIntFromDouble.ToString("N0"
  31.  
  32. #Create second big integer then display it nicely 
  33. $BigIntFromInt64 = New-object System.Numerics.BigInteger 1234934157136952 
  34. "Big Integer from 1234934157136952:" 
  35. $Bigintfromint64.tostring("N0"

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 

Sunday, 18 July 2010

Get-Performance.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script displays basic performance information for a computer 
  4. .DESCRIPTION 
  5.     This script calls Get-Counter on a computer to obtain default  
  6.     performance counters. These are obtained and displayed. The script 
  7.     takes a parameter which is the host name to display. 
  8. .NOTES 
  9.     File Name  : Get-Performance.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-Performance.ps1 
  17.     Basic Performance for system: Cookham8 
  18.      Network Bytes Per Second (2 adapters) 
  19.        broadcom netxtreme gigabit etherne          0.00 kbps 
  20.        netgear fa311v2 pci adapter                 8.15 kbps 
  21.      CPU Usage                                     1.93 % 
  22.      Committed Bytes                              30.61 mb 
  23.      Cache Faults                                  0.00 p/s 
  24.      Percent Disk Time                            34.15 % 
  25.      Disk Queue length                             0.00 
  26. .EXAMPLE 
  27.     PSH [C:\foo]: .\Get-Performance cookham2 
  28.     Basic Performance for system: cookham2 
  29.      Network Bytes Per Second (6 adapters) 
  30.        public                                     25.31 kbps 
  31.        broadcom netxtreme 57xx gigabit co         26.20 kbps 
  32.        internal                                    0.16 kbps 
  33.        isatap.cookham.net                          0.00 kbps 
  34.        teredo tunneling pseudo-interface           0.00 kbps 
  35.        isatap.{0b3c0adc-3418-4842-8b4e-cf          0.00 kbps 
  36.      CPU Usage                                     0.39 % 
  37.      Committed Bytes                              45.80 mb 
  38.      Cache Faults                                  0.00 p/s 
  39.      Percent Disk Time                             0.00 % 
  40.      Disk Queue length                             0.00 
  41. .PARAMETER comp 
  42.     The name of the computer to display. Default is localhost. 
  43. #> 
  44.  
  45. param
  46. [string] $comp = "localhost" 
  47.  
  48. # Get counters 
  49. $counter = Get-Counter -computername $comp 
  50.  
  51. #Display results 
  52. "Basic Performance for system: {0}" -f $comp 
  53.  
  54. # Network interface
  55. $c= $counter.countersamples | where {$_.path -match "network Interface"
  56. " Network Bytes Per Second ({0} adapters)" -f $c.count 
  57. foreach ($ni in $c){ 
  58.   $nin = if ($ni.instancename.length -le 34)  {$ni.InstanceName} 
  59.          else {$ni.instancename.substring(0,34)} 
  60.  
  61.  "   {0,-35} {1,12:n2} kbps" -f $nin,$($ni.cookedvalue/1kb) 
  62.  
  63. # CPU Usage 
  64. $c= $counter.countersamples | where {$_.path -match "processor time"
  65. " CPU Usage           {0,30:n2} %" -f $c.cookedvalue 
  66.  
  67. # Memory 
  68. $cb = ($counter.countersamples | where {$_.Path -match "bytes in use"}).cookedvalue 
  69. $cf = ($counter.countersamples | where {$_.Path -match "cache"}).cookedvalue 
  70. " Committed Bytes     {0,30:n2} mb" -f $cb 
  71. " Cache Faults        {0,30:n2} p/s" -f $cf 
  72.  
  73. # Disk 
  74. $dt =  ($counter.countersamples | where {$_.Path -match "disk time"}).cookedvalue 
  75. $dql = ($counter.countersamples | where {$_.Path -match "queue"}).cookedvalue 
  76. " Percent Disk Time     {0,30:p2}" -f $dt 
  77. " Disk Queue Length   {0,30:n2}" -f $dql 

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. .EXAMPLE 
  18.    PSH [C:\foo]: .\Get-Driveinfo.ps1 
  19.    Drive C:\ 
  20.       File type: Fixed 
  21.       Volume label: 
  22.       File system: NTFS 
  23.       Available space to current user:           6.75 gb 
  24.       Total available space:                     6.75 gb 
  25.       Total size of drive:                     48.828 gb 
  26.    Drive D:\ 
  27.       File type: Fixed 
  28.       Volume label: 
  29.       File system: FAT32 
  30.       Available space to current user:           1.81 gb 
  31.       Total available space:                     1.81 gb 
  32.       Total size of drive:                      2.003 gb 
  33.       <ETC...> 
  34. #> 
  35.  
  36. # Get Drive Info 
  37. $allDrives = [system.IO.DriveInfo]::GetDrives() 
  38.  
  39. # Now display details 
  40. foreach ($d in $allDrives)  { 
  41.     "Drive {0}" -f $d.Name 
  42.      "  File type: {0}" -f $d.DriveType 
  43.     if ($d.IsReady)  { 
  44.          "  Volume label: {0}" -f $d.VolumeLabel 
  45.          "  File system: {0}"  -f $d.DriveFormat 
  46.          $fs  = $d.AvailableFreeSpace/1gb 
  47.          $tfs = $d.TotalFreeSpace/1gb 
  48.          $TS  = $d.TotalSize/1gb 
  49.          "  Available space to current user:{0, 15:n2} gb" -f $fs 
  50.          "  Total available space:          {0, 15:n2} gb" -f $tfs 
  51.          "  Total size of drive:            {0, 15:n3} gb" -f $ts 
  52.     } 

Friday, 16 July 2010

Set-SQLServerOption.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script sets options on a SQL Server using SMO 
  4. .DESCRIPTION 
  5.     This script first loads the SQL cmdlet and provider snapin and 
  6.     displays information about the SQL Server. The script then sets 
  7.     two server options and alters to database to persist the changes. 
  8. .NOTES 
  9.     File Name  : Set-SQLServerOption.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]: .\Set-SQLServerOption.ps1 
  17.     Network Name   SQL1 
  18.     Instance Name   
  19.     OS Version     6.1 (7600) 
  20.     SQL Edition    Enterprise Edition (64-bit) 
  21.     Settings State Existing 
  22. #> 
  23.  
  24. # Load the SMO Objects 
  25. $null = Add-PSSnapIn SqlServerCmdletSnapin100   -erroraction silentlycontinue 
  26. $null = Add-PSSnapIn SqlServerProviderSnapin100 -erroraction silentlycontinue 
  27.  
  28. # Set the path context to the local, default instance of SQL Server. 
  29. CD sqlserver:\sql\localhost\ 
  30. $srv = get-item default 
  31.  
  32. # Display information about the instance of SQL Server and settings state 
  33. "Network Name   {0}"  -f $srv.NetName 
  34. "Instance Name  {0}"  -f $srv.InstanceName 
  35. "OS Version     {0}"  -f $srv.Information.OSVersion 
  36. "SQL Edition    {0}"  -f $srv.Edition 
  37. "Settings State {0}"  -f $srv.Settings.State.ToString() 
  38.  
  39. # Modify LoginMode settings 
  40. $srv.Settings.LoginMode = [Microsoft.SqlServer.Management.SMO.ServerLoginMode]::Integrated 
  41.  
  42. # Modify settings specific to the current connection in UserOptions 
  43. $srv.UserOptions.AbortOnArithmeticErrors = $true 
  44.  
  45. # Run the Alter method to make the changes on the instance of SQL Server 
  46. $srv.Alter() 
Technorati Tags: ,,,

Thursday, 15 July 2010

Copy-File2.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script creates a file, then reads it and displays the output. 
  4. .DESCRIPTION 
  5.     This script implements the MSDN sample for this page. It first creates a file (assuming the file 
  6.     does not already exist) and writes three lines to it. The script then opens the file, reads each 
  7.     line and displays it.    
  8.      
  9. .NOTES 
  10.     File Name  : Copy-File2.ps1 
  11.     Author     : Thomas Lee - tfl@psp.co.uk 
  12.     Requires   : PowerShell Version 2.0 
  13. .LINK 
  14.     This script posted to: 
  15.         http://pshscripts.blogspot.com/2010/07/copy-file2ps1.html
  16.     MSDN Sample posted at: 
  17.         http://msdn.microsoft.com/en-us/library/system.io.file.aspx        
  18.         http://msdn.microsoft.com/en-us/library/system.io.file.createtext.aspx
  19.         http://msdn.microsoft.com/en-us/library/system.io.file.opentext.aspx
  20. .EXAMPLE 
  21.     PSH [C:\foo]: . 'E:\PowerShellScriptLib\System.Io.File\Copy-File2.PS1' 
  22.     Hello 
  23.     And 
  24.     Welcome  
  25. #> 
  26.    
  27. # Set name of file to use for this script 
  28. $path = "c:\foo\MyTest.txt" 
  29.    
  30. # Create the file if it does not already exist 
  31. if (![System.IO.File]::Exists($path))  { 
  32.   $sw = [System.Io.File]::CreateText($path
  33.   $sw.WriteLine("Hello"
  34.   $sw.WriteLine("And"
  35.   $sw.WriteLine("Welcome"
  36.    
  37. # Open the file to read from and set $s to an empty string 
  38. $sr = [System.Io.File]::OpenText($path
  39. $s = ""
  40.  
  41. # Loop through the file, line at a time and display the output 
  42. while (($s = $sr.ReadLine()) -ne $null)  { 
  43.   $s 
  44.   
  45. # And close the reader/writer 
  46. $sw.Close() 
  47. $sr.Close() 

Wednesday, 14 July 2010

Get-AssemblyDetails.ps1

  1. # 
  2. .SYNOPSIS 
  3.     This script demonstrates the Assembly.GetType method. 
  4. .DESCRIPTION 
  5.     This script creates an int32, then calls into .NET to find  
  6.     the assembly that the int32 class comes from. The assembly 
  7.     details are then output. This script is a re-work of an MSDN 
  8.     code sample. 
  9. .NOTES 
  10.     File Name  : Get-AssemblyDetails.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.reflection.assembly.getassembly.aspx  
  18. .EXAMPLE 
  19.     PSH [C:\foo]: .\Get-AssemblyDetails.ps1' 
  20.     CodeBase=: file:///C:/Windows/Microsoft.NET/Framework64/v2.0.50727/mscorlib.dll 
  21. #> 
  22.  
  23. # Set the Type instance to the target class type. 
  24. $Integer1 = New-Object System.Int32 
  25. $Type1 = $Integer1.GetType(); 
  26.  
  27. # Instantiate an Assembly class to the assembly housing the Integer type.   
  28. $SampleAssembly = [System.Reflection.Assembly]::GetAssembly($Integer1.GetType()) 
  29.  
  30. # Gets the location of the assembly using file: protocol. 
  31. "CodeBase=: {0}" -f $SampleAssembly.CodeBase 

Tuesday, 13 July 2010

Say-HelloWorld.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script uses the Speech API to say "Hello World" 
  4. .DESCRIPTION 
  5.     This script first loads the Speech API, then creates a  
  6.     SpeechSynthesiser object. It uses this object's Speak method 
  7.     to speak the time honoured phrase "hello world." 
  8. .NOTES 
  9.     File Name  : Say-HelloWorld.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/ms586901.aspx 
  17. .EXAMPLE 
  18.     Left as an exercise for the reader. 
  19. #> 
  20. # First load the dll 
  21. [System.Reflection.Assembly]::LoadWithPartialName("System.Speech") | out-null 
  22. $spk = New-Object system.Speech.Synthesis.SpeechSynthesizer 
  23.   
  24. # Now say "Hello world" 
  25. $spk.Speak("Hello world"

Monday, 12 July 2010

Get-InstalledVoice.ps1


  1. <# 
  2. .SYNOPSIS 
  3.     This script displays the installed Speech Synthesiser voices 
  4. .DESCRIPTION 
  5.     This script first loads the System.Speech DLL, then 
  6.     creates a new SpeechSynthesizer object. It uses this 
  7.     object to enumerate the installed speech voices. 
  8. .NOTES 
  9.     File Name  : Get-InstalledVoice.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.speech.synthesis.installedvoice%28VS.90%29.aspx 
  17. .EXAMPLE 
  18.     PSH [C:\foo]: .\Get-InstalledVoice.ps1 
  19.  
  20.     Gender                : Female 
  21.     Age                   : Adult 
  22.     Name                  : Microsoft Anna 
  23.     Culture               : en-US 
  24.     Id                    : MS-Anna-1033-20-DSK 
  25.     Description           : Microsoft Anna - English (United States) 
  26.     SupportedAudioFormats : {System.Speech.AudioFormat.SpeechAudioFormatInfo} 
  27.     AdditionalInfo        : {[Age, Adult], [AudioFormats, 18], [Gender, Female], [Language, 409]...} 
  28. #> 
  29. # 
  30. # First load the dll 
  31. $r = [system.Reflection.Assembly]::LoadWithPartialName("system.speech"
  32.  
  33. # Create a speech synthesizer object and enumerate voices 
  34. $spk = New-Object system.Speech.Synthesis.SpeechSynthesizer 
  35. $spk.GetInstalledVoices() | %{$_.voiceinfo}