- <#
- .SYNOPSIS
- This script displays the virtual Memory assigned to Hyper-V
- VMs on a given host.
- .DESCRIPTION
- This script uses The Hyper-V module from Codeplex to first
- get all the VMs on a given server, then display the memory
- allocated to each VM.
- .NOTES
- File Name : get-VMMemory.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- and HyperV module from Codeplex
- http://pshyperv.codeplex.com
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- .EXAMPLE
- PSH [C:\Foo]: c:\foo\get-VMMemory.ps1 Foo21
- On Server Foo21 there are 2 VMs as follows:
- Virtual Machine Memory
- SQL-2008-R2-CTP 768
- Server-2008-R2 768
- .EXAMPLE
- PSH [C:\Foo]: "Server2" | c:\foo\get-VMMemory.ps1 Foo21
- On Server Server2 there are 3 VMs as follows:
- Virtual Machine Memory
- Server-2008-R2-1 768
- Server-2008-R2-2 768
- Server-2008-R2-3 768
- #>
- # Parameter is the machine to look at
- param (
- [Parameter(Position=0, Mandatory=$false,ValueFromPipeLine=$true)]
- [string] $Computer = "MyServer"
- )
- ##
- # Start of script
- ##
- # Import the module
- Import-Module HyperV
- # Get all the VMs on the target system
- $Vms = Get-Vm -Server $Computer
- # Now loop through the VMs and display memory allocated
- "On Server {0} there are {1} VMs as follows:" -f $Computer,$Vms.count
- "{0,-30} {1,10}" -f "Virtual Machine", "Memory"
- Foreach ($Vm in $Vms) {
- $Mem = Get-Vmmemory $Vm
- "{0,-30} {1,10}" -f $Vm.Elementname,$Mem.Virtualquantity
- }
- # End of script
This blog contains PowerShell scripts, more PowerShell scripts and still more PowerShell scripts. Occasionally you may see some organisational posts.
Saturday, 9 January 2010
Get-VMMemory
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
- <#
- .SYNOPSIS
- This script displays a number formatted in Currency for each locale
- .DESCRIPTION
- This script first creates a value to be formatted, and creates an array
- containing all the locales defined on the system. The script then uses
- each locale to format the value as currency.
- .NOTES
- File Name : Get-LocaleCurrency.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell V2
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- .EXAMPLE
- Left as an exercise for the reader! NB: the output will look better
- is this script is run in PowerShell ISE vs PowerShell console.
- #>
- ##
- # Start of script
- ##
- #Create a value to be formatted
- [int] $Value = 100
- # get all hte locales defined in the system
- $L=[system.Globalization.CultureInfo]::GetCultures('AllCultures') | sort lcid
- foreach ($C in $L) {
- $C=New-Object System.Globalization.CultureInfo $C.Name
- if (!$C.IsNeutralCulture){
- "{0,-50} {1,-6} {2}" -f $C.Displayname,$C.Name,$Value.ToString("C",$c)
- }
- }
Saturday, 14 November 2009
Protect-ByteArray.ps1
- <#
- .SYNOPSIS
- This script encrpyts then decrypts a byte string
- .DESCRIPTION
- This script uses System.Security to encrpyt a byte
- string, then decrypts it.
- .NOTES
- File Name : Protect-ByteArray.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell V2
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN Sample posted at:
- http://msdn.microsoft.com/en-us/library/system.security.cryptography.protectedmemory.aspx
- .EXAMPLE
- PSH [C:\foo]: .Protect-ByteArray.ps1'
- Unencrpyted byte string:
- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 6
- Encrpyted byte string:
- 199
- 52
- 177
- 169
- 162
- 117
- 118
- 127
- 180
- 16
- 230
- 70
- 19
- 89
- 85
- 168
- Unencrpyted byte string:
- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 6
- #>
- ##
- # Start of script
- ##
- # Load System.Security
- [void] [Reflection.Assembly]::LoadWithPartialName("System.Security")
- # Create and display a byte string
- [byte[]] $Secret = 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3,6
- "Unencrpyted byte string:"
- $Secret
- # now encrypt it and display the encrpyted string
- [System.Security.Cryptography.ProtectedMemory]::Protect($Secret,[System.Security.Cryptography.MemoryProtectionScope]::SameLogon)
- "Encrpyted byte string:"
- $Secret
- # Now decrypt it and re-display it - it's the same byte array we started with
- [System.Security.Cryptography.ProtectedMemory]::UnProtect($Secret,[System.Security.Cryptography.MemoryProtectionScope]::SameLogon)
- "Unencrpyted byte string:"
- $Secret
- #End of Script
Thursday, 29 October 2009
Restart-DNS.ps1
- <#
- .SYNOPSIS
- This script restarts the DNS Service on a Remote System
- .DESCRIPTION
- This script uses WMI to reach out and restart the DNS
- DNS service on a remote machine. in my home environment,
- I find the DNS service on the home DC fails and needs
- to be restarted - this script works!
- .NOTES
- File Name : Restart-DNS.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell V2
- .LINK
- This script posted to:
- http://pshscripts.blogspot.com/2009/10/restart-dnsps1.html
- .EXAMPLE
- PSH [C:\foo]: .\Restart-DNS.ps1
- Restarting DNS Service on: Cookham1
- DNS Service stopped
- DNS Service started
- #>
- ##
- # Start Script
- ##
- # Set server to reset and display our intention
- $Server = "Cookham1"
- "Restarting DNS Service on: $Server"
- # Get DNS service
- $dns = gwmi win32_service -computer $Server | where {$_.name -eq "DNS"}
- # Now stop it
- $ret = $dns.stopservice()
- if ($ret.returnvalue -eq 0) {"DNS Service stopped"}
- else {"DNS Service not stopped"}
- # And now restart it
- $ret = $dns.startservice()
- if ($ret.returnvalue -eq 0) {"DNS Service started"}
- else {"DNS Service not started"}
- # End of Script
Saturday, 24 October 2009
Get-CARolesOfCaller.ps1
- <#
- .SYNOPSIS
- This script displays the CA roles of the caller
- .DESCRIPTION
- This script instantiates the CA COM object, gets
- the allowed roles, and displays them. This script
- also shows use of Bitwise And operations, typical
- when using output from API calls. Based on an earlier
- script by Vadims Podans.
- .NOTES
- File Name : Get-CARolesofCaller.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell V2
- .LINK
- This script posted to:
- http://pshscripts.blogspot.com/2009/10/get-carolesofcallerps1.html
- MSDN Sample posted at:
- http://msdn.microsoft.com/en-us/library/aa383243%28VS.85%29.aspx
- .EXAMPLE
- PSH [C:\foo]: .\Get-CARolesOfCaller.ps1'
- You have the following rights on this CA: Cookham11\Cookham-CookhamCA
- CA administrator
- CA officer
- CA auditor
- CA backup
- Enrollment access
- #>
- # Instantiate the COM object
- $CertAdmin = New-Object -com "CertificateAuthority.Admin.1"
- # Now get the roles assigned to me
- $CA = "Cookham11\Cookham-CookhamCA"
- $MyRoles = $CertAdmin.GetMyRoles([string] $CA)
- #Display Granular Rights
- "You have the following rights on this CA: {0}" -f $CA
- switch ($MyRoles){
- {$MyRoles -band 1} {" CA administrator"}
- {$MyRoles -band 2} {" CA officer"}
- {$MyRoles -band 4} {" CA auditor"}
- {$MyRoles -band 8} {" CA backup"}
- {$MyRoles -band 256} {" CA Read access"}
- {$MyRoles -band 512} {" Enrollment access"}
- default {" No CA Access"}
- }
Wednesday, 21 October 2009
Get-DNSInfo.ps1
- <#
- .SYNOPSIS
- This script re-implements a MSDN Sample in PowerShell that
- shows the use of System.Net.NetworkInformation.NetworkInterface
- .DESCRIPTION
- This script gets network information and formats DNS info. The
- script is effectively a one-liner!
- .NOTES
- File Name : Get-DNSInfo.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell V2
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN Sample posted at:
- http://msdn.microsoft.com/en-us/library/system.net.networkinformation.networkinterface.getallnetworkinterfaces.aspx
- .EXAMPLE
- PSH [C:\foo]: . 'E:\PowerShellScriptLib\System.Net.NetworkInformation\Get-DNSInfo.ps1'
- DnsSuffix IsDnsEnabled IsDynamicDnsEnabled
- --------- ------------ -------------------
- cookham.net False True
- cookham.net False True
- False True
- cookham.net False True
- #>
- ##
- # Start of script
- ##
- #Get information and print - do this as a 1-liner!
- [System.Net.Networkinformation.NetworkInterface]::GetAllNetworkInterfaces() | %{$_.GetIpProperties()} | FT Dnssuffix,Isdnsenabled,IsDynamicDnsEnabled -a
- # End of script
Thursday, 8 October 2009
Get-OutlookFolders.ps1
- <#
- .SYNOPSIS
- This script displays the MAPI namespace in the current
- Outlook Profile.
- .DESCRIPTION
- This script creates and Outlook.application object,
- then gets and displays the top level folders in the store, and
- the folders one level below.
- .NOTES
- File Name : Get-OutlookFolders.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell V2
- .LINK
- This script posted to:
- http://pshscripts.blogspot.com/2009/10/get-outlookfoldersps1.html
- .EXAMPLE
- PSH [C:\foo]: .\Get-OutLookFolders.PS1
- 3 Top Level Folders:
- Mailbox - Thomas Lee
- Public Folders
- Archive Folders
- Folder 'Mailbox - Thomas Lee' contains the following subfolders:
- Deleted Items
- Grateful Dead
- Inbox
- Outbox
- Sent Items
- Calendar
- Contacts
- Drafts
- Journal
- Junk E-mail
- Notes
- Quarantine
- RSS Feeds
- Sync Issues
- Tasks
- Conversation Action Settings
- Quick Step Settings
- Suggested Contacts
- Folder 'Public Folders' contains the following subfolders:
- Favorites
- All Public Folders
- Folder 'Archive Folders' contains the following subfolders:
- Deleted Items
- Sent Items
- Calendar
- Journal
- Tasks
- #>
- ##
- # Start of Script
- ##
- # First create Outlook object and get the Mapi namespace.
- $Outlook = New-Object -com Outlook.Application
- $Namespace = $outlook.GetNamespace("MAPI")
- # Now display top level folders
- "{0} Top Level Folders: " -f $Namespace.folders.count
- foreach ($Fl in $namespace.Folders) {
- " {0}" -f $Fl.Name}
- ""
- # Now look inside
- foreach ($Folder in $Namespace.Folders) {
- "Folder `'{0}`' contains the following subfolders: " -f $Folder.Name
- foreach ($fl in $Folder.Folders){
- " {0}" -f $Fl.Name
- }
- }
- # end of script
Wednesday, 7 October 2009
New-Folder.ps1
- <#
- .SYNOPSIS
- This script Creates then removes a folder using methods in
- System.IO.Directory.
- .DESCRIPTION
- This script checks to see if a staticly named folder exists.
- if not, it creates then removes the folder. The creation/deletion
- logic in enclosed within a try/catch block to capture errors.
- .NOTES
- File Name : New-Folder.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell V2
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN Sample posted at:
- http://msdn.microsoft.com/en-us/library/54a0at6s.aspx
- .EXAMPLE
- PSH [C:\foo]: .\New-Folder.ps1'
- The directory was created successfully at 10/7/2009 12:00:35 PM.
- The directory was deleted successfully.
- #>
- # Specify the directory to manipulate
- $path = "c:\fooxx"
- # try to see if the dirctory exists (It should not!)
- try {
- if ([System.Io.Directory]::Exists($path)) {
- "That path exists already."
- return
- }
- # Try to create the directory
- $di = [System.Io.Directory]::CreateDirectory($path)
- # Get creattion time and display results
- $dit = [System.Io.Directory]::GetCreationTime($path)
- "The directory was created successfully at {0}." -f $dit
- # Delete the directory.
- $di.Delete()
- "The directory was deleted successfully."
- }
- catch {
- "The process failed"
- }
Tuesday, 6 October 2009
Create-TempFile.ps1
- <#
- .SYNOPSIS
- This script creates then copies a file using
- System.IO.Fileinfo.CopyTo()
- .DESCRIPTION
- This script first creates a temporary file, then
- copies it to another file then deletes the
- second file.
- .NOTES
- File Name : Create-TempFile.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell V2
- .LINK
- This script posted to:
- http://pshscripts.blogspot.com/2009/10/create-tempfile.html
- MSDN Sample posted at:
- http://msdn.microsoft.com/en-us/library/system.io.fileinfo.aspx
- .EXAMPLE
- PSH [C:\foo]: .\Create-TempFile.PS1'
- File C:\Users\tfl\AppData\Local\Temp\tmpF2C1.tmp exists
- Original file:
- -----------
- Hello
- And
- Welcome
- -----------
- C:\Users\tfl\AppData\Local\Temp\tmpF2C1.tmp was copied to C:\Users\tfl\AppData\Local\Temp\tmpF2E1.tmp
- C:\Users\tfl\AppData\Local\Temp\tmpF2E1.tmp was successfully deleted.
- #>
- ##
- # start of script
- ##
- # Create temp file and FileInfo object
- $Path = [System.Io.Path]::GetTempFileName()
- $fi1 = New-Object System.Io.FileInfo $Path
- # See if file exists
- if ((Ls $fi1)) {
- "File {0} exists" -f $fi1
- # Create a file to write to and write to it
- $sw = $fi1.CreateText()
- $result=$sw.WriteLine("Hello")
- $result=$sw.WriteLine("And")
- $result=$sw.WriteLine("Welcome")
- $sw.Close()
- }
- # Open the file to read from.
- $sr = $fi1.OpenText()
- $s = "";
- "Original file:";"-----------"
- while (($s = $sr.ReadLine()) -ne $null) {
- $s
- }
- "-----------"
- try {
- $path2 = [System.IO.Path]::GetTempFileName()
- $fi2 = New-Object system.IO.FileInfo $path2
- # Ensure that the target does not exist.
- $fi2.Delete()
- # Copy the file
- $result = $fi1.CopyTo($path2)
- "{0} was copied to {1}" -f $path, $path2
- # Delete the newly created file.
- $fi2.Delete()
- "{0} was successfully deleted." -f $path2
- }
- catch {
- "The process failed:";$Error[0]
- }
- # End of Script
- "-----------"