- <#
- .SYNOPSIS
- This script creates a function to run culture sensitive operations
- in some other culture.
- .DESCRIPTION
- The Using-Culture function gets the current culture, saves it, then
- runs the script block in the requested culture. The function then
- restores the culture. The script then ends with calls to
- Using-Culture to demonstrate its use.
- The script is based on a blog note at: http://blogs.msdn.com/powershell/archive/2006/04/25/583235.aspx.
- .NOTES
- File Name : Using-Culture.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://pshscripts.blogspot.com/2010/02/using-cultureps1.html
- .EXAMPLE
- run the script and see!
- #>
- # Defind the Using-Culture function
- Function Using-Culture {
- Param (
- [System.Globalization.CultureInfo]$culture = (throw "USAGE: Using-Culture -Culture culture -Script {scriptblock}"),
- [ScriptBlock]$script= (throw "USAGE: Using-Culture -Culture culture -Script {scriptblock}")
- )
- $OldCulture = [System.Threading.Thread]::CurrentThread.CurrentCulture
- trap
- {
- [System.Threading.Thread]::CurrentThread.CurrentCulture = $OldCulture
- }
- [System.Threading.Thread]::CurrentThread.CurrentCulture = $culture
- Invoke-Command $script
- [System.Threading.Thread]::CurrentThread.CurrentCulture = $OldCulture
- }
- ##
- # Now use the function
- ##
- # Here is an example of Using-Culture
- "Get-Date using ar-IQ"
- using-culture ar-IQ {get-date}
- ""
- # Parse ar-IQ formatted date into local culture
- "Parse IQ date to English and display"
- $IQD = "30 تشرين الثاني, 2005 09:01:38 ص"
- using-culture ar-IQ {$global:d=[DateTIme]::Parse($IQD)}
- $IQD
- $d
- ""
- # Now do it in German
- "Get-Date using de-de"
- using-culture de-de {get-date}
- ""
- "Parsing a german date into local"
- $ded="Mittwoch, 30. November 2005 09:02:29"
- using-culture de-de {$global:d=[DateTIme]::Parse($DED)}
- $ded
- $d
This blog contains PowerShell scripts, more PowerShell scripts and still more PowerShell scripts. Occasionally you may see some organisational posts.
Sunday, 28 February 2010
Using-Culture.ps1
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
- <#
- .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
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