- <#
- .SYNOPSIS
- This script re-implements an MSDN Sample showing the
- use of the NumberFormatInfo class to nicely format things
- in this case, currency.
- .DESCRIPTION
- This script iterates through the Windows cultures and
- displays those whose 2-letter ISO code is 'en' and
- displays how Windows formats currency in that culture.
- .NOTES
- File Name : Show-CurrencyFormatting.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN sample posted to:
- http://msdn.microsoft.com/en-us/library/system.globalization.numberformatinfo.aspx
- .EXAMPLE
- Psh > .\Show-CurrencyFormatting.ps1
- The currency symbol for 'English (United States)' is '$'
- The currency symbol for 'English (United Kingdom)' is '£'
- The currency symbol for 'English (Australia)' is '$'
- The currency symbol for 'English (Canada)' is '$'
- The currency symbol for 'English (New Zealand)' is '$'
- The currency symbol for 'English (Ireland)' is '€'
- The currency symbol for 'English (South Africa)' is 'R'
- The currency symbol for 'English (Jamaica)' is 'J$'
- The currency symbol for 'English (Caribbean)' is '$'
- The currency symbol for 'English (Belize)' is 'BZ$'
- The currency symbol for 'English (Trinidad and Tobago)' is 'TT$'
- The currency symbol for 'English (Zimbabwe)' is 'Z$'
- The currency symbol for 'English (Republic of the Philippines)' is 'Php'
- The currency symbol for 'English (Singapore)' is '$'
- The currency symbol for 'English (Malaysia)' is 'RM'
- The currency symbol for 'English (India)' is 'Rs.'
- #>
- # Loop through all the specific cultures known to the CLR.
- foreach ($ci in [System.Globalization.CultureInfo]::GetCultures([System.Globalization.CultureTypes]::SpecificCultures))
- {
- # Only show the currency symbols for cultures that speak English.
- if ($ci.TwoLetterISOLanguageName -eq "en") {
- # Display the culture name and currency symbol.
- $nfi = $ci.NumberFormat
- "The currency symbol for '{0}' is '{1}'" -f $ci.DisplayName, $nfi.CurrencySymbol
- }
- }
This blog contains PowerShell scripts, more PowerShell scripts and still more PowerShell scripts. Occasionally you may see some organisational posts.
Monday, 31 October 2011
Show-CurrencyFormatting.ps1
Show-CalendarAlgorithm.ps1
- <#
- .SYNOPSIS
- This script re-implements an MSDN Sample showing the
- CalendarAlgorithmType enumeration.
- .DESCRIPTION
- This script creates there calendars and displays
- algorithm type,
- .NOTES
- File Name : Show-CalendarAlgorithm.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN sample posted to:
- http://msdn.microsoft.com/en-us/library/system.globalization.calendaralgorithmtype.aspx
- .EXAMPLE
- #>
- # Helper function
- Function Display{
- Param ([System.Globalization.Calendar] $c)
- $name = $c.ToString().PadRight(50, '.')
- "{0} {1}" -f $name, $c.AlgorithmType
- }
- ## Start of script
- # Create three new calendars
- $grCal = new-object System.Globalization.GregorianCalendar
- $hiCal = new-object System.Globalization.HijriCalendar
- $jaCal = new-object System.Globalization.JapaneseLunisolarCalendar
- # Display them
- Display($grCal);
- Display($hiCal);
- Display($jaCal);
- [enum]::GetNames([System.Globalization.CalendarAlgorithmType])
Show-CurrencyGroupSize
- <#
- .SYNOPSIS
- This script reimplements a code sample from MSDN in PowerShell.
- This sample formats and display currency using standard and
- different currency groupings.
- .DESCRIPTION
- This script displays a currency using standard, then two
- custom CurrencyGroupSizes.
- .NOTES
- File Name : Show-CurrencyGroupSize.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN sample posted tot:
- http://msdn.microsoft.com/en-us/library/system.globalization.numberformatinfo.currencygroupsizes.aspx
- .EXAMPLE
- PSH:> .\Show-CurrencyGroupSize.ps1
- Default numeric format string "C"
- $123,456,789,012,345.00
- Display with array = 2,3,4
- $12,3456,7890,123,45.00
- Display with array = 2,3,0
- $1234567890,123,45.00
- #>
- # Get a NumberFormatInfo associated with the en-US culture.
- $fi = new-object System.Globalization.CultureInfo "en-US", false
- $nfi = $fi.NumberFormat
- # Display a value with the default separator (".")
- "Default numeric format string `"C`""
- [Int64] $myInt = 123456789012345
- $myInt.ToString( "C", $nfi )
- # Display the same value with different groupings.
- [int[]] $mySizes1 = (2,3,4)
- $mySizes = 2,3,0
- "";"Display with array = 2,3,4"
- $nfi.CurrencyGroupSizes = $mySizes1
- $myInt.ToString( "C", $nfi )
- "";"Display with array = 2,3,0"
- $nfi.CurrencyGroupSizes = $mySizes2
- $myInt.ToString( "C", $nfi )
Show-StandardNFS-1.ps1
- <#
- .SYNOPSIS
- This script shows the first two samples on this MSDN Page,
- convered to PowerShell
- .DESCRIPTION
- This script displays two lines of text using PowerShell
- and .NET formatting information
- .NOTES
- File Name : Show-StandardNFS-1.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN sample posted tot:
- http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx
- .EXAMPLE
- [Cookham8:C:\foo]> .\Show-StandardNFS.ps1
- $123.46
- Your account balance is $123.46.
- #>
- # Show formatting a number as currency
- $value = 123.456
- $value.ToString("C2")
- # Show the -f operator with currency formatting
- $value = 123.456
- "Your account balance is {0:C2}." -f $value
Labels:
code,
powershell,
PowerShell scripts,
ToString()
Tuesday, 11 October 2011
Get-OSInstallDate.ps1
- <#
- .SYNOPSIS
- This script gets/displays the Sysgtem Uptime after
- converting it from WEBM time/date format
- .DESCRIPTION
- This script creates an instance of a SWbemDateTime object,
- gets the OS install date and converts the date to a more
- useful format.
- .NOTES
- File Name : Get-OSInstallDate.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN sample posted to:
- http://msdn.microsoft.com/en-us/library/aa393687%28VS.85%29.aspx
- .EXAMPLE
- Psh [C:\foo]> .\Get-OsInstallDate.ps1
- This OS was installed in the year 2008
- Full installation date (VT_DATE format) is 4/9/2008 8:17:23 PM
- Full installation date (FILETIME format) is 128522458430000000
- #>
- # Create swbemdatetime object
- $datetime = New-Object -ComObject WbemScripting.SWbemDateTime
- # Get OS installation time and assign to datetime object
- $os = Get-WmiObject -Class Win32_OperatingSystem
- $dateTime.Value = $os.InstallDate
- # Now display the time
- "This OS was installed in the year {0}" -f $dateTime.Year
- "Full installation date (VT_DATE format) is {0}" -f $dateTime.GetVarDate()
- "Full installation date (FILETIME format) is {0}" -f $dateTime.GetFileTime()
- ##
Labels:
powershell,
PowerShell scripts,
SWbemDateTime,
wmi
Friday, 7 October 2011
Register-Event1.ps1
- <#
- .SYNOPSIS
- This script registers for a WMI event
- .DESCRIPTION
- This script used PowerShell to register for then display
- an event. This script is a re-write on an MSDN Sample.
- .NOTES
- File Name : Register-Event1.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN sample posted tot:
- http://msdn.microsoft.com/en-us/library/aa393013%28VS.85%29.aspx
- .EXAMPLE
- [PSH] C:\FOO> Register-Event1.ps1
- Waiting for events
- Log Event Occured
- EVENT MESSAGE
- A logon was attempted using explicit credentials.
- Subject:
- Security ID: S-1-5-21-2824006062-479960714-4144511058-1105
- Account Name: tfl
- ... -> Reminder snipped for brevity
- #>
- # Define event Query
- $query = "SELECT * FROM __InstanceCreationEvent
- WHERE TargetInstance ISA 'Win32_NTLogEvent' "
- # Register for event - also specify an action that
- # displays the log event when the event fires.
- Register-WmiEvent -Source Demo1 -Query $query -Action {
- Write-Host "Log Event occured"
- $global:myevent = $event
- Write-Host "EVENT MESSAGE"
- Write-Host $event.SourceEventArgs.NewEvent.TargetInstance.Message}
- # So wait
- "Waiting for events"
Labels:
powershell,
PowerShell scripts,
wmi
Friday, 30 September 2011
Get-FolderObjects.ps1
- <#
- .SYNOPSIS
- This script displays the WMI objects associated with a folder
- .DESCRIPTION
- This script uses the Associators Of query
- .NOTES
- File Name : get-folderobjects.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- .EXAMPLE
- Get-FolderObjects "C:\Foo"
- what's in C:\foo
- 0 Volume objects
- 0 Logical Disk objects
- 10 Directory objects
- 171 File objects
- 0 Pagefile objects
- 1 Security Settings
- 0 Share objects
- 0 other objects
- .EXAMPLE
- Get-FolderObjects "C:\"
- what's in C:\
- 1 Volume objects
- 1 Logical Disk objects
- 18 Directory objects
- 8 File objects
- 1 Pagefile objects
- 1 Security Settings
- 1 Share objects
- 0 other objects
- #>
- # Function to get folder WMI object associations
- # Define function
- Function Get-FolderObjects {
- Param (
- $Folder = "c:\"
- )
- # Set query then get associations
- $query = "ASSOCIATORS OF {Win32_Directory.Name='$folder'}"
- $objs = Gwmi -q $query
- #
- # Now group them
- # Create empty arrays
- $directory = @()
- $datafile = @()
- $pagefile = @()
- $volume = @()
- $filesec = @()
- $share = @()
- $Logdisk = @()
- $unknown = @()
- # Now fill the arrays
- foreach ($obj in $objs) {
- switch ($Obj.__class) {
- "Win32_Directory" {$directory += $obj; break}
- "Cim_DataFile" {$datafile += $obj; break}
- "Win32_PageFile" {$pagefile += $obj; break}
- "Win32_Volume" {$volume += $obj; break}
- "Win32_LogicalDisk" {$logdisk += $obj; break}
- "Win32_LogicalFileSecuritySetting" {$filesec += $obj; break}
- "Win32_share" {$share += $obj; break}
- default {$unknown += $obj; break}
- }
- }
- # Display the output
- " what's in $folder"
- "{0} Volume objects" -f $volume.count
- "{0} Logical Disk objects" -f $logdisk.count
- "{0} Directory objects" -f $directory.count
- "{0} File objects" -f $datafile.count
- "{0} Pagefile objects" -f $pagefile.count
- "{0} Security Settings" -f $filesec.count
- "{0} Share objects" -f $share.count
- "{0} other objects" -f $unknown.count
- }
- # Here call function as an example
- Get-FolderObjects "C:\"
- "***"
- Get-FolderObjects "C:\foo"
Sunday, 25 September 2011
Set-ComputerName.ps1
- <#
- .SYNOPSIS
- This script renames a computer using WMI.
- .DESCRIPTION
- This script uses the Rename method from
- the Win32_OperatingSystem WMI class.
- This is sample 5 from http://msdn.microsoft.com/en-us/library/aa394586
- .NOTES
- File Name : Set-ComputerName.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN sample posted tot:
- http://msdn.microsoft.com/en-us/library/aa394586%28VS.85%29.aspx
- .EXAMPLE
- Left as an exercise for the Reader
- #>
- # This script takes two parameters, The computer to
- # rename and then the newname for that computer
- param (
- [$String] $NewName = 'NewName',
- [$string] $Comp = "."
- }
- # Get computer object
- $Computer = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $comp
- #Rename the Computer
- $Return = $Computer.Rename($NewName)
- if ($return.ReturnValue -eq 0) {
- "Computer name is now: $NewName"
- " but you need to reboot first"
- } else {
- " RenameFailed, return code: {0}" -f $return.ReturnValue
- }
Tuesday, 13 September 2011
Remove-WmiRegistryKey.ps1
- <#
- .SYNOPSIS
- This script creates removes registry key using WMI.
- .DESCRIPTION
- This script uses WMI to get remove registry key.
- This script deletes the key and everything below
- it in the registry - use carefully!
- .NOTES
- File Name : Remove-WmiRegistryKey.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN sample posted to:
- http://msdn.microsoft.com/en-us/library/aa394600%28VS.85%29.aspx
- .EXAMPLE
- Psh[C:\foo]>New-WMIRegistryKey.ps1
- Key removed
- #>
- # Define Constants
- $HKEY_Local_Machine =2147483650
- $computer ='.'
- # Get Class to call static methods on
- $reg = [WMIClass]"ROOT\DEFAULT:StdRegProv"
- # Define key to create
- $Key = "SOFTWARE\NewKey"
- # Create key and display reslts
- $results = $reg.DeleteKey($HKEY_LOCAL_MACHINE, $Key)
- If ($results.Returnvalue -eq 0) {"Key Removed"}
Labels:
DeleteKey,
powershell,
StdRegProv,
wmi
Monday, 12 September 2011
Get-WmiRegistryBinaryValue.ps1
- <#
- .SYNOPSIS
- This script Gets and displays a registry
- binary value using WMI.
- .DESCRIPTION
- This script uses WMI to get, then display
- a binary registry Value.
- This is a re-write of a VB Sample Script.
- .NOTES
- File Name : Set-WmiRegistryBinaryValue.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN sample posted to:
- http://msdn.microsoft.com/en-us/library/aa394600%28VS.85%29.aspx
- .EXAMPLE
- Psh[C:\foo]>Get-WmiRegistryBinaryValue.ps1
- 54
- 46
- 4c
- #>
- # Define Constants
- $HKEY_Local_Machine =2147483650
- $computer ='.'
- # Get Class to call static methods on
- $reg = [WMIClass]"ROOT\DEFAULT:StdRegProv"
- # Define key to create
- $ValueName = "Example Binary Value"
- $Values = @(0x54, 0x46, 0x4C)
- $Key = "SOFTWARE\NewKey"
- # Create Value entry
- $results = $reg.GetBinaryValue($HKEY_LOCAL_MACHINE, $Key, $ValueName)
- Foreach ($byte in $results.uvalue) {"{0}" -f $byte.tostring("x")}
W
Subscribe to:
Posts (Atom)