Sunday, 21 March 2010

Add-FireWallApplication.ps1

  1.    <# 
  2. .SYNOPSIS 
  3.     This script adds a program to the firewall. 
  4. .DESCRIPTION 
  5.     This script used the firewall com object to add 
  6.     a new application to the firewall.   
  7. .NOTES 
  8.     File Name  : Add-FirewallApplication.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/03/add-firewallapplicationps1.html
  14.     MSDN Sample posted at: 
  15.         http://msdn.microsoft.com/en-us/library/aa366421%28VS.85%29.aspx 
  16. .EXAMPLE 
  17.     At start of script, authorised applications are: 
  18.  
  19.     Name                            Enabled 
  20.     ----                            ------- 
  21.     Delivery Manager Service           True 
  22.     BitTornado                         True 
  23.     driver                             True 
  24.     driver                             True 
  25.     BitTorrent                         True 
  26.     DNA                                True 
  27.     Microsoft Office OneNote           True 
  28.  
  29.     After adding Notepad - here are authorised applications 
  30.  
  31.    Name                            Enabled 
  32.    ----                            ------- 
  33.     Notepad                           True 
  34.     Delivery Manager Service          True 
  35.     BitTornado                        True 
  36.     driver                            True 
  37.     driver                            True 
  38.     BitTorrent                        True 
  39.     DNA                               True 
  40.     Microsoft Office OneNote          True 
  41. #> 
  42.  
  43. ## 
  44. # Start of script 
  45. ## 
  46.  
  47. # Set constants 
  48. $NET_FW_PROFILE_DOMAIN = 0 
  49. $NET_FW_PROFILE_STANDARD = 1 
  50.  
  51. # Scope 
  52. $NET_FW_SCOPE_ALL = 0 
  53.  
  54. # IP Version - ANY is the only allowable setting for now 
  55. $NET_FW_IP_VERSION_ANY = 2 
  56.  
  57. # Create the firewall manager object. 
  58. $fwMgr = new-object -com HNetCfg.FwMgr 
  59.  
  60. # Get the current profile for the local firewall policy. 
  61. $profile = $fwMgr.LocalPolicy.CurrentProfile 
  62.  
  63. # Display applications available 
  64. "At start of script, authorised applications are:" 
  65. $profile.AuthorizedApplications | ft name, enabled -AutoSize 
  66.  
  67. # Create application to add to firewall 
  68. $app = New-Object -com HNetCfg.FwAuthorizedApplication 
  69. $app.ProcessImageFileName = "C:\windows\notepad.exe" 
  70. $app.Name = "Notepad" 
  71. $app.Scope = $NET_FW_SCOPE_ALL 
  72.  
  73. # Use either Scope or RemoteAddresses, but not both 
  74. # $app.RemoteAddresses = "*" 
  75. $app.IpVersion = $NET_FW_IP_VERSION_ANY 
  76. $app.Enabled = $TRUE 
  77.  
  78. # Use this line if you want to add the app, but disabled. 
  79. # $app.Enabled = FALSE 
  80. $profile.AuthorizedApplications.Add($app
  81.  
  82. # Show applications after addition 
  83. "After adding Notepad - here are authorised applications" 
  84. $profile.AuthorizedApplications | ft name, enabled -AutoSize 
  85. # End of script 

Saturday, 20 March 2010

Get-OutlookStores.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script uses the Outlook COM object to  
  4.     display the data stores in the current profile 
  5. .DESCRIPTION 
  6.     This script creates an Outlook object, displays 
  7.     user information, and the stores currently 
  8.     attached to the profile. 
  9. .NOTES 
  10.     File Name  : Get-OutlookStores.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. .EXAMPLE 
  17.     PSH [C:\foo]: .\Get-OutlookStores.ps1' 
  18.     Current profile has the following configured accounts: 
  19.  
  20.     Account Type           User Name        SMTP Address 
  21.     ------------           ---------        ------------ 
  22.     Microsoft Exchange     Thomas.Lee       Thomas.Lee@cookham.net 
  23.     thomas_lee@hotmail.com Thomas Lee (MSN) thomas_lee@hotmail.com 
  24.  
  25.     Exchange Offile Folder Store: 
  26.     C:\Users\tfl\AppData\Local\Microsoft\Outlook\outlook0.ost 
  27.     
  28.     PST Files 
  29.     Display Name    File Path  
  30.     ------------    --------- 
  31.     Archive Folders C:\Users\tfl\AppData\Local\Microsoft\Outlook\archive.pst 
  32. #> 
  33.  
  34. ## 
  35. # Begin Script 
  36. ## 
  37.  
  38. # Create Outlook object 
  39. $Outlook = New-Object -ComObject Outlook.Application 
  40. $stores = $Outlook.Session.Stores 
  41. $accounts = $outlook.session.accounts 
  42.  
  43. # Basic information 
  44. "Current profile has the following configured accounts:" 
  45. $dn = @{label = "Account Type"; expression={$_.displayname}} 
  46. $un = @{label = "User Name"; expression = {$_.username}} 
  47. $sm = @{label = "SMTP Address"; expression = {$_.smtpaddress}} 
  48. $accounts | Format-Table -AutoSize $dn,$un,$sm 
  49. # Check number of stores > 0 
  50. if ($stores.Count -le 0) {"No stores found"; return
  51.  
  52. # Outlook Off-Line folder store 
  53. $ost = $stores | where{$_.filepath -match ".ost$"
  54. if (!$ost
  55.    "No Outlook Offline Folder store found" 
  56. else 
  57.    "Exchange Offile Folder Store:" 
  58.    $ost | ft filepath -HideTableHeaders 
  59.   } 
  60.  
  61. # PST Files 
  62. $pst = $stores | where {$_.filepath -match ".pst$"
  63. if (!$pst
  64.   { 
  65.     "No PST files found" 
  66.   } 
  67. else 
  68.   { 
  69.     "PST Files" 
  70.     $dn = @{label = "Display Name"; expression={$_.displayname}} 
  71.     $fn = @{label = "File Path"; expression={$_.filepath}} 
  72.     $pst | ft $dn,$fn        
  73.   } 
  74. # End Script

Thursday, 18 March 2010

Enable-ICMP.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script Enables ICMP on the Standard Firewall profile. 
  4. .DESCRIPTION 
  5.     This script creates a Firewall object then configures it. 
  6. .NOTES 
  7.     File Name  : Enable-ICMP.ps1 
  8.     Author     : Thomas Lee - tfl@psp.co.uk 
  9.     Requires   : PowerShell Version 2.0 
  10. .LINK 
  11.     This script posted to: 
  12.         http://www.pshscripts.blogspot.com 
  13.     MSDN Sample posted at: 
  14.         http:// 
  15. .EXAMPLE 
  16.     PSH [C:\foo]: . 'E:\PowerShellScriptLib\COM\HNetCfg.FwMgr\Enable-ICMP.ps1' 
  17.  
  18.     AllowOutboundDestinationUnreachable : False 
  19.     AllowRedirect                       : False 
  20.     AllowInboundEchoRequest             : False 
  21.     AllowOutboundTimeExceeded           : False 
  22.     AllowOutboundParameterProblem       : False 
  23.     AllowOutboundSourceQuench           : False 
  24.     AllowInboundRouterRequest           : False 
  25.     AllowInboundTimestampRequest        : False 
  26.     AllowInboundMaskRequest             : False 
  27.     AllowOutboundPacketTooBig           : True 
  28.  
  29.     After Script ran: 
  30.     AllowOutboundDestinationUnreachable : False 
  31.     AllowRedirect                       : False 
  32.     AllowInboundEchoRequest             : True 
  33.     AllowOutboundTimeExceeded           : False 
  34.     AllowOutboundParameterProblem       : False 
  35.     AllowOutboundSourceQuench           : False 
  36.     AllowInboundRouterRequest           : False 
  37.     AllowInboundTimestampRequest        : False 
  38.     AllowInboundMaskRequest             : False 
  39.     AllowOutboundPacketTooBig           : True 
  40. #> 
  41.  
  42. ## 
  43. # Start of script 
  44. ## 
  45.  
  46. # Set strict mode 
  47. Set-StrictMode -Version 2.0 
  48.  
  49. # Set Constants 
  50. $NET_FW_PROFILE_DOMAIN   = 0 
  51. $NET_FW_PROFILE_STANDARD = 1 
  52.  
  53. # Create the firewall manager object. 
  54. $fwMgr = New-Object -com HNetCfg.FwMgr 
  55.  
  56. # Get the current profile for the local firewall policy. 
  57. $profile = $fwMgr.LocalPolicy.GetProfileByType($NET_FW_PROFILE_STANDARD
  58.  
  59. # Display current ICMP settings 
  60. $Profile.IcmpSettings 
  61.  
  62. # Now set it to True 
  63. $profile.IcmpSettings.AllowInboundEchoRequest = $True 
  64.  
  65. # Use this line if you want to disable the setting. 
  66. #profile.IcmpSettings.AllowInboundEchoRequest = $FALSE 
  67.  
  68. # Display it again 
  69. "After Script ran: " 
  70. $Profile.IcmpSettings 
  71.  
  72. # End Script 

New-ExcelWorkbook.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script creates an Excel workbook using PowerShell 
  4. .DESCRIPTION 
  5.     This script demonstrates manipulating Excell with PowerShell 
  6.     and the Excel.Application COM object. 
  7. .NOTES 
  8.     File Name  : New-ExcelWorkbook.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/03/new-excelworkbookps1.html
  14.     MSDN Sample posted at: 
  15.         http://msdn.microsoft.com/en-us/library/bb211359.aspx  
  16. .EXAMPLE
  17.     Run it and see one! 
  18. #> 
  19. ##  
  20. # Start of Script 
  21. ## 
  22. # Then we create and save a sample worksheet 
  23. # Create Excel object 
  24. $excel = new-object -comobject Excel.Application 
  25.     
  26. # Make Excel visible 
  27. $excel.visible = $true 
  28.    
  29. # Create a new workbook 
  30. $workbook = $excel.workbooks.add() 
  31.  
  32. # The default workbook has three sheets, remove 2 
  33. $S2 = $workbook.sheets | where {$_.name -eq "Sheet2"
  34. $s3 = $workbook.sheets | where {$_.name -eq "Sheet3"
  35. $s2.delete() 
  36. $s3.delete() 
  37. # Get sheet and update sheet name 
  38. $s1 = $workbook.sheets | where {$_.name -eq 'Sheet1'
  39. $s1.name = "PowerShell Sample" 
  40.    
  41. # Update workook properties 
  42. $workbook.author = "Thomas Lee - tfl@psp.co.uk" 
  43. $workbook.title = "Excel and PowerShell rock!" 
  44. $workbook.subject = "Demonstrating the Power of PowerShell with Excel" 
  45.    
  46. # Next update some cells in the worksheet 'PowerShell Sample' 
  47. $s1.range("A1:A1").cells="Cell a1" 
  48. $s1.range("A2:A2").cells="A2" 
  49. $s1.range("b1:b1").cells="Cell B1" 
  50. $s1.range("b2:b2").cells="b2" 
  51.  
  52. # now make a sum 
  53. $s1.range("E1:E2").cells="Widgets" 
  54. $s1.range("E2:E2").cells=2 
  55. $s1.range("E3:E3").cells=2 
  56. $s1.range("E4:E4").cells=38 
  57. $s1.range("D5:D5").cells="Total" 
  58. $s1.range("E5:E5").cells.formula = "=sum(e2,E4)" 
  59.    
  60. # And save it away: 
  61. $s1.saveas("c:\foo\xlsx3.xlsx"
  62. # end of script 

Wednesday, 17 March 2010

Enable-FirewallPort2.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script creates a rule in the Windows Host Firewall. 
  4. .DESCRIPTION 
  5.     This script creates a new firewall rule for 
  6.     port 80 over tcp (i.e. 80). 
  7. .NOTES 
  8.     File Name  : Enable-FirewallPort.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/03/enable-firewallport2ps1.html 
  14.     MSDN Sample posted at: 
  15.         http://msdn.microsoft.com/en-us/library/aa366423%28VS.85%29.aspx 
  16. .EXAMPLE 
  17.     PSH [C:\foo]: .\Enable-FirewallPort.ps1
  18.     Before Script Runs: 
  19.  
  20.     Name   IpVersion Protocol Port Scope RemoteAddresses Enabled 
  21.     ----   --------- -------- ---- ----- --------------- ------- 
  22.     HTTPS          2        6  443     0 *                  True 
  23.     driver         2        6 8085     0 *                  True 
  24.     driver         2        6 8085     0 *                  True 
  25.   
  26.  
  27.     After Script Runs: 
  28.  
  29.     Name   IpVersion Protocol Port Scope RemoteAddresses Enabled 
  30.     ----   --------- -------- ---- ----- --------------- ------- 
  31.     HTTP           2        6   80     0 *                  True 
  32.     HTTPS          2        6  443     0 *                  True 
  33.     driver         2        6 8085     0 *                  True 
  34.     driver         2        6 8085     0 *                  True 
  35.  
  36. #> 
  37.  
  38. ## 
  39. # Start Script 
  40. ## 
  41.  
  42. # Set Strict Mode  
  43. Set-Strictmode -Version 2.0 
  44. # Set Constants 
  45. $NET_FW_IP_PROTOCOL_UDP = 17 
  46. $NET_FW_IP_PROTOCOL_TCP = 6 
  47.  
  48. # Create the firewall manager object. 
  49. $fwMgr = New-Object -COM HNetCfg.FwMgr 
  50.  
  51. # Get the current profile for the local firewall policy. 
  52. $profile = $fwMgr.LocalPolicy.CurrentProfile 
  53.  
  54. # Display it 
  55. "Before Script Runs:" 
  56. $profile.GloballyOpenPorts | ` 
  57. ft name, ipversion, protocol, port, scope, remoteaddresses, enabled -auto 
  58.  
  59. # Now add Port 80 
  60.  
  61. $port = New-Object -COM HNetCfg.FWOpenPort 
  62. $port.Name = "HTTP" 
  63. $port.Protocol = $NET_FW_IP_PROTOCOL_TCP 
  64. $port.Port = 80 
  65.  
  66. # If using RemoteAddresses, don't use Scope 
  67. # "*" means Scope of Any. Other entries are ignored if this is specified. 
  68. # "LocalSubnet" means Scope of Local Subnet. Can be used with other addresses as well.  
  69. $port.RemoteAddresses = "*" 
  70.  
  71. # Use this line to scope the port to Local Subnet only 
  72. #$port.RemoteAddresses = "LocalSubnet" 
  73.  
  74. #Use this line to scope the port to the specific IP 10.1.1.1, the specific subnet 12.5.0.0, and Local Subnet. Don't put spaces. 
  75. #port.RemoteAddresses = "LocalSubnet,10.1.1.1/255.255.255.255,12.5.0.0/255.255.0.0" 
  76.  
  77. $port.Enabled = $TRUE 
  78.  
  79. #Use this line instead if you want to add the port, but disabled 
  80. #port.Enabled = FALSE 
  81.  
  82. # Now add the port 
  83. $profile.GloballyOpenPorts.Add($port
  84.  
  85. # Print Results 
  86. " After Script Runs:" 
  87. $profile = $fwMgr.LocalPolicy.CurrentProfile 
  88. $profile.GloballyOpenPorts | ` 
  89. ft name, ipversion, protocol, port, scope, remoteaddresses, enabled -auto 
  90. # End of script 

Tuesday, 16 March 2010

Set-ProgrammerAlias.ps1


  1. <# 
  2. .SYNOPSIS 
  3.     This script creates a function to set aliases for all Cmdlets which omit the '-' 
  4. .DESCRIPTION 
  5.     This script defines a function which uses Get-Command to find all
  6.     cmdlets. For each of them, it then creates an alias which omits
  7.     the "-". This function  was oritinally written by Jeffrey Snover for
  8.     Monad back in the day, but I updated it slightly for PowerShell V2. 
  9.     Updated after a comment to be a tad shorter. 
  10. .NOTES 
  11.     File Name  : Set-ProgrammerAlias.ps1 
  12.     Author     : Jeffrey Snover - jsnover@microsoft.com 
  13.     Updated by : Thomas Lee - tfl@psp.co.uk 
  14.     Requires   : PowerShell Version 2.0 
  15. .LINK 
  16.     This script posted to: 
  17.         http://www.pshscripts.blogspot.com 
  18. #> 
  19. ## 
  20. # Start of Script 
  21.  
  22. function Set-ProgrammerAlias { 
  23.  get-command -Com Cmdlet | % {set-alias $($_.Verb + $_.Noun) $_.Name}  
Technorati Tags: ,,,

Friday, 5 March 2010

Enable-FWPort.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script enables then disables the SMTP port on a local system  
  4. .DESCRIPTION 
  5.     This script first creates a FW object, then creates a port. The 
  6.     script then addes that port to the firewall rules. The script 
  7.     finally removes the port. The script also prints before/after 
  8.     results. 
  9. .NOTES 
  10.     File Name  : Enable-FWPort.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/aa366425(VS.85).aspx 
  18. #> 
  19.  
  20. ##  
  21. # Start of Script 
  22. ##  
  23.   
  24. # Define Constants 
  25. $NET_FW_IP_PROTOCOL_UDP = 17 
  26. $NET_FW_IP_PROTOCOL_TCP = 6 
  27. $NET_FW_SCOPE_ALL = 0 
  28.   
  29. # Create FW objecct 
  30. $fwMgr = new-object -com HNetCfg.FwMgr 
  31.   
  32. # Get current profile 
  33. $profile = $fwMgr.LocalPolicy.CurrentProfile 
  34.   
  35. # Display ports open: 
  36. $profile.GloballyOpenPorts | ft name,port,enabled -auto 
  37.   
  38. # Create Port object 
  39. $port = New-Object -com HNetCfg.FWOpenPort 
  40. $port.Name = "SMTP" 
  41. $port.Port = 25 
  42. $port.Protocol = $NET_FW_IP_PROTOCOL_TCP 
  43. $port.Scope = $NET_FW_SCOPE_ALL 
  44.   
  45. # Enable the port 
  46. $port.Enabled = $True 
  47. $profile.GloballyOpenPorts.Add($port
  48.   
  49. # Display results 
  50. $profile.GloballyOpenPorts | ft name,port,enabled -auto 
  51.   
  52. # now remove the port 
  53. $profile.GloballyOpenPorts.remove($port.port,$NET_FW_IP_PROTOCOL_TCP
  54.   
  55. # Display results 
  56. $profile.GloballyOpenPorts | ft name,port,enabled -auto 
  57. # End of script 
Technorati Tags: ,,,

Sunday, 28 February 2010

Using-Culture.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script creates a function to run culture sensitive operations
  4.     in some other culture. 
  5. .DESCRIPTION 
  6.     The Using-Culture function gets the current culture, saves it, then
  7.     runs the script block in the requested culture. The function then
  8.     restores the culture.  The script then ends with calls to
  9.     Using-Culture to demonstrate its use.
  10.  
  11.     The script is based on a blog note at: http://blogs.msdn.com/powershell/archive/2006/04/25/583235.aspx
  12. .NOTES 
  13.     File Name  : Using-Culture.ps1 
  14.     Author     : Thomas Lee - tfl@psp.co.uk 
  15.     Requires   : PowerShell Version 2.0 
  16. .LINK 
  17.     This script posted to: 
  18.         http://pshscripts.blogspot.com/2010/02/using-cultureps1.html
  19. .EXAMPLE 
  20.     run the script and see! 
  21. #> 
  22.  
  23. # Defind the Using-Culture function 
  24. Function Using-Culture { 
  25. Param ( 
  26. [System.Globalization.CultureInfo]$culture = (throw "USAGE: Using-Culture -Culture culture -Script {scriptblock}"), 
  27. [ScriptBlock]$script= (throw "USAGE: Using-Culture -Culture culture -Script {scriptblock}"
  28.     $OldCulture = [System.Threading.Thread]::CurrentThread.CurrentCulture 
  29.     trap  
  30.     { 
  31.         [System.Threading.Thread]::CurrentThread.CurrentCulture = $OldCulture 
  32.     } 
  33.     [System.Threading.Thread]::CurrentThread.CurrentCulture = $culture 
  34.     Invoke-Command $script 
  35.     [System.Threading.Thread]::CurrentThread.CurrentCulture = $OldCulture 
  36. ## 
  37. # Now use the function 
  38. ## 
  39.  
  40. # Here is an example of Using-Culture 
  41. "Get-Date using ar-IQ" 
  42. using-culture ar-IQ {get-date} 
  43. "" 
  44. # Parse ar-IQ formatted date into local culture 
  45. "Parse IQ date to English and display" 
  46. $IQD = "30 تشرين الثاني, 2005 09:01:38 ص" 
  47. using-culture ar-IQ {$global:d=[DateTIme]::Parse($IQD)} 
  48. $IQD 
  49. $d 
  50. "" 
  51. # Now do it in German 
  52. "Get-Date using de-de" 
  53. using-culture de-de {get-date} 
  54. "" 
  55. "Parsing a german date into local" 
  56. $ded="Mittwoch, 30. November 2005 09:02:29" 
  57. using-culture de-de {$global:d=[DateTIme]::Parse($DED)} 
  58. $ded 
  59. $d 

Saturday, 27 February 2010

PowerShell Master Class in Stockholm – Filling Up Quickly!

In a recent blog post, I mentioned the PowerShell Master Class I am running in Stockholm on March 9-11 2010 (see Http://Www.PowerShellMasterClass.Com for more details). I’m pleased to say the class is nearly full! I am looking forward to three great days with PowerShell!

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