Thursday, 24 December 2009

PowerShell V2 Download

This blog is mainly about PowerShell scripts. But from now on all scripts will be V2.0 and beyond. I have tested most of the scripts in the script library against V2 and will announce when that work is completed. In the meantime – if you are not running PowerShell V2 because you are on older OSs, help it is at hand.

Of course, if you are running Windows 7 or Windows Server 2008 R2, then you have PowerShell installed by default. On Windows 7, both the PowerShell console and PowerShell ISE are installed by default. On Server 2008 R2, PowerShell console is installed by default, whilst the PowerShell ISE is a feature you can add using Server Manager. And of course, for any Server 2008 R2 core installations, you would need to add both the .NET Framework and PowerShell – virtually nothing extra gets added to Server Core!

For down level (aka older) versions of Windows, Microsoft has released a KB article which directs you to versions you can download. Go to the page http://support.microsoft.com/kb/968929 where you can find PowerShell V2 for Windows XP, Vista, Server 2003 and Server 2008 (RTM) – in both 32-bit and 64-bit versions. Note that in Server 2008 RTM, PowerShell is not supported in Server Core.

In addition to PowerShell V2, the downloads also include the RTM version of Windows Remote Management (WinRM). WinRM is Microsoft’s  implementation of the standards based WS-Management Protocol. WinRM is based on Simple Object Access Protocol (SOAP), and provides a firewall-friendly protocol enabling hardware and operating systems from different vendors to interoperate.

If you are are using an older OS, i.e. Windows 2000 or earlier, then there is no supported version of PowerShell for you. You can probably hack some or even most of PowerShell V2 into those older OSs. But things may not work – and it’s not ever going to be supported.


Sunday, 13 December 2009

Get-LocaleCurrency.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script displays a number formatted in Currency for each locale 
  4. .DESCRIPTION 
  5.     This script first creates a value to be formatted, and creates an array 
  6.     containing all the locales defined on the system. The script then uses  
  7.     each locale to format the value as currency. 
  8. .NOTES 
  9.     File Name  : Get-LocaleCurrency.ps1 
  10.     Author     : Thomas Lee - tfl@psp.co.uk 
  11.     Requires   : PowerShell V2 
  12. .LINK 
  13.     This script posted to: 
  14.         http://www.pshscripts.blogspot.com 
  15. .EXAMPLE 
  16.     Left as an exercise for the reader! NB: the output will look better 
  17.     is this script is run in PowerShell ISE vs PowerShell console. 
  18.      
  19. #> 
  20. ## 
  21. # Start of script 
  22. ## 
  23.   
  24. #Create a value to be formatted 
  25. [int] $Value = 100 
  26.  
  27. # get all hte locales defined in the system 
  28. $L=[system.Globalization.CultureInfo]::GetCultures('AllCultures') | sort lcid 
  29.  
  30. foreach ($C in $L) { 
  31.  
  32. $C=New-Object System.Globalization.CultureInfo $C.Name 
  33. if (!$C.IsNeutralCulture){ 
  34.    "{0,-50} {1,-6}  {2}" -f $C.Displayname,$C.Name,$Value.ToString("C",$c
  35.    } 

Saturday, 14 November 2009

Protect-ByteArray.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script encrpyts then decrypts a byte string 
  4. .DESCRIPTION 
  5.     This script uses System.Security to encrpyt a byte 
  6.     string, then decrypts it. 
  7. .NOTES 
  8.     File Name  : Protect-ByteArray.ps1 
  9.     Author     : Thomas Lee - tfl@psp.co.uk 
  10.     Requires   : PowerShell V2 
  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.protectedmemory.aspx 
  16. .EXAMPLE 
  17.     PSH [C:\foo]: .Protect-ByteArray.ps1' 
  18.     Unencrpyted byte string: 
  19.     1 
  20.     2 
  21.     3 
  22.     4 
  23.     1 
  24.     2 
  25.     3 
  26.     4 
  27.     1 
  28.     2 
  29.     3 
  30.     4 
  31.     1 
  32.     2 
  33.     3 
  34.     6 
  35.     Encrpyted byte string: 
  36.     199 
  37.     52 
  38.     177 
  39.     169 
  40.     162 
  41.     117 
  42.     118 
  43.     127 
  44.     180 
  45.     16 
  46.     230 
  47.     70 
  48.     19 
  49.     89 
  50.     85 
  51.     168 
  52.     Unencrpyted byte string: 
  53.     1 
  54.     2 
  55.     3 
  56.     4 
  57.     1 
  58.     2 
  59.     3 
  60.     4 
  61.     1 
  62.     2 
  63.     3 
  64.     4 
  65.     1 
  66.     2 
  67.     3 
  68.     6 
  69. #> 
  70.    
  71. ## 
  72. # Start of script 
  73. ## 
  74.   
  75. # Load System.Security  
  76. [void] [Reflection.Assembly]::LoadWithPartialName("System.Security") 
  77.   
  78. # Create and display a byte string 
  79. [byte[]] $Secret =  1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3,6 
  80. "Unencrpyted byte string:" 
  81. $Secret 
  82.   
  83. # now encrypt it and display the encrpyted string 
  84. [System.Security.Cryptography.ProtectedMemory]::Protect($Secret,[System.Security.Cryptography.MemoryProtectionScope]::SameLogon) 
  85. "Encrpyted byte string:" 
  86. $Secret 
  87.   
  88. # Now decrypt it and re-display it - it's the same byte array we started with 
  89. [System.Security.Cryptography.ProtectedMemory]::UnProtect($Secret,[System.Security.Cryptography.MemoryProtectionScope]::SameLogon) 
  90. "Unencrpyted byte string:" 
  91. $Secret 
  92. #End of Script 

Thursday, 29 October 2009

Restart-DNS.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script restarts the DNS Service on a Remote System 
  4. .DESCRIPTION 
  5.     This script uses WMI to reach out and restart the DNS 
  6.     DNS service on a remote machine. in my home environment, 
  7.     I find the DNS service on the home DC fails and needs  
  8.     to be restarted - this script works! 
  9. .NOTES 
  10.     File Name  : Restart-DNS.ps1 
  11.     Author     : Thomas Lee - tfl@psp.co.uk 
  12.     Requires   : PowerShell V2 
  13. .LINK 
  14.     This script posted to: 
  15.         http://pshscripts.blogspot.com/2009/10/restart-dnsps1.html
  16. .EXAMPLE 
  17.     PSH [C:\foo]: .\Restart-DNS.ps1 
  18.     Restarting DNS Service on: Cookham1 
  19.     DNS Service stopped 
  20.     DNS Service started 
  21. #> 
  22. ## 
  23. # Start Script 
  24. ## 
  25.  
  26. # Set server to reset and display our intention 
  27. $Server = "Cookham1" 
  28. "Restarting DNS Service on: $Server" 
  29.   
  30. # Get DNS service 
  31. $dns = gwmi win32_service -computer $Server  | where {$_.name -eq "DNS"
  32.   
  33. # Now stop it 
  34. $ret = $dns.stopservice() 
  35. if ($ret.returnvalue -eq 0) {"DNS Service stopped"
  36. else {"DNS Service not stopped"
  37.   
  38. # And now restart it 
  39. $ret = $dns.startservice() 
  40. if ($ret.returnvalue -eq 0) {"DNS Service started"
  41. else {"DNS Service not started"
  42.   
  43. # End of Script 
Technorati Tags: ,,,,

Saturday, 24 October 2009

Get-CARolesOfCaller.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script displays the CA roles of the caller 
  4. .DESCRIPTION 
  5.     This script instantiates the CA COM object, gets 
  6.     the allowed roles, and displays them. This script 
  7.     also shows use of Bitwise And operations, typical  
  8.     when using output from API calls.  Based on an earlier
  9.     script by Vadims Podans.
  10. .NOTES 
  11.     File Name  : Get-CARolesofCaller.ps1 
  12.     Author     : Thomas Lee - tfl@psp.co.uk 
  13.     Requires   : PowerShell V2 
  14. .LINK 
  15.     This script posted to: 
  16.         http://pshscripts.blogspot.com/2009/10/get-carolesofcallerps1.html
  17.     MSDN Sample posted at: 
  18.         http://msdn.microsoft.com/en-us/library/aa383243%28VS.85%29.aspx 
  19. .EXAMPLE 
  20.     PSH [C:\foo]: .\Get-CARolesOfCaller.ps1' 
  21.     You have the following rights on this CA: Cookham11\Cookham-CookhamCA 
  22.         CA administrator 
  23.         CA officer 
  24.         CA auditor 
  25.         CA backup 
  26.         Enrollment access 
  27. #> 
  28. # Instantiate the COM object 
  29. $CertAdmin = New-Object -com "CertificateAuthority.Admin.1" 
  30.   
  31. # Now get the roles assigned to me 
  32. $CA = "Cookham11\Cookham-CookhamCA" 
  33. $MyRoles = $CertAdmin.GetMyRoles([string] $CA
  34.  
  35. #Display Granular Rights 
  36. "You have the following rights on this CA: {0}" -f $CA 
  37.  switch ($MyRoles){ 
  38. {$MyRoles -band 1}     {"    CA administrator"
  39. {$MyRoles -band 2}     {"    CA officer"
  40. {$MyRoles -band 4}     {"    CA auditor"
  41. {$MyRoles -band 8}     {"    CA backup"
  42. {$MyRoles -band 256}   {"    CA Read access"
  43. {$MyRoles -band 512}   {"    Enrollment access"
  44. default                {"    No CA Access"

Wednesday, 21 October 2009

Get-DNSInfo.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script re-implements a MSDN Sample in PowerShell that 
  4.    shows the use of System.Net.NetworkInformation.NetworkInterface 
  5. .DESCRIPTION 
  6.     This script gets network information and formats DNS info. The 
  7.     script is effectively a one-liner! 
  8. .NOTES 
  9.     File Name  : Get-DNSInfo.ps1 
  10.     Author     : Thomas Lee - tfl@psp.co.uk 
  11.     Requires   : PowerShell V2 
  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.net.networkinformation.networkinterface.getallnetworkinterfaces.aspx 
  17. .EXAMPLE 
  18.     PSH [C:\foo]: . 'E:\PowerShellScriptLib\System.Net.NetworkInformation\Get-DNSInfo.ps1' 
  19.  
  20.     DnsSuffix   IsDnsEnabled IsDynamicDnsEnabled 
  21.     ---------   ------------ ------------------- 
  22.     cookham.net        False                True 
  23.     cookham.net        False                True 
  24.                        False                True 
  25.     cookham.net        False                True 
  26. #> 
  27.   
  28. ## 
  29. # Start of script 
  30. ## 
  31.  
  32. #Get information and print - do this as a 1-liner! 
  33. [System.Net.Networkinformation.NetworkInterface]::GetAllNetworkInterfaces() | %{$_.GetIpProperties()} | FT Dnssuffix,Isdnsenabled,IsDynamicDnsEnabled -a 
  34. # End of script 

Thursday, 8 October 2009

Get-OutlookFolders.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script displays the MAPI namespace in the current  
  4.     Outlook Profile. 
  5. .DESCRIPTION 
  6.     This script creates and Outlook.application object, 
  7.     then gets and displays the top level folders in the store, and 
  8.     the folders one level below.      
  9. .NOTES 
  10.     File Name  : Get-OutlookFolders.ps1 
  11.     Author     : Thomas Lee - tfl@psp.co.uk 
  12.     Requires   : PowerShell V2 
  13. .LINK 
  14.     This script posted to: 
  15.         http://pshscripts.blogspot.com/2009/10/get-outlookfoldersps1.html 
  16. .EXAMPLE 
  17.     PSH [C:\foo]: .\Get-OutLookFolders.PS1
  18.     3 Top Level Folders: 
  19.        Mailbox - Thomas Lee 
  20.        Public Folders 
  21.        Archive Folders 
  22.  
  23.     Folder 'Mailbox - Thomas Lee' contains the following subfolders: 
  24.        Deleted Items 
  25.        Grateful Dead 
  26.        Inbox 
  27.        Outbox 
  28.        Sent Items 
  29.        Calendar 
  30.        Contacts 
  31.        Drafts 
  32.        Journal 
  33.        Junk E-mail 
  34.        Notes 
  35.        Quarantine 
  36.        RSS Feeds 
  37.        Sync Issues 
  38.        Tasks 
  39.        Conversation Action Settings 
  40.        Quick Step Settings 
  41.        Suggested Contacts 
  42.     Folder 'Public Folders' contains the following subfolders: 
  43.        Favorites 
  44.        All Public Folders 
  45.     Folder 'Archive Folders' contains the following subfolders: 
  46.        Deleted Items 
  47.        Sent Items 
  48.        Calendar 
  49.        Journal 
  50.        Tasks 
  51. #> 
  52.  
  53. ##  
  54. # Start of Script 
  55. ## 
  56.  
  57. # First create Outlook object and get the Mapi namespace. 
  58. $Outlook = New-Object -com Outlook.Application 
  59. $Namespace = $outlook.GetNamespace("MAPI") 
  60.  
  61. # Now display top level folders 
  62. "{0} Top Level Folders:      " -f $Namespace.folders.count 
  63. foreach ($Fl in $namespace.Folders) { 
  64.   "   {0}" -f $Fl.Name} 
  65. "" 
  66. # Now look inside 
  67. foreach ($Folder in $Namespace.Folders) { 
  68. "Folder `'{0}`' contains the following subfolders: " -f $Folder.Name 
  69.   foreach ($fl in $Folder.Folders){ 
  70.   "   {0}" -f $Fl.Name 
  71.   } 
  72. }  
  73. # end of script 

Wednesday, 7 October 2009

New-Folder.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script Creates then removes a folder using methods in 
  4.     System.IO.Directory. 
  5. .DESCRIPTION 
  6.     This script checks to see if a staticly named folder exists. 
  7.     if not, it creates then removes the folder. The creation/deletion 
  8.     logic in enclosed within a try/catch block to capture errors. 
  9. .NOTES 
  10.     File Name  : New-Folder.ps1 
  11.     Author     : Thomas Lee - tfl@psp.co.uk 
  12.     Requires   : PowerShell V2 
  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/54a0at6s.aspx 
  18. .EXAMPLE 
  19.     PSH [C:\foo]: .\New-Folder.ps1' 
  20.     The directory was created successfully at 10/7/2009 12:00:35 PM. 
  21.     The directory was deleted successfully. 
  22. #> 
  23. # Specify the directory to manipulate 
  24. $path = "c:\fooxx" 
  25.   
  26. # try to see if the dirctory exists (It should not!) 
  27. try { 
  28.    if ([System.Io.Directory]::Exists($path)) {   
  29.                 "That path exists already." 
  30.                 return 
  31.     } 
  32.  
  33. # Try to create the directory 
  34. $di  = [System.Io.Directory]::CreateDirectory($path
  35.   
  36. # Get creattion time and display results 
  37. $dit = [System.Io.Directory]::GetCreationTime($path
  38. "The directory was created successfully at {0}." -f  $dit
  39.   
  40. # Delete the directory. 
  41.   $di.Delete() 
  42.   "The directory was deleted successfully." 
  43. }  
  44. catch { 
  45.             "The process failed" 
  46. }  

Tuesday, 6 October 2009

Create-TempFile.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script creates then copies a file using 
  4.     System.IO.Fileinfo.CopyTo() 
  5. .DESCRIPTION 
  6.     This script first creates a temporary file, then 
  7.     copies it to another file then deletes the  
  8.     second file. 
  9. .NOTES 
  10.     File Name : Create-TempFile.ps1 
  11.     Author : Thomas Lee - tfl@psp.co.uk 
  12.     Requires : PowerShell V2 
  13. .LINK 
  14.     This script posted to: 
  15.     http://pshscripts.blogspot.com/2009/10/create-tempfile.html
  16.     MSDN Sample posted at: 
  17.     http://msdn.microsoft.com/en-us/library/system.io.fileinfo.aspx 
  18. .EXAMPLE 
  19.     PSH [C:\foo]: .\Create-TempFile.PS1' 
  20.     File C:\Users\tfl\AppData\Local\Temp\tmpF2C1.tmp exists 
  21.     Original file: 
  22.     ----------- 
  23.     Hello 
  24.     And 
  25.     Welcome 
  26.     ----------- 
  27.     C:\Users\tfl\AppData\Local\Temp\tmpF2C1.tmp was copied to C:\Users\tfl\AppData\Local\Temp\tmpF2E1.tmp 
  28.     C:\Users\tfl\AppData\Local\Temp\tmpF2E1.tmp was successfully deleted.    
  29. #> 
  30.   
  31. ## 
  32. # start of script 
  33. ## 
  34.    
  35. # Create temp file and FileInfo object  
  36. $Path = [System.Io.Path]::GetTempFileName() 
  37. $fi1 = New-Object System.Io.FileInfo $Path 
  38.  
  39. # See if file exists 
  40. if ((Ls $fi1)) { 
  41.     "File {0} exists" -f $fi1  
  42.     # Create a file to write to and write to it 
  43.     $sw = $fi1.CreateText() 
  44.     $result=$sw.WriteLine("Hello"
  45.     $result=$sw.WriteLine("And"
  46.     $result=$sw.WriteLine("Welcome"
  47.     $sw.Close() 
  48.   
  49. # Open the file to read from. 
  50. $sr = $fi1.OpenText() 
  51. $s = ""
  52. "Original file:";"-----------" 
  53. while (($s = $sr.ReadLine()) -ne $null) { 
  54.     $s 
  55. "-----------"
  56. try { 
  57.     $path2 = [System.IO.Path]::GetTempFileName() 
  58.     $fi2 = New-Object system.IO.FileInfo $path2 
  59.  
  60.     # Ensure that the target does not exist. 
  61.     $fi2.Delete() 
  62.  
  63.     # Copy the file 
  64.     $result = $fi1.CopyTo($path2
  65.  
  66.     "{0} was copied to {1}" -f $path, $path2 
  67.  
  68.     # Delete the newly created file. 
  69.     $fi2.Delete() 
  70.     "{0} was successfully deleted." -f $path2 
  71. }  
  72. catch { 
  73.     "The process failed:";$Error[0] 
  74. # End of Script 
  75. "-----------" 

Monday, 5 October 2009

Get-FolderSize.ps1

  1. <#
  2. .SYNOPSIS
  3. This script displays the size of a folder
  4. .DESCRIPTION
  5. This script is a rewrite of an MSDN sample. It
  6. uses System.IO namespace to determine the size
  7. of a folder and its subfolders, then displays it.
  8. Note that the Get-DirSize function is recursive!
  9. .NOTES
  10. File Name : Get-FolderSize.ps1
  11. Author : Thomas Lee - tfl@psp.co.uk
  12. Requires : PowerShell V2
  13. .LINK
  14. This script posted to: http://pshscripts.blogspot.com/2009/10/get-foldersizeps1.html
  15. MSDN Sample posted at: http://msdn.microsoft.com/en-us/library/system.io.directory.aspx

  16. .EXAMPLE
  17. PSH [C:\foo]: .Get-FolderSize c:\foo
  18. The size of C:\Foo and its subdirectories is 17,577,318 bytes
  19. #>

  20. param (
  21. $folder = "C:\Foo"
  22. )

  23. ##
  24. # Helper Function Get-DirSize
  25. ##
  26. function Get-DirSize {
  27. param ([system.IO.DirectoryInfo] $d)
  28. [long] $Size = 0;

  29. # Add file sizes.
  30. $fis = $d.GetFiles();
  31. foreach ($fi in $fis){
  32. $size += $fi.Length;
  33. }
  34. # Add subdirectory sizes recursively.
  35. $dis = $d.GetDirectories()
  36. foreach ($di in $dis) {
  37. $Size += Get-DirSize($di)
  38. }
  39. return $Size
  40. }

  41. ## End of Get-DirSize helper function

  42. ##
  43. # Start of Script
  44. $d = New-Object system.IO.DirectoryInfo $folder
  45. $size= Get-Dirsize $d
  46. "The size of {0} and its subdirectories is {1} bytes" -f $d,$size.ToString("###,###")

Sunday, 20 September 2009

Get-PlatformId.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script displays the platform id of the system 
  4. .DESCRIPTION 
  5.     This script is a rewrite of an MSDN sample. It  
  6.     gets System.Environment.Version's Platform field 
  7.     and displays it. The sample is simplified from 
  8.     the C# version! Not tested except on Server 2008. 
  9. .NOTES 
  10.     File Name  : Get-PlatformId.ps1 
  11.     Author     : Thomas Lee - tfl@psp.co.uk 
  12.     Requires   : PowerShell V2 
  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/3a8hyw88.aspx 
  18. .EXAMPLE 
  19.     PSH [C:\foo]: .Get-PlatformID.ps1' 
  20.     Platform ID: Win32NT 
  21. #> 
  22.  
  23. ## 
  24. # Start of Script 
  25. ## 
  26.  
  27. #Get OS Version from System.Environment 
  28. $OS  =  [System.Environment]::OSVersion 
  29.  
  30. # Get and display the PlatformID 
  31. $Platformid$OS.Platform; 
  32. "Platform ID: {0}" -f $Platformid 
  33. # End of script 

Saturday, 5 September 2009

Send-EmailMessage.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script creates and sends an SMTP email message. 
  4. .DESCRIPTION 
  5.     This script first creates a System.Net.Mail.Mailmessage, and populates 
  6.     it. Next, it creates an system.Net.Mail.SmtpClient, which then sends 
  7.     the message to the SMTP Server, and onwards transmission. This script 
  8.     is effectively a re-write of the C# sample above. 
  9. .NOTES 
  10.     File Name  : Send-EmailMessage.ps1 
  11.     Author     : Thomas Lee - tfl@psp.co.uk 
  12.     Requires   : PowerShell V2 
  13. .LINK 
  14.     This script posted to: 
  15.         http://pshscripts.blogspot.com/2009/09/send-emailmessageps1.html
  16.     MSDN Sample posted at: 
  17.         http://msdn.microsoft.com/en-us/library/67w4as51.aspx 
  18. .EXAMPLE 
  19.     PSH [C:\foo]: .\Send-EmailMessage.ps1' 
  20.     Message sent successfully 
  21. #> 
  22.   
  23. ## 
  24. # Start of Script 
  25. ## 
  26.   
  27. # Set contents of the Email message 
  28. $To      = "doctordns@gmail.com" 
  29. $From    = "jane@cookham.net" 
  30. $Subject = "Using the .NET SMTP client." 
  31. $Body    = "Using this .NET feature, you can send an e-mail message from an application very easily." 
  32.               
  33. # Create meil message 
  34. $Message = New-Object System.Net.Mail.MailMessage $From, $To, $Subject, $Body 
  35.   
  36. # Create SMTP client 
  37. $Server = "cookham8" 
  38. $Port = 25 
  39. $Client = New-Object System.Net.Mail.SmtpClient $Server, $Port 
  40.   
  41. # Credentials are necessary if the server requires the client  
  42. # to authenticate before it will send e-mail on the client's behalf. 
  43. $Client.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials 
  44.   
  45. # Try to send the message 
  46. try { 
  47.        $Client.Send($Message
  48.        "Message sent successfully" 
  49.    
  50. # Catch an error 
  51. catch { 
  52. "Exception caught in Send-Emailmessage.ps1" 
  53. # End of Script 

Friday, 7 August 2009

Get-SQLServerVersion.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script displays the versions of SQL Server running on a system 
  4. .DESCRIPTION 
  5.     This script uses WMI to get the SQLServiceAdvancedProperty class from 
  6.     the ComputerManagement namespace to print out the versions. This  
  7.     script is an adaptation of the VBS script on MSDN. 
  8. .NOTES 
  9.     File Name  : Get-SQLServerVersion.ps1 
  10.     Author     : Thomas Lee - tfl@psp.co.uk 
  11.     Requires   : PowerShell V2 CTP3 
  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/ms186353.aspx 
  17. .EXAMPLE 
  18.     PSH [C:\foo]: .\Get-SQLVersion.ps1' 
  19.     You are running the following versions of SQL: 
  20.     Service Name            Version 
  21.     ------------            ------- 
  22.     MSSQL$MICROSOFT##SSEE   9.3.4035.00 
  23.     MSSQL$SQLEXPRESS        9.3.4035.00 
  24. #> 
  25.  
  26. ## 
  27. # Start of script 
  28. ## 
  29.  
  30. # Get the versions of SQL from WMI 
  31. $Versions = Get-WmiObject -Namespace root\Microsoft\SQLServer\computerManagement -Class SqlServiceAdvancedProperty | where {$_.SqlServiceType -eq 1 -and $_.PropertyName -eq "VERSION"
  32.  
  33. # Now display results 
  34. "You are running the following versions of SQL:" 
  35. "Service Name            Version" 
  36. "------------            -------" 
  37. foreach ($version in $versions) { 
  38. "{0} `t{1}" -f $version.servicename,$version.propertystrvalue 
  39. #End of script 
Technorati Tags: ,,,

Thursday, 6 August 2009

New-Ou.PS1

  1. <# 
  2. .SYNOPSIS 
  3.     This script uses ADSI to add a new OU to a domain. 
  4. .DESCRIPTION 
  5.     This script creates a pointer to the domain, then 
  6.     uses the Create method to create a new OU under 
  7.     the root of the domain. 
  8. .NOTES 
  9.     File Name  : New-Ou.ps1 
  10.     Author     : Thomas Lee - tfl@psp.co.uk 
  11.     Requires   : PowerShell V2 CTP3 
  12. .LINK 
  13.     This script posted to: 
  14.         http://www.pshscripts.blogspot.com 
  15. .EXAMPLE 
  16.     PSH [C:\foo]: .\New-OU.PS1' 
  17.     Created OU: PowerShell 
  18.      
  19.     distinguishedName : {OU=PowerShell,DC=cookham,DC=net} 
  20.     Path              : LDAP://ou=PowerShell,dc=Cookham,dc=Net 
  21. #> 
  22. ## 
  23. # Start of Script 
  24. ## 
  25.  
  26. #Set variables 
  27. $OuName = “PowerShell” 
  28. $Domain = [ADSI]“LDAP://dc=Cookham,dc=Net” 
  29.  
  30. # Create the OU 
  31. $Ou = $Domain.Create(”OrganizationalUnit”, “ou=” + $OuName
  32. $Ou.SetInfo() 
  33.  
  34. # Display results 
  35. "Created OU: {0}" -f $OUName 
  36. $OU 
Technorati Tags: ,,,

Wednesday, 5 August 2009

Get-SpecialFolders.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script lists the special folders enumerated in System.Environment.SpecialFolder 
  4. .DESCRIPTION 
  5.     This script first enumerates the SpecialFolder Enum. for each member, the script 
  6.     then looks up, and displays, the value of that folder. 
  7. .NOTES 
  8.     File Name  : Get-SpecialFolders.ps1 
  9.     Author     : Thomas Lee - tfl@psp.co.uk 
  10.     Requires   : PowerShell V2 CTP3 
  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.environment.specialfolder.aspx 
  16. .EXAMPLE 
  17.     PSH [C:\foo]: . 'E:\PowerShellScriptLib\System.Environment\Get-SpecialFolders.PS1' 
  18.     Folder Name            Path 
  19.     -----------            ----------------------------------------------- 
  20.     Desktop                C:\Users\tfl\Desktop 
  21.     Programs               C:\Users\tfl\AppData\Roaming\Microsoft\Windows\Start Menu\Programs 
  22.     Personal               C:\Users\tfl\Documents 
  23.     Personal               C:\Users\tfl\Documents 
  24.     Favorites              C:\Users\tfl\NetHood\Favorites 
  25.     Startup                C:\Users\tfl\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup 
  26.     Recent                 C:\Users\tfl\AppData\Roaming\Microsoft\Windows\Recent 
  27.     SendTo                 C:\Users\tfl\AppData\Roaming\Microsoft\Windows\SendTo 
  28.     StartMenu              C:\Users\tfl\AppData\Roaming\Microsoft\Windows\Start Menu 
  29.     MyMusic                C:\Users\tfl\Music 
  30.     DesktopDirectory       C:\Users\tfl\Desktop 
  31.     MyComputer 
  32.     Templates              C:\Users\tfl\AppData\Roaming\Microsoft\Windows\Templates 
  33.     ApplicationData        C:\Users\tfl\AppData\Roaming 
  34.     LocalApplicationData   C:\Users\tfl\AppData\Local 
  35.     InternetCache          C:\Users\tfl\AppData\Local\Microsoft\Windows\Temporary Internet Files 
  36.     Cookies                C:\Users\tfl\AppData\Roaming\Microsoft\Windows\Cookies 
  37.     History                C:\Users\tfl\AppData\Local\Microsoft\Windows\History 
  38.     CommonApplicationData  C:\ProgramData 
  39.     System                 C:\Windows\system32 
  40.     ProgramFiles           C:\Program Files (x86) 
  41.     MyPictures             C:\Users\tfl\Pictures 
  42.     CommonProgramFiles     C:\Program Files (x86)\Common Files 
  43. #> 
  44.  
  45. ## 
  46. # Start of Script 
  47. ## 
  48.  
  49. # Get the list of special folders 
  50. $folders = [system.Enum]::GetValues([System.Environment+SpecialFolder])  
  51.  
  52. # Display these folders 
  53. "Folder Name            Path" 
  54. "-----------            -----------------------------------------------" 
  55. foreach ($folder in $folders) { 
  56. "{0,-22} {1,-15}"  -f $folder,[System.Environment]::GetFolderPath($folder
  57. #End of Script 

Get-Thursday

  1. <# 
  2. .SYNOPSIS 
  3.     This script checks of a particular date in the past was a Thurday. 
  4. .DESCRIPTION 
  5.     This script creates a DateTime object set for 1st May, 2003. The 
  6.     script then check to see if that day is a Thursday then displays
  7.     the day of week for that date (which is a Thursday). This script
  8.     is a copy of the MSDN sample, written in PowerShell.   
  9. .NOTES 
  10.     File Name  : Get-Thursday.ps1 
  11.     Author     : Thomas Lee - tfl@psp.co.uk 
  12.     Requires   : PowerShell V2 CTP3 
  13. .LINK 
  14.     This script posted to: 
  15.         http://pshscripts.blogspot.com/2009/08/get-thursday.html
  16.     MSDN Sample posted at: 
  17.         http://msdn.microsoft.com/en-us/library/system.dayofweek(VS.71).aspx 
  18. .EXAMPLE 
  19.     PSH [C:\foo]: . 'E:\PowerShellScriptLib\System.DateTime\get-thursday.ps1' 
  20.     Is Thursday the day of the week for 5/1/2003?: True 
  21.     The day of the week for 5/1/2003 is Thursday. 
  22. #> 
  23.   
  24. ## 
  25. # Start of script 
  26. ## 
  27.   
  28. # Create a DateTime for the first of May, 2003. 
  29. $dt = New-Object System.DateTime 2003, 5, 1 
  30.   
  31. # Now - is it Thursday? 
  32. "Is Thursday the day of the week for {0:d}?: {1}" -f $dt,($dt.DayOfWeek -eq [system.DayOfWeek]::Thursday) 
  33. "The day of the week for {0:d} is {1}." -f $dt, $dt.DayOfWeek 
  34. # End of Script