Monday, 31 May 2010

Get-SQLServer.ps1

Microsoft.SqlServer.Management.Smo.Server

  1. <# 
  2. .SYNOPSIS 
  3.     This script uses SQL Server Server SMO objects to display server information. 
  4. .DESCRIPTION 
  5.     This script first loads the SMO assembly and then creates a server object. 
  6.     The script then prints basic server information plus details of databases and 
  7.     tables. 
  8. .NOTES 
  9.     File Name  : Get-SQLServer.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/05/get-sqlserverps1.html 
  15.     MSDN Sample posted at: 
  16.        http://msdn.microsoft.com/en-us/library/microsrosoft.management.smo.server.aspx 
  17. .EXAMPLE 
  18.     PS C:\Foo> .\Get-SQLServer.ps1' 
  19.     Server Details 
  20.     -------------- 
  21.     Server Name:      SQL1 
  22.     Product:          Microsoft SQL Server 
  23.     Edition:          Enterprise Edition (64-bit) 
  24.     Type:             Singleton 
  25.     Version:          10.0.2531 
  26.     Version String:   10.0.2531.0 
  27.     Service Account:  .\sql 
  28.   
  29.     Databases and Table 
  30.     ------------------- 
  31.     master (contains 6 tables) 
  32.     model (contains 0 tables) 
  33.     msdb (contains 102 tables) 
  34.     PSMC (contains 1 tables) 
  35.     tempdb (contains 0 tables) 
  36. #>  li class="alt">## 
  37. # Start of Script 
  38. ## 
  39.  
  40. # Load the SMO Assembly 
  41. $null = [system.Reflection.Assembly]::LoadWithPartialName("Microsoft.SQLServer.Smo"
  42.   
  43. # Create Server Object 
  44. $server = New-Object Microsoft.SqlServer.Management.Smo.Server "SQL1" 
  45.   
  46. # Display key properties  
  47. "Server Details" 
  48. "--------------" 
  49. "Server Name:      {0}" -f $Server.Netname 
  50. "Product:          {0}" -f $Server.Product 
  51. "Edition:          {0}" -f $Server.Edition 
  52. "Type:             {0}" -f $Server.ServerType 
  53. "Version:          {0}" -f $Server.Version 
  54. "Version String:   {0}" -f $Server.Versionstring 
  55. "Service Account:  {0}" -f $Server.ServiceAccount 
  56. "" 
  57.   
  58. # Display Database/Table info 
  59. "Databases and Table" 
  60. "-------------------" 
  61. foreach ($database in $server.Databases) { 
  62. "{0} (contains {1} tables)" -f $Database.name, $Database.Tables.Count 

Saturday, 29 May 2010

Replace-String.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script demonstrates how to do .REPLACE() in a string. 
  4. .DESCRIPTION 
  5.     This script is a MSDN sample, recoded in PowerShell 
  6. .NOTES 
  7.     File Name  : Replace-String.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/05/replace-stringps1.html 
  13.     MSDN Sample posted at: 
  14.         http://msdn.microsoft.com/en-us/library/fk49wtc1%28VS.80%29.aspx  
  15. .EXAMPLE 
  16.     PSH [C:\foo]: .\Replace-String.ps1 
  17.     The original string is: 
  18.     This docment uses 3 other docments to docment the docmentation 
  19.      
  20.     After correcting the string, the result is: 
  21.     This document uses 3 other documents to document the documentation 
  22. #> 
  23.  
  24. ## 
  25. # Start of script 
  26. ## 
  27.   
  28. # Create a string complete with misspelling and display it 
  29. $errString = "This docment uses 3 other docments to docment the docmentation" 
  30. "The original string is:`n{0}" -f $errString 
  31. "" 
  32.  
  33. # Correct the spelling of "document" using .Replace() and display  
  34. # the corrected string 
  35. $correctString = $errString.Replace("docment", "document"
  36. "After correcting the string, the result is: `n{0}" -f $correctstring 
  37. # End of script 

Saturday, 22 May 2010

Get-ServiceDetails.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script displays service details 
  4. .DESCRIPTION 
  5.     This script first enumerates all the service controllers (ie services) 
  6.     running on the local system. For each service, we look into WMI and 
  7.     get info about that running service. 
  8. .NOTES 
  9.     File Name  : Get-ServiceDetails.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/05/get-servicedetailsps1.html 
  15.     MSDN Sample posted at: 
  16.         http://msdn.microsoft.com/en-us/library/hde9d63a.aspx   
  17. .EXAMPLE 
  18.     PSH [C:\foo]: .\Get-ServiceDetails.ps1' 
  19.     Services running on the local computer: 
  20.  
  21.       Service :        AeLookupSvc 
  22.         Display name:    Application Experience 
  23.         Start name:      localSystem 
  24.         Description:     Processes application compatibility cache requests for applications as they are launched 
  25.  
  26.       Service :        AppHostSvc 
  27.         Display name:    Application Host Helper Service 
  28.         Start name:      LocalSystem 
  29.         Description:     Provides administrative services for IIS, for example configuration history and Applicati 
  30. on Pool account mapping. If this service is stopped, configuration history and locking down files or directori 
  31. es with Application Pool specific Access Control Entries will not work. 
  32. <rest snipped to save space!> 
  33. #> 
  34.  
  35. ## 
  36. # Start of script 
  37. ## 
  38.   
  39. # Load assembly with ServiceProcess class 
  40. $result = [reflection.Assembly]::LoadWithPartialName("System.ServiceProcess"
  41. $Services = [System.ServiceProcess.ServiceController]::GetServices() 
  42.   
  43. # Get WMI Services 
  44. $WMIServices = gwmi win32_service 
  45. # Display the list of services currently running on this computer. 
  46.   
  47. "Services running on the local computer:" 
  48. foreach ($Service in $Services) { 
  49.   
  50. if ($Service.Status -eq [system.ServiceProcess.ServiceControllerStatus]::Running)  { 
  51.   # Write the service name and the display name 
  52.   # for each running service. 
  53.   "" 
  54.   "  Service :        {0}"      -f  $Service.ServiceName 
  55.   "    Display name:    {0}"    -f  $Service.DisplayName 
  56.   # query WMI for more info on service 
  57.   $svc = $wmiServices  | where {$_.name -eq $service.servicename}   
  58.   "    Start name:      {0}"    -f  $Svc.StartName 
  59.   "    Description:     {0}"    -f  $Svc.Description 
  60. # End 

Friday, 21 May 2010

Write-XmlElementString.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script uses XMLWriter to write an XML Element 
  4. .DESCRIPTION 
  5.     This script creates and uses and XML writer to write an  
  6.     XML Element 
  7. .NOTES 
  8.     File Name  : Write-XxmElementString.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/05/write-xxmelementstringps1.html
  14.     MSDN Sample posted at: 
  15.         http://msdn.microsoft.com/en-us/library/aex0e7zs.aspx  
  16. .EXAMPLE 
  17.     PSH [C:\foo]: .\Write-XmlElementString.ps1' 
  18.     <orderID>1-456-ab</orderID><orderID>2-36-00a</orderID> 
  19. #> 
  20. # Create new XMLWriter settings 
  21. $settings = new-object System.Xml.XmlWriterSettings 
  22. $settings.OmitXmlDeclaration = $true 
  23. $settings.ConformanceLevel = 'Fragment' 
  24. $settings.CloseOutput = $false
  25.  
  26. # Create the XmlWriter object and write some content. 
  27. #$strm = new-object system.io.MemoryStream 
  28. $writer = [system.xml.XmlWriter]::Create("c:\foo\data.xml", $settings
  29. $writer.WriteElementString("orderID", "1-456-ab"
  30. $writer.WriteElementString("orderID", "2-36-00a"
  31. $writer.Flush() 
  32. $writer.Close() 
  33.  
  34. # Display data 
  35. cat c:\foo\data.xml 
  36. # End Script 

Write-XML.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script uses XMLWriter to write XML 
  4. .DESCRIPTION 
  5.     This script creates and XML writer and writes some basic 
  6.     XML. 
  7. .NOTES 
  8.     File Name  : Write-XML.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/05/write-xmlps1.html 
  14.     MSDN Sample posted at: 
  15.        http://msdn.microsoft.com/en-us/library/system.xml.xmlwritersettings.aspx  
  16. .EXAMPLE 
  17.     PSH [C:\foo]: .\Write-XML.ps1' 
  18.     <?xml version="1.0" encoding="IBM437"?> 
  19.     <order 
  20.       orderID="367A54" 
  21.       date="2001-05-03"
  22.       <price>19.95</price> 
  23.     </order>PSH [C:\foo]: 
  24. #> 
  25. # Create a new writer settings object 
  26. # And set settings 
  27. $settings = New-Object system.Xml.XmlWriterSettings 
  28. $settings.Indent = $true 
  29. $settings.OmitXmlDeclaration = $false 
  30. $settings.NewLineOnAttributes = $true 
  31.  
  32. # Create a new Writer 
  33. $writer = [system.xml.XmlWriter]::Create([system.console]::Out, $settings
  34.  
  35. #Write some XML 
  36. $writer.WriteStartElement("order"
  37. $writer.WriteAttributeString("orderID", "367A54"
  38. $writer.WriteAttributeString("date", "2001-05-03"
  39. $writer.WriteElementString("price", "19.95"
  40. $writer.WriteEndElement() 
  41. # Flush the writer (and close the file)     
  42. $writer.Flush() 
  43. $writer.Close() 

Wednesday, 19 May 2010

Start-Process2.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script starts up a process using System.Diagnostics.Process  
  4. .DESCRIPTION 
  5.     This script creates 7 occurences of IExplore.exe, using 
  6.     diffrent start up options, based on a sample script in MSDN. 
  7. .NOTES 
  8.     File Name  : Start-process2.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/05/start-process2ps1.html
  14.     MSDN Sample posted at: 
  15.         http://msdn.microsoft.com/en-us/library/0w4h05yb.aspx  
  16. .EXAMPLE 
  17.     PSH [C:\foo]: .\Start-Process2.ps1' 
  18.     Starting IE at home page (p1) 
  19.     Opening Favourites (p2) 
  20.     Opening PSHScripts (p3) 
  21.     Opening c:\foo\myfile.htm (p4) 
  22.     Opening c:\foo\myfile.asp (p5) 
  23.     Opening with Start info (p6) 
  24.     Opening with startinfo + www.reskit.net (p7) 
  25. #> 
  26.  
  27. # Helper Functions 
  28.  
  29. # Opens the Internet Explorer application. 
  30. function OpenApplication { 
  31. param([string] $FavoritesPath) 
  32.  
  33. # Start Internet Explorer. Defaults to the home page. 
  34. "Starting IE at home page (p1)" 
  35. $proc1 = [System.Diagnostics.Process]::start("IExplore.exe"); 
  36. # Display the contents of the favorites folder in the browser. 
  37. "Opening Favourites (p2)" 
  38. $proc2=[System.Diagnostics.Process]::Start($FavoritesPath); 
  39. } 
  40.  
  41. # Opens urls and .html documents using Internet Explorer. 
  42. function OpenWithArguments {  
  43. # url's are not considered documents. They can only be opened 
  44. # by passing them as arguments. 
  45. "Opening PSHScripts (p3)" 
  46. $proc3 = [System.Diagnostics.Process]::Start("IExplore.exe", "www.pshscripts.blogspot.com"
  47.  
  48. # Start a Web page using a browser associated with .html and .asp files. 
  49. "Opening c:\foo\myfile.htm (p4)" 
  50. $proc4 = [System.Diagnostics.Process]::Start("IExplore.exe", "C:\foo\myFile.htm"
  51. "Opening c:\foo\myfile.asp (p5)" 
  52. $proc5 = [System.Diagnostics.Process]::Start("IExplore.exe", "C:\foo\myFile.asp"
  53.  
  54. # Uses the ProcessStartInfo class to start new processes, 
  55. # both in a minimized mode. 
  56. function OpenWithStartInfo { 
  57. $StartInfo = New-Object System.Diagnostics.ProcessStartInfo "IExplore.exe" 
  58. $StartInfo.WindowStyle = [System.Diagnostics.ProcessWindowStyle]::Minimized; 
  59. "Opening with Start info (p6)" 
  60. $proc6 = [System.Diagnostics.Process]::Start($StartInfo
  61. "Opening with startinfo + www.reskit.net (p7)" 
  62. $StartInfo.Arguments = "www.reskit.net"
  63. $proc7 = [System.Diagnostics.Process]::Start($startInfo
  64.  
  65. ## 
  66. # Start of script 
  67. ## 
  68.  
  69. # First, get Favourites path 
  70. $FavoritesPath = [system.Environment]::GetFolderPath('Favorites'
  71.  
  72. # Open two windows - one pointing to IE's home page, the other 
  73. # to my favourites folder (p1, p2) 
  74. OpenApplication $FavoritesPath 
  75.  
  76. # Here call function to open pshscripts, c:\foo\myfile.htm c:\foo\myfile.asp 
  77. #p3, p3, p5 
  78. OpenWithArguments 
  79.  
  80. #Here open two processes using Startinfo object 
  81. # p6, p7 
  82. OpenWithStartInfo 
  83. #End of script 

Tuesday, 18 May 2010

Start-Process.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script creates and starts a Process using .NET 
  4. .DESCRIPTION 
  5.     This script Creates a process object and sets 
  6.     the executable to notepad. The script then starts 
  7.     the process. 
  8. .NOTES 
  9.     File Name  : Start-Process.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/05/start-processps1.html
  15.     MSDN Sample posted at: 
  16.         http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx  
  17. .EXAMPLE 
  18.    When this script runs - you see a copy of notepad popup. 
  19. #> 
  20.  
  21. ## 
  22. # Start of Script 
  23. ## 
  24.  
  25. # Create a new process object 
  26. $Process = new-object System.Diagnostics.Process  
  27. try  { 
  28. $Process.StartInfo.UseShellExecute = $false 
  29.  
  30. # Pick process to start  now - for excitement, use notepad.exe 
  31. $Process.StartInfo.FileName = "C:\windows\system32\notepad.exe" 
  32.  
  33. # Start process  
  34. $Process.Start() 
  35. catch { 
  36.   " Error:";$Error[0]  

Sunday, 2 May 2010

New-MailAddress.ps1


  1. <# 
  2. .SYNOPSIS 
  3.     This script demonstrates the constructors for system.net.mail.mailaddress  
  4. .DESCRIPTION 
  5.     This script uses the three constructors for new objects of the type 
  6.     system.net.mail.mailaddress using PowerShell. These constructors are: 
  7.     . single string 
  8.     . two strings 
  9.     . two strings and an encoding.  
  10. .NOTES 
  11.     File Name  : New-MailAddress.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.     MSDN Sample posted at: 
  18.         http://pshscripts.blogspot.com/2010/05/get-utf8encoding.html 
  19. .EXAMPLE 
  20. PSH [C:\foo]: .\New-MailAddress.ps1' 
  21.     Creating mail address for: tfl@psp.co.uk 
  22.     User Host      Display Name To String 
  23.     ---- ----      ------------ --------- 
  24.     tfl  psp.co.uk              tfl@psp.co.uk 
  25.  
  26.     Creating mail address for: Thomas Lee <tfl@psp.co.uk> 
  27.     User Host      Display Name To String 
  28.     ---- ----      ------------ --------- 
  29.     tfl  psp.co.uk Thomas Lee   "Thomas Lee" <tfl@psp.co.uk> 
  30.  
  31.     Creating mail address for: "Thomas Lee" and "tfl@psp.co.uk" 
  32.     User Host      Display Name To String 
  33.     ---- ----      ------------ --------- 
  34.     tfl  psp.co.uk Thomas Lee   "Thomas Lee" <tfl@psp.co.uk> 
  35.    Creating mail address for: "Thomas Lee","tfl@psp.co.uk", and Ascii encoding 
  36.     User Host      Display Name    To String 
  37.     ---- ----      ------------    --------- 
  38.     tfl  psp.co.uk ThOmas Lee - πΣ "ThOmas Lee - πΣ" <tfl@psp.co.uk> 
  39.  
  40.     Creating mail address for: "Thomas Lee","tfl@psp.co.uk", and UTF8 encoding 
  41.     User Host      Display Name    To String 
  42.     ---- ----      ------------    --------- 
  43.     tfl  psp.co.uk ThOmas Lee - πΣ "ThOmas Lee - πΣ" <tfl@psp.co.uk> 
  44. #> 
  45. ## 
  46. # Start of script 
  47. ## 
  48. # Define helper functions 
  49. # nma - creates a new mail address 
  50. # uses single string constuctor 
  51. function nma { 
  52. param ([string] $addr
  53. new-object system.net.Mail.MailAddress $addr 
  54. # nma2 - creates a new mail address 
  55. # uses twin string constuctor 
  56. function nma2 { 
  57. param ([string] $addr
  58.        [string] $dspnam 
  59.        ) 
  60. new-object system.net.Mail.MailAddress $addr, $dspnam 
  61. # nma3 - creates a new mail address 
  62. # uses twin string+encoding constuctor 
  63. function nma3 { 
  64. param ([string] $addr, [string] $dspnam, $enc
  65. new-object system.net.Mail.MailAddress $addr, $dspnam, $enc 
  66.  
  67. # Create hash tables for display of email addresses 
  68. $ts=@{label="To String";    expression={$_.tostring()}} 
  69. $dn=@{label="Display Name"; expression={$_.displayname}} 
  70.  
  71. # Look at single string constructor usage 
  72. # Create and display for address: tfl@psp.co.uk 
  73. $ad="tfl@psp.co.uk"  
  74. "Creating mail address for: $ad" 
  75. nma  $ad | ft user,host,$dn,$ts -a 
  76.  
  77. # Create and display for address: Thomas Lee <tfl@psp.co.uk> 
  78. $ad = "Thomas Lee <tfl@psp.co.uk>" 
  79. "Creating mail address for: $ad" 
  80. nma  $ad | ft user,host,$dn,$ts -a 
  81.  
  82. # Twin string constructor 
  83. # Create and display for address: tfl@psp.co.uk, "Thomas Lee" 
  84. $ad   = "tfl@psp.co.uk" 
  85. $dnam = "Thomas Lee" 
  86. "Creating mail address for: `"$dname`" and `"$ad`"" 
  87. nma2  $ad $dnam | ft user,host,$dn,$ts -a 
  88.  
  89. # Using 3rd constructor - two strings and  and encoding 
  90. # Create and display for address: tfl@psp.co.uk, "Thomas Lee" in Ascii 
  91. $c=[char]0xd8 
  92. $ad   = "tfl@psp.co.uk" 
  93. $dnam = "Th"+$c+"mas Lee - " + [char] [char]0x03a0 + [char] 0x03a3 
  94. $enc  = New-Object System.Text.AsciiEncoding   
  95. "Creating mail address for: `"$dname`",`"$ad`", and Ascii encoding" 
  96. nma3  $ad $dnam $enc | ft user,host,$dn,$ts -a 
  97.  
  98. # Create and display for address: tfl@psp.co.uk, "Thomas Lee" in UTF8 
  99. $c=[char]0xd8 
  100. $ad   = "tfl@psp.co.uk" 
  101. $dnam = "Th"+$c+"mas Lee - " + [char] [char]0x03a0 + [char] 0x03a3 
  102. $enc  = New-Object System.Text.utf8encoding   
  103. "Creating mail address for: `"$dname`",`"$ad`", and UTF8 encoding" 
  104. nma3  $ad $dnam $enc | ft user,host,$dn,$ts -a 

Get-UTF8Encoding

  1. <# 
  2. .SYNOPSIS 
  3.     This script implements an MSDN Encoding example in PowerShell  
  4. .DESCRIPTION 
  5.     This script creates an encoding plus a Unicode string then 
  6.     manipulates it as per the C# sample. 
  7. .NOTES 
  8.     File Name  : Get-UTF8Encoding 
  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/05/get-utf8encoding.html 
  14.     MSDN Sample posted at: 
  15.         http://msdn.microsoft.com/en-us/library/system.text.utf8encoding.aspx  
  16. .EXAMPLE 
  17.     PSH [C:\foo]: .\Get-UTF8Encoding.ps1
  18.     Original string:This unicode string contains two characters with
  19.     codes outside an 8-bit code range, Pi (Ï€) and Sigma (Σ). 
  20.     Encoded bytes: 
  21.     <long table omitted> 
  22.     Decoded bytes: 
  23.     This unicode string contains two characters with codes outside an
  24.     8-bit code range, Pi (Ï€) and Sigma (Σ). 
  25. #> 
  26. #  Start of Script 
  27. # Create a UTF-8 encoding. 
  28. $utf8 = New-Object System.Text.utf8encoding 
  29. $unicodeString
  30.             "This unicode string contains two characters "
  31.             "with codes outside an 8-bit code range, "
  32.             "Pi (" + [char] 0x03a0 + ") and Sigma (" + [char] 0x03a3 + ")." 
  33. "Original string:{0}" -f $unicodeString 
  34.  
  35. # Encode the string. 
  36. $encodedBytes = $utf8.GetBytes($unicodeString
  37. "" 
  38. "Encoded bytes:" 
  39. foreach($b in $encodedBytes) {"[{0}]" -f $b
  40.  
  41. # Decode bytes back to string 
  42. # Notice Pi and Sigma characters are still present 
  43. $decodedString = $utf8.GetString($encodedBytes
  44. "";"Decoded bytes:" 
  45. $decodedString    
  46. # End of Script