- ####
- # Configure-DC1-2
- # Configures DC1 after dcpromo is completed
- #
- # Version Date What Changed
- # ------- ----------- -------------------------------------------
- # 1.0.0 14 Jan 2013 Initial release
- # 1.1.0 24 Jan 2013 Added code to count how long it all took,
- # Added checkpoint at the end of this script
- # 1.1.1 25 Jan 2013 Added auto admin logon
- # 1.1.2 5 Feb 2013 Added forced reboot of DC1-1 at script end
- ####
- # Configuration block
- $Conf = {
- $StartTime = Get-Date
- Write-Host "Starting at: $StartTime"
- # Set Credentials for use in this configuration block
- $User = "Reskit\Administrator"
- $Password = 'Pa$$w0rd'
- $PasswordSS = ConvertTo-SecureString -String $Password –AsPlainText `
- -Force
- $Dom = 'Reskit'
- $CredRK = New-Object `
- -Typename System.Management.Automation.PSCredential `
- -Argumentlist $User,$PasswordSS
- # Define registry path for autologon, then set admin logon
- $RegPath = 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon'
- Set-ItemProperty -Path $RegPath -Name AutoAdminLogon `
- -Value 1 -EA 0
- Set-ItemProperty -Path $RegPath -Name DefaultUserName `
- -Value $User -EA 0
- Set-ItemProperty -Path $RegPath -Name DefaultPassword `
- -Value $Password -EA 0
- Set-ItemProperty -Path $RegPath -Name DefaultDomainName `
- -Value $Dom -EA 0
- # Install key Windows features for labs
- Write-Verbose 'Installing Windows fetures needed for DC1'
- $Features = @('PowerShell-ISE',
- 'Hyper-V-PowerShell',
- 'Rsat-AD-PowerShell',
- 'Web-Server','Web-Mgmt-Tools',
- 'Web-Mgmt-Console',
- 'Web-Scripting-Tools',
- 'Telnet-Client')
- Install-WindowsFeature @Features -IncludeManagementTools -Verbose
- # Install and configure DHCP
- Write-Verbose -Message 'Adding and then configuring DHCP'
- Install-WindowsFeature DHCP -IncludeManagementTools
- Add-DhcpServerV4Scope -Name "ReskitNet0" `
- -StartRange 10.0.0.100 `
- -EndRange 10.0.0.119 `
- -SubnetMask 255.255.255.0
- Set-DhcpServerV4OptionValue -DnsDomain Reskit.Org `
- -DnsServer 10.0.0.10
- Add-DhcpServerInDC -DnsName Dc1.reskit.org
- # Add users to the AD and then add them to some groups
- # Hash table for common new user paraemters
- Write-Verbose -Message
- $NewUserHT = @{AccountPassword = $PasswordSS;
- Enabled = $true;
- PasswordNeverExpires = $true;
- ChangePasswordAtLogon = $false
- }
- # Create one new user (me!) and add to enterprise and domain admins security groups
- New-ADUser @NewUserHT -SamAccountName tfl `
- -UserPrincipalName 'tfl@reskit.org' `
- -Name "tfl" `
- -DisplayName 'Thomas Lee'
- Add-ADPrincipalGroupMembership `
- -Identity "CN=tfl,CN=Users,DC=reskit,DC=org" `
- -MemberOf "CN=Enterprise Admins,CN=Users,DC=reskit,DC=org" ,
- "CN=Domain Admins,CN=Users,DC=reskit,DC=org"
- # Say nice things and finish
- $FinishTime = Get-Date
- Write-Verbose "Finished at: $FinishTime"
- Write-Verbose "DC1 Configuration took $(($FinishTime - $StartTime).TotalSeconds.ToString('n2')) seconds"
- } # End Conf configuration script block
- # Start of script proper
- # Set Credentials
- $Username = "Reskit\administrator"
- $Password = 'Pa$$w0rd'
- $PasswordSS = ConvertTo-SecureString -String $Password -AsPlainText
- -Force
- $CredRK = New-Object -Typename System.Management.Automation.PSCredential `
- -Argumentlist $Username,$PasswordSS
- # Following code used to test the credentials. Remove the comments on next two lines the first time you
- # run this script
- Invoke-Command -ComputerName DC1 -ScriptBlock {hostname} -Credential $Credrk -verbose
- Pause
- Invoke-Command -ComputerName DC1 -Scriptblock $conf -Credential $CredRK `
- -Verbose
- # OK - script block has completed - reboot the system and wait till it comes up
- Restart-Computer -ComputerName DC1 -Wait -For PowerShell –Force
- -Credential $CredRK
- # Finally, run a post-DCPromo snapshot
- Checkpoint-VM -VM $(Get-VM DC1) `
- -SnapshotName "DC1 - post configuration by ConfigureDC1-2.ps1"
This blog contains PowerShell scripts, more PowerShell scripts and still more PowerShell scripts. Occasionally you may see some organisational posts.
Showing posts with label PowerShell scripts. Show all posts
Showing posts with label PowerShell scripts. Show all posts
Monday, 4 February 2013
Configure-DC1-2.ps1
Labels:
powershell,
PowerShell scripts,
PowerShell v3
Wednesday, 23 January 2013
Configure-DC1-1.ps1
- # Configure-DC1-1.ps1
- # Converts Server to DC
- # Version 1.0.0 - 14 Jan 2013
- # See http://tfl09.blogspot.com for more details
- # Config script block
- $conf = {
- # Install the AD - the reboot is or should be automagic
- Install-Windowsfeature AD-Domain-Services -IncludeManagementTools
- # Now install the AD to DC1
- $PasswordSS = ConvertTo-SecureString -string 'Pa$$w0rd' -AsPlainText -Force
- Install-ADDSForest -DomainName Reskit.Org -SafeModeAdministratorPassword $PasswordSS -force -InstallDNS -DomainMode Win2012 -ForestMode Win2012
- }
- # Here is start of script
- $Username = "DC1\administrator"
- $PasswordSS = ConvertTo-SecureString 'Pa$$w0rd' -AsPlainText -Force
- $Creddc1 = New-Object system.management.Automation.PSCredential $username,$PasswordSS
- # First, run a simple script block to check that the server is actually running and is the one we think it is
- Invoke-Command -ComputerName DC1 -ScriptBlock { ipconfig;hostname} -Credential $Creddc1 -verbose
- Pause
- # Now add create our forest/domain/dc
- Invoke-Command -ComputerName DC1 -Scriptblock $conf -Credential $Creddc1 -verbose
Labels:
deployment scripts,
Hyper-V,
powershell,
PowerShell scripts
Monday, 21 January 2013
Create-VM.PS1
- # Create-VM.ps1
- # Script that creates VMs
- # Version 1.0.0 - 20 Jan 2013
- # See http://tfl09.blogspot.co.uk/2013/01/building-hyper-v-test-lab-on-windows-8.html
- # First define the Create-VM Function
- Function Create-VM {
- #===================================================
- # Create a New VM
- #===================================================
- # Parameters are Name, Virtual Machine Path, path to reference Vhdx,
- # network switch to use, VM Memory, Unattend file, IP address and DNS
- # Server to set. Default values are specified in the Param block,
- # but these are normally overridden in the call to Create0VM
- [Cmdletbinding()]
- Param (
- $Name = "Server",
- $VmPath = "C:\v3",
- $ReferenceVHD = "C:\v3\Ref2012.vhdx",
- $Network = "Internal",
- $VMMemory = 512mb,
- $UnattendXML = "C:\v3\unattend.xml",
- $IPAddr = '10.0.0.250/24',
- $DnsSvr = '10.0.0.10'
- )
- $Starttime = Get-Date
- Write-Verbose "Starting Create-VM at $Starttime"
- Write-verbose "Creating VM: [$name]"
- Write-verbose "Path to VM : [$VMpath]"
- # Set path to differencing disk location
- $path = "$vmpath\$name.vhdx"
- Write-Verbose "Creating Disk at [$path]"
- # Add a new differencing VHDX, Based on parent parent
- $vmDisk01 = New-VHD –Path $path -Differencing –ParentPath $ReferenceVHD -ErrorAction Stop
- Write-Verbose "Added VM Disk [$VMdisk01], pointing to [ReferenceVHD]"
- # Create a New VM
- $VM = New-VM –Name $name –MemoryStartupBytes $VMMemory –VHDPath $VMDisk01.path -SwitchName $Network -Path $vmPath
- Write-Verbose "VM [$name] created"
- # Mount the Disk into the VM
- Mount-DiskImage -ImagePath $path
- $VHDDisk = Get-DiskImage -ImagePath $path | Get-Disk
- $VHDPart = Get-Partition -DiskNumber $VHDDisk.Number
- $VHDVolumeName = [string]$VHDPart.DriveLetter
- $VHDVolume = [string]$VHDPart.DriveLetter + ":"
- Write-verbose "Volume [$Volumename] created in VM [$name]"
- # Get Unattended.XML file
- Write-Verbose "Using Unattended XML file [$unattendXML]"
- # Open XML file
- $Xml = [xml](get-content $UnattendXML)
- # Change ComputerName
- Write-Verbose "Setting VM ComputerName to: [$name]"
- $Xml.unattend.settings.component | Where-Object { $_.Name -eq "Microsoft-Windows-Shell-Setup" } |
- ForEach-Object {
- if($_.ComputerName) {
- $_.ComputerName = $name
- }
- }
- # Change IP address
- Write-Verbose "Setting VM ComputerName to: [$name]"
- $Xml.unattend.settings.component | Where-Object { $_.Name -eq "Microsoft-Windows-TCPIP" } |
- ForEach-Object {
- if($_.Interfaces) {
- $ht='#text'
- $_.interfaces.interface.unicastIPaddresses.ipaddress.$ht = $IPAddr
- }
- }
- # Change DNS Server address
- # Use obscure way to create the #TEXT node
- Write-Verbose "Setting VM DNS address to: [$DNSSvr]"
- $Xml.Unattend.Settings.Component | Where-Object { $_.Name -eq "Microsoft-Windows-DNS-Client" } |
- ForEach-Object {
- if($_.Interfaces) {
- $ht='#text'
- $_.Interfaces.Interface.DNSServerSearchOrder.Ipaddress.$ht = $DNSSvr
- }
- }
- # Save XML File on Mounted VHDX differencing disk
- $xml.Save("$VHDVolume\Unattend.XML")
- Write-Verbose "Unattended XML file saved to vhd [$vhdvolume\unattend.xml]"
- # Dismount VHDX
- Write-Verbose "Dismounting disk image: [$Path]"
- Dismount-DiskImage -ImagePath $path
- # Update additional VM settings
- Write-Verbose 'Setting additional VM settings'
- Set-VM -Name $name -DynamicMemory
- Set-VM -Name $name -MemoryMinimumBytes $VMMemory
- Set-VM -Name $name -AutomaticStartAction Nothing
- Set-Vm -Name $name -AutomaticStopAction ShutDown
- # Show what has been created!
- "VM Created:"
- Get-VM -Name $name | fl *
- # Start VM
- Write-verbose "VM [$Name] being started"
- Start-VM -Name $name
- # Now work out and write how long it took to create the VM
- $Finishtime = Get-Date
- Write-Verbose ("Creating VB ($name) took {0} seconds" -f ($FinishTime - $Starttime).totalseconds)
- } # End of Create-VM function
- #######################################################################################################
- # CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS #
- # CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS #
- # CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS #
- # Location of Server 2012 DVD Iso Image
- $iso = 'c:\Builds\9200.16384.120725-1247_x64frev_Server_Datacenter_VL_HRM_SSS_X64FREV_EN-US_DVD.iso'
- # Where we put the reference VHDX
- $ref = 'c:\v3\Ref2012.vhdx'
- # Path were VMs, VHDXs and unattend.txt files live
- $path = 'c:\V3'
- # Location of Unattend.xml - first for workstation systems, second for domain joined systems
- $una = 'c:\V3\UnAttend.xml'
- $unadj = 'c:\V3\UnAttend.dj.xml'
- # CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS #
- # CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS #
- # CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS #
- #######################################################################################################
- # Now run the script to create the VMs as appropriate.
- $Start = Get-Date
- "Create-VM --- Started at: $Start"
- ####################################################################
- # Comment out VMs you do NOT want to create then run the entire script
- # To comment out a VM creation, just add a '#" at the start of the line.
- # Removing the comment line means you want to create that VM.
- # BE creful! If you make a mistake, stop the script. Kill any VMs created, then remove the
- # storage for the VMs.
- #######################################################################################################
- # Create the DC - NON-domained joined
- # Create-VM -name "DC1" -VmPath $path -ReferenceVHD $ref -Network "Internal" -UnattendXML $una -Verbose -IPAddr '10.0.0.10/24' -DNSSvr 10.0.0.10 -VMMemory 1gb
- # Remaining VMs use the domain-join version of unattend.xml
- # Create-VM -name "Srv1" -VmPath $path -ReferenceVHD $ref -Network "Internal" -UnattendXML $unadj -Verbose -IPAddr '10.0.0.30/24' -DNSSvr 10.0.0.10 -VMMemory 512mb
- # Create-VM -name "Srv2" -VmPath $path -ReferenceVHD $ref -Network "Internal" -UnattendXML $unadj -Verbose -IPAddr '10.0.0.31/24' -DNSSvr 10.0.0.10 -VMMemory 512mb
- # Create-VM -name "Sql1" -VmPath $path -ReferenceVHD $ref -Network "Internal" -UnattendXML $unadj -Verbose -IPAddr '10.0.0.20/24' -DNSSvr 10.0.0.10 -VMMemory 768mb
- # Create-VM -name "Exch1" -VmPath $path -ReferenceVHD $ref -Network "Internal" -UnattendXML $unadj -Verbose -IPAddr '10.0.0.21/24' -DNSSvr 10.0.0.10 -VMMemory 768mb
- # DHCP 1,2 for advanced networking class
- # Create-VM -name "DHCP1" -VmPath $path -ReferenceVHD $ref -Network "Internal" -UnattendXML $unadj -Verbose -IPAddr '10.0.0.51/24' -DNSSvr 10.0.0.10 -VMMemory 512mb
- # Create-VM -name "DHCP2" -VmPath $path -ReferenceVHD $ref -Network "Internal" -UnattendXML $unadj -Verbose -IPAddr '10.0.0.52/24' -DNSSvr 10.0.0.10 -VMMemory 512mb
- # Create a second DC for reskit.org for advanced class
- # Create-VM -name "DC2" -vmPath $path -ReferenceVHD $ref -network "Internal" -UnattendXML $unadj -Verbose -IPAddr '10.0.0.11/24' -DNSSvr 10.0.0.10 -VMMemory 512mb
- # script is all done - just say nice things and quit.
- $Finish = Get-Date
- "Create-VM --- Finished at: $Finish"
- "Elapsed Time : $(($Finish-$Start).totalseconds) seconds"
Wednesday, 3 October 2012
Set-PowerShellAsShell
- <#
- .Synopsis
- Creates a function to set PowerShell as GUI in Server 2012
- .DESCRIPTION
- The function in this script sets PowerShell as the
- default shell in Server 2012. When the server is rebooted,
- it runs PowerShell.exe by default. When PowerShell starts, it
- displays the $PSVersionTable variable.
- .NOTES
- File Name : Set-PowerShellAsGui.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : Server 2012
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- .EXAMPLE
- Left as an exercise to the reader
- #>
- Function Set-PowerShellAsShell {
- [CmdletBinding()]
- Param (
- [switch] $Reboot = $false
- )
- # Create Registry Path variable
- $RegPath = "Microsoft.PowerShell.Core\Registry::"
- $RegPath += "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\"
- $RegPath += "Windows NT\CurrentVersion\winlogon"
- # Create splatted parameter hash table
- $parm = @{Path = $regpath} # key
- $parm += @{Name = 'Shell'} # value name
- $parm += @{Value = 'PowerShell.exe –NoExit
-Command "$psversiontable"'} # value’s value - # Set Registry value entry
- Set-ItemProperty @parm
- # And restart to see PowerShell
- if ($Reboot) {Restart-Computer -confirm}
- }
Labels:
powershell,
PowerShell scripts,
PowerShell V2,
PowerShell v3,
Server,
Server2012,
ServerCore
Tuesday, 7 August 2012
Show-Formatting1.ps1
- <#
- .SYNOPSIS
- MSDN Sample Recoded in PowerShell demonstrating formatting
- .DESCRIPTION
- This sample recodes an MSDN Sample into PowerShell that
- shows some of the options of formatting using ToString() and
- various .NET formatting strings
- .NOTES
- File Name : Show-Formatting1.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/0c899ak8.aspx
- .EXAMPLE
- Psh> .\Show-Formatting1.ps1\
- 00123
- 1.20
- 01.20
- 01,20
- 0.6
- 1,234,567,890
- 1.234.567.890
- 1,234,567,890.1
- 1,234.57
- #>
- ## Start script
- [double] $value = 123;
- $value.ToString("00000")
- # Displays 00123
- $value = 1.2;
- $value.ToString("0.00", [System.Globalization.CultureInfo]::InvariantCulture)
- # Displays 1.20
- $value.ToString("00.00",[System.Globalization.CultureInfo]::InvariantCulture)
- # Displays 01.20
- $value.ToString("00.00",
- [System.Globalization.CultureInfo]::CreateSpecificCulture("da-DK"))
- # Displays 01,20
- $value = .56
- $value.ToString("0.0", [System.Globalization.CultureInfo]::InvariantCulture)
- # Displays 0.6
- $value = 1234567890
- $value.ToString("0,0", [System.Globalization.CultureInfo]::InvariantCulture)
- # Displays 1,234,567,890
- $value.ToString("0,0",
- [System.Globalization.CultureInfo]::CreateSpecificCulture("el-GR"))
- # Displays 1.234.567.890
- $value = 1234567890.123456;
- $value.ToString("0,0.0", [System.Globalization.CultureInfo]::InvariantCulture)
- # Displays 1,234,567,890.1
- $value = 1234.567890;
- $value.ToString("0,0.00", [System.Globalization.CultureInfo]::InvariantCulture)
- # Displays 1,234.57
Monday, 30 April 2012
Show-DnsConfiguration.ps1
- <#
- .SYNOPSIS
- This script shows the DNS Configuration of NICs
- in your system
- .DESCRIPTION
- This script is a re-write of an MSDN Sample
- using PowerShell./ The script gets all network
- active network interfaces then prints out that
- interfaces' DNS Properties.
- .NOTES
- File Name : Show-DnsConfiguration.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.net.networkinformation.networkinterface.getallnetworkinterfaces.aspx
- .EXAMPLE
- Psh[C:\foo]> .\Show-DnsConfiguration.ps1
- Broadcom NetXtreme 57xx Gigabit Controller
- DNS suffix .............................. : cookham.net
- DNS enabled ............................. : False
- Dynamically configured DNS .............. : True
- ... more interfaces snipped for brevity!
- #>
- # Get the adapters than iterate over the collection and display DNS configuration
- $adapters = [System.Net.NetworkInformation.NetworkInterface]::GetAllNetworkInterfaces()
- ForEach ($adapter in $adapters) {
- $properties = $adapter.GetIPProperties()
- $adapter.Description
- " DNS suffix .............................. : {0}" -f $properties.DnsSuffix
- " DNS enabled ............................. : {0}" -f $properties.IsDnsEnabled
- " Dynamically configured DNS .............. : {0}" -f $properties.IsDynamicDnsEnabled
- }
Sunday, 29 April 2012
Show-NetworkInterfaces1.ps1
- <#
- .SYNOPSIS
- This script displays the NICs in a system and their physical
- address.
- .DESCRIPTION
- This script is a MSDN Sample recoded in PowerShell. The script
- first gets all the interfaces on the system, then loops through
- them displaying more information about them to the console.
- Note the use of Console.Write in the loop at the end. Not quite
- sure a better PowerShell equivalent other than creating a
- string with all the bytes, then displaying that string.
- .NOTES
- File Name : Show-NetworkInterfaces1.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.net.networkinformation.physicaladdress.aspx
- .EXAMPLE
- PSH[c:\foo]> .\Show-Networkinterfaces1.ps1
- Interface information for Cookham8.cookham.net
- Number of interfaces .................... : 2
- Broadcom NetXtreme 57xx Gigabit Controller
- ==========================================
- Interface type .......................... : Ethernet
- Physical address ........................ : 00-1E-4F-95-5C-C4
- Microsoft ISATAP Adapter
- ========================
- Interface type .......................... : Tunnel
- Physical address ........................ : 00-00-00-00-00-00-00-E0
- #>
- # Get computer IP global properties
- $ComputerProperties = [System.Net.NetworkInformation.IpGlobalProperties]::GetIPGlobalProperties()
- # Get the nics in this system
- $nics = [System.Net.NetworkInformation.NetworkInterface]::GetAllNetworkInterfaces()
- # Show information header
- "Interface information for {0}.{1} " -f $ComputerProperties.HostName, $ComputerProperties.DomainName
- # Iterate through the NIcs ans outout per nic info
- # First, check if interfaces are found
- if (!$nics -OR $nics.Length -LT 1)
- {
- " No network interfaces found."
- return
- }
- # Here print out number of interfaces and interface details
- [System.Console]::WriteLine(" Number of interfaces .................... : {0}" -f $nics.Length)
- Foreach ($adapter in $nics)
- {
- $properties = $adapter.GetIPProperties();
- " ";""
- "{0}" -F $adapter.Description
- "=" * $adapter.Description.Length
- " Interface type .......................... : {0}" -F $adapter.NetworkInterfaceType
- [System.Console]::Write(" Physical address ........................ : ")
- $address = $adapter.GetPhysicalAddress();
- $bytes = $address.GetAddressBytes()
- for($i = 0; $i -lt $bytes.Length; $i++)
- {
- # Display the physical address in hexadecimal.
- [system.Console]::Write("{0}" -f $bytes[$i].ToString("X2"))
- # Insert a hyphen after each byte, unless we are at the end of the
- # address.
- if ($i -NE $bytes.Length -1)
- {
- [System.Console]::Write("-")
- }
- }
- [System.Console]::WriteLine()
- }
Tuesday, 7 February 2012
Test-FileOpen
- <#
- .SYNOPSIS
- This script defines a function that tests to
- see if a file is open.
- .DESCRIPTION
- This script used the System.Io.FileStream class
- and the FileInfo class to try to open a file
- stream for write. If it fails, we return $false,
- else we close the file and return $True
- .NOTES
- File Name : Test-FileOpen.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- .EXAMPLE
- Psh[Cookham8:C:\foo]> $file = New-Object -TypeName System.IO.FileInfo C:\foo\doc1.docx
- Psh[Cookham8:C:\foo]>Test-FileOpen $file
- True
- #>
- Function Test-FileOpen {
- Param (
- $fileName = $(Throw '***** No File specified')
- )
- $ErrorActionPreference = "SilentlyContinue"
- [System.IO.FileStream] $fs = $file.OpenWrite();
- if (!$?) {$true}
- else {$fs.Dispose();$false}
- }
- # Test the function
- $file = New-Object -TypeName System.IO.FileInfo C:\foo\doc1.docx
- Test-FileOpen $file
Sunday, 22 January 2012
New-SpanishCulture.ps1
- <#
- .SYNOPSIS
- This script creates a Spanish cultureinfo object with a traditional
- sort and another with an international sort. The script then compares them.
- .DESCRIPTION
- This script re-implements an MSDN sample.
- .NOTES
- File Name : New-SpanishCulture.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.cultureinfo.aspx
- .EXAMPLE
- C:\foo> .\New-SpanishCulture.ps1
- PROPERTY INTERNATIONAL TRADITIONAL
- CompareInfo CompareInfo - es-ES CompareInfo - es-ES_tradnl
- DisplayName Spanish (Spain) Spanish (Spain)
- EnglishName Spanish (Spain, International Sort) Spanish (Spain, Traditional Sort)
- IsNeutralCulture False False
- IsReadOnly False
- LCID 3082 1034
- Name es-ES es-ES
- NativeName Español (España, alfabetización internacional) Español (España, alfabetización tradicional)
- Parent es es
- TextInfo TextInfo - es-ES TextInfo - es-ES_tradnl
- ThreeLetterISOLanguageName spa spa
- ThreeLetterWindowsLanguageName ESN ESP
- TwoLetterISOLanguageName es es
- Comparing [llegar] and [lugar]
- With myCIintl.CompareInfo.Compare: -1
- With myCItrad.CompareInfo.Compare: 1
- #>
- # Create and initialize the CultureInfo which uses the international sort
- $myCIintl = New-Object System.Globalization.CultureInfo "es-ES", $false
- # Create and initialize the CultureInfo which uses the traditional sort
- $myCItrad = New-Object System.Globalization.CultureINfo 0x040A, $false
- # Display the properties of each culture.
- "{0,-31}{1,-47}{2,-25}" -f "PROPERTY", "INTERNATIONAL", "TRADITIONAL"
- "{0,-31}{1,-47}{2,-25}" -f "CompareInfo", $myCIintl.CompareInfo, $myCItrad.CompareInfo
- "{0,-31}{1,-47}{2,-25}" -f "DisplayName", $myCIintl.DisplayName, $myCItrad.DisplayName
- "{0,-31}{1,-47}{2,-25}" -f "EnglishName", $myCIintl.EnglishName, $myCItrad.EnglishName
- "{0,-31}{1,-47}{2,-25}" -f "IsNeutralCulture", $myCIintl.IsNeutralCulture, $myCItrad.IsNeutralCulture
- "{0,-31}{1,-47}{2,-25}" -f "IsReadOnly", $myCIintl.$IsReadOnly, $myCItrad.IsReadOnly
- "{0,-31}{1,-47}{2,-25}" -f "LCID", $myCIintl.LCID, $myCItrad.LCID
- "{0,-31}{1,-47}{2,-25}" -f "Name", $myCIintl.Name, $myCItrad.Name
- "{0,-31}{1,-47}{2,-25}" -f "NativeName", $myCIintl.NativeName, $myCItrad.NativeName
- "{0,-31}{1,-47}{2,-25}" -f "Parent", $myCIintl.Parent, $myCItrad.Parent
- "{0,-31}{1,-47}{2,-25}" -f "TextInfo", $myCIintl.TextInfo, $myCItrad.TextInfo
- "{0,-31}{1,-47}{2,-25}" -f "ThreeLetterISOLanguageName", $myCIintl.ThreeLetterISOLanguageName, $myCItrad.ThreeLetterISOLanguageName
- "{0,-31}{1,-47}{2,-25}" -f "ThreeLetterWindowsLanguageName",$myCIintl.ThreeLetterWindowsLanguageName, $myCItrad.ThreeLetterWindowsLanguageName
- "{0,-31}{1,-47}{2,-25}" -f "TwoLetterISOLanguageName", $myCIintl.TwoLetterISOLanguageName, $myCItrad.TwoLetterISOLanguageName
- ""
- # Compare two strings using myCIintl
- "Comparing [llegar] and [lugar]"
- " With myCIintl.CompareInfo.Compare: {0}" -f $myCIintl.CompareInfo.Compare("llegar", "lugar")
- " With myCItrad.CompareInfo.Compare: {0}" -f $myCItrad.CompareInfo.Compare("llegar", "lugar")
Show-ChineeseParentCulture.ps1
- <#
- .SYNOPSIS
- This script displays the parent culture of each
- specific culture using the Chinese language.
- .DESCRIPTION
- This script looks at each Chineese culture and displays
- the culture name and the parent.
- .NOTES
- File Name : Show-ChineeseParentCulture.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.cultureinfo.aspx
- .EXAMPLE
- C:\foo> .\Show-ChineeseParentCulture.ps1
- SPECIFIC CULTURE PARENT CULTURE
- 0x0804 zh-CN Chinese (Simplified, PRC) 0x0004 zh-CHS Chinese (Simplified) Legacy
- 0x0C04 zh-HK Chinese (Traditional, Hong Kong S.A.R.) 0x7C04 zh-CHT Chinese (Traditional) Legacy
- 0x1404 zh-MO Chinese (Traditional, Macao S.A.R.) 0x7C04 zh-CHT Chinese (Traditional) Legacy
- 0x1004 zh-SG Chinese (Simplified, Singapore) 0x0004 zh-CHS Chinese (Simplified) Legacy
- 0x0404 zh-TW Chinese (Traditional, Taiwan) 0x7C04 zh-CHT Chinese (Traditional) Legacy
- #>
- # Display a header
- "SPECIFIC CULTURE PARENT CULTURE"
- # Determine the specific cultures that use the Chinese language, and displays the parent culture
- ForEach ($ci in [System.Globalization.CultureInfo]::GetCultures([System.Globalization.CultureTypes]::SpecificCultures)) {
- if ($ci.TwoLetterISOLanguageName -eq "zh")
- {
- $s1 = "0x{0} {1} {2,-40}" -f $ci.LCID.ToString("X4"), $ci.Name, $ci.EnglishName
- $s2 = "0x{0} {1} {2}" -f $ci.Parent.LCID.ToString("X4"), $ci.Parent.Name, $ci.Parent.EnglishName
- "{0}{1}" -f $s1, $s2
- }
- }
Subscribe to:
Posts (Atom)