Saturday, 9 January 2010

Get-VMMemory

  1. <# 
  2. .SYNOPSIS 
  3.     This script displays the virtual Memory assigned to Hyper-V 
  4.     VMs on a given host. 
  5. .DESCRIPTION 
  6.     This script uses The Hyper-V module from Codeplex to first 
  7.     get all the VMs on a given server, then display the memory  
  8.     allocated to each VM. 
  9. .NOTES 
  10.     File Name  : get-VMMemory.ps1 
  11.     Author     : Thomas Lee - tfl@psp.co.uk 
  12.     Requires   : PowerShell Version 2.0 
  13.                  and HyperV module from Codeplex 
  14.                  http://pshyperv.codeplex.com 
  15. .LINK 
  16.     This script posted to: 
  17.         http://www.pshscripts.blogspot.com 
  18. .EXAMPLE 
  19.     PSH [C:\Foo]: c:\foo\get-VMMemory.ps1 Foo21 
  20.     On Server Foo21 there are 2 VMs as follows: 
  21.     Virtual Machine                       Memory 
  22.     SQL-2008-R2-CTP                          768 
  23.     Server-2008-R2                           768 
  24. .EXAMPLE 
  25.     PSH [C:\Foo]: "Server2" | c:\foo\get-VMMemory.ps1 Foo21 
  26.     On Server Server2 there are 3 VMs as follows: 
  27.     Virtual Machine                       Memory 
  28.     Server-2008-R2-1                         768 
  29.     Server-2008-R2-2                         768 
  30.     Server-2008-R2-3                         768 
  31. #> 
  32.  
  33. # Parameter is the machine to look at 
  34. param
  35. [Parameter(Position=0, Mandatory=$false,ValueFromPipeLine=$true)]  
  36. [string] $Computer = "MyServer" 
  37. ## 
  38. # Start of script 
  39. ## 
  40.  
  41. # Import the module 
  42. Import-Module HyperV 
  43.  
  44. # Get all the VMs on the target system 
  45. $Vms = Get-Vm -Server $Computer 
  46.  
  47. # Now loop through the VMs and display memory allocated 
  48. "On Server {0} there are {1} VMs as follows:" -f $Computer,$Vms.count 
  49. "{0,-30}    {1,10}" -f "Virtual Machine", "Memory" 
  50. Foreach ($Vm in $Vms) { 
  51. $Mem = Get-Vmmemory $Vm
  52. "{0,-30}    {1,10}" -f $Vm.Elementname,$Mem.Virtualquantity 
  53. }  
  54. # End of script 

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