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

  1. #### 
  2. # Configure-DC1-2 
  3. # Configures DC1 after dcpromo is completed 
  4. # 
  5. # Version    Date         What Changed 
  6. # -------    -----------  ------------------------------------------- 
  7. # 1.0.0      14 Jan 2013  Initial release 
  8. # 1.1.0      24 Jan 2013  Added code to count how long it all took, 
  9. #                         Added checkpoint at the end of this script 
  10. # 1.1.1      25 Jan 2013  Added auto admin logon 
  11. # 1.1.2      5  Feb 2013  Added forced reboot of DC1-1 at script end  
  12. #### 
  13.  
  14. #     Configuration block 
  15. $Conf = { 
  16.  
  17. $StartTime = Get-Date 
  18. Write-Host "Starting at: $StartTime" 
  19.  
  20. #    Set Credentials for use in this configuration block 
  21. $User       = "Reskit\Administrator" 
  22. $Password   = 'Pa$$w0rd' 
  23. $PasswordSS = ConvertTo-SecureString  -String $Password –AsPlainText `
  24.               -Force 
  25. $Dom        = 'Reskit' 
  26. $CredRK     = New-Object  `
  27.     -Typename System.Management.Automation.PSCredential  `
  28.      -Argumentlist $User,$PasswordSS 
  29.  
  30. #    Define registry path for autologon, then set admin logon 
  31. $RegPath  = 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon' 
  32. Set-ItemProperty -Path  $RegPath -Name AutoAdminLogon   ` 
  33.                  -Value 1      -EA 0   
  34. Set-ItemProperty -Path  $RegPath -Name DefaultUserName   ` 
  35.                  -Value $User  -EA 0   
  36. Set-ItemProperty -Path  $RegPath -Name DefaultPassword   ` 
  37.                  -Value $Password -EA 0 
  38. Set-ItemProperty -Path  $RegPath -Name DefaultDomainName  ` 
  39.                  -Value $Dom -EA 0  
  40.  
  41. #    Install key Windows features for labs 
  42. Write-Verbose 'Installing Windows fetures needed for DC1' 
  43. $Features = @('PowerShell-ISE'
  44.               'Hyper-V-PowerShell'
  45.               'Rsat-AD-PowerShell',               
  46.               'Web-Server','Web-Mgmt-Tools'
  47.               'Web-Mgmt-Console'
  48.               'Web-Scripting-Tools'
  49.               'Telnet-Client'
  50. Install-WindowsFeature @Features -IncludeManagementTools -Verbose 
  51.  
  52. #    Install and configure DHCP 
  53. Write-Verbose -Message 'Adding and then configuring DHCP' 
  54. Install-WindowsFeature DHCP -IncludeManagementTools 
  55. Add-DhcpServerV4Scope -Name "ReskitNet0"
  56.                       -StartRange 10.0.0.100 ` 
  57.                       -EndRange 10.0.0.119 ` 
  58.                       -SubnetMask 255.255.255.0 
  59. Set-DhcpServerV4OptionValue -DnsDomain Reskit.Org ` 
  60.                             -DnsServer 10.0.0.10 
  61. Add-DhcpServerInDC -DnsName Dc1.reskit.org 
  62.  
  63. #    Add users to the AD and then add them to some groups 
  64. #    Hash table for common new user paraemters 
  65. Write-Verbose -Message 
  66. $NewUserHT  = @{AccountPassword       = $PasswordSS
  67.                 Enabled               = $true
  68.                 PasswordNeverExpires  = $true
  69.                 ChangePasswordAtLogon = $false 
  70.                 } 
  71.  
  72. #     Create one new user (me!) and add to enterprise and domain admins security groups 
  73. New-ADUser @NewUserHT -SamAccountName tfl ` 
  74.                       -UserPrincipalName 'tfl@reskit.org'
  75.                       -Name "tfl"
  76.                       -DisplayName 'Thomas Lee' 
  77. Add-ADPrincipalGroupMembership `
  78.        -Identity "CN=tfl,CN=Users,DC=reskit,DC=org"
  79.        -MemberOf "CN=Enterprise Admins,CN=Users,DC=reskit,DC=org"
  80.                  "CN=Domain Admins,CN=Users,DC=reskit,DC=org"  
  81.  
  82. #     Say nice things and finish 
  83. $FinishTime = Get-Date 
  84. Write-Verbose "Finished at: $FinishTime" 
  85. Write-Verbose "DC1 Configuration took $(($FinishTime - $StartTime).TotalSeconds.ToString('n2')) seconds" 
  86.  
  87. } # End Conf configuration script block 
  88.  
  89. #    Start of script proper 
  90.  
  91. #    Set Credentials 
  92. $Username   = "Reskit\administrator" 
  93. $Password   = 'Pa$$w0rd' 
  94. $PasswordSS = ConvertTo-SecureString  -String $Password -AsPlainText
  95.               -Force 
  96. $CredRK     = New-Object -Typename System.Management.Automation.PSCredential `
  97.         -Argumentlist $Username,$PasswordSS 
  98.  
  99. #    Following code used to test the credentials. Remove the comments on next two lines the first time you  
  100. #    run this script 
  101. Invoke-Command -ComputerName DC1 -ScriptBlock {hostname} -Credential $Credrk -verbose 
  102. Pause 
  103.  
  104. Invoke-Command -ComputerName DC1 -Scriptblock $conf -Credential $CredRK `
  105.                -Verbose 
  106.  
  107. #     OK - script block has completed - reboot the system and wait till it comes up 
  108. Restart-Computer -ComputerName DC1  -Wait -For PowerShell –Force
  109.                  -Credential $CredRK 
  110.   
  111. #    Finally, run a post-DCPromo snapshot 
  112. Checkpoint-VM -VM $(Get-VM DC1) `
  113. -SnapshotName "DC1 - post configuration by ConfigureDC1-2.ps1"  

Wednesday, 23 January 2013

Configure-DC1-1.ps1

  1. # Configure-DC1-1.ps1
  2. # Converts Server to DC
  3. # Version 1.0.0 - 14 Jan 2013
  4. # See http://tfl09.blogspot.com for more details
  5. # Config script block
  6. $conf = {
  7. # Install the AD - the reboot is or should be automagic
  8. Install-Windowsfeature AD-Domain-Services -IncludeManagementTools
  9. # Now install the AD to DC1
  10. $PasswordSS = ConvertTo-SecureString -string 'Pa$$w0rd' -AsPlainText -Force
  11. Install-ADDSForest -DomainName Reskit.Org -SafeModeAdministratorPassword $PasswordSS -force -InstallDNS -DomainMode Win2012 -ForestMode Win2012
  12. }
  13. # Here is start of script
  14. $Username = "DC1\administrator"
  15. $PasswordSS = ConvertTo-SecureString 'Pa$$w0rd' -AsPlainText -Force
  16. $Creddc1 = New-Object system.management.Automation.PSCredential $username,$PasswordSS
  17. # First, run a simple script block to check that the server is actually running and is the one we think it is
  18. Invoke-Command -ComputerName DC1 -ScriptBlock { ipconfig;hostname} -Credential $Creddc1 -verbose
  19. Pause
  20. # Now add create our forest/domain/dc
  21. Invoke-Command -ComputerName DC1 -Scriptblock $conf -Credential $Creddc1 -verbose
Technorati Tags: ,,,

Monday, 21 January 2013

Create-VM.PS1

  1. # Create-VM.ps1 
  2. # Script that creates VMs 
  3. # Version 1.0.0 - 20 Jan 2013 
  4. # See http://tfl09.blogspot.co.uk/2013/01/building-hyper-v-test-lab-on-windows-8.html 
  5.  
  6. # First define the Create-VM Function 
  7.  
  8. Function Create-VM { 
  9. #=================================================== 
  10. # Create a New VM 
  11. #=================================================== 
  12.  
  13. # Parameters are Name, Virtual Machine Path, path to reference Vhdx,
  14. # network switch to use, VM Memory, Unattend file, IP address and DNS
  15. # Server to set. Default values are specified in the Param block,
  16. # but these are normally overridden in the call to Create0VM 
  17.  
  18.  
  19. [Cmdletbinding()] 
  20. Param (  
  21.  $Name         = "Server"
  22.  $VmPath       = "C:\v3"
  23.  $ReferenceVHD = "C:\v3\Ref2012.vhdx"
  24.  $Network      = "Internal"
  25.  $VMMemory     = 512mb, 
  26.  $UnattendXML  = "C:\v3\unattend.xml"
  27.  $IPAddr       = '10.0.0.250/24'
  28.  $DnsSvr       = '10.0.0.10' 
  29.  
  30. $Starttime = Get-Date 
  31. Write-Verbose "Starting Create-VM at $Starttime" 
  32. Write-verbose "Creating VM: [$name]" 
  33. Write-verbose "Path to VM : [$VMpath]" 
  34.  
  35. #    Set path to differencing disk location 
  36. $path = "$vmpath\$name.vhdx" 
  37. Write-Verbose "Creating Disk at [$path]" 
  38.  
  39. #    Add a new differencing VHDX, Based on parent parent 
  40. $vmDisk01 = New-VHD –Path $path -Differencing –ParentPath $ReferenceVHD -ErrorAction Stop 
  41. Write-Verbose "Added VM Disk [$VMdisk01], pointing to [ReferenceVHD]" 
  42.  
  43. #    Create a New VM 
  44. $VM = New-VM –Name $name –MemoryStartupBytes $VMMemory –VHDPath $VMDisk01.path -SwitchName $Network -Path $vmPath 
  45. Write-Verbose "VM [$name] created" 
  46.  
  47. # Mount the Disk into the VM 
  48. Mount-DiskImage -ImagePath $path 
  49. $VHDDisk = Get-DiskImage -ImagePath $path | Get-Disk 
  50. $VHDPart = Get-Partition -DiskNumber $VHDDisk.Number 
  51. $VHDVolumeName = [string]$VHDPart.DriveLetter 
  52. $VHDVolume = [string]$VHDPart.DriveLetter + ":" 
  53. Write-verbose "Volume [$Volumename] created in VM [$name]" 
  54.  
  55.  
  56. #    Get Unattended.XML file 
  57. Write-Verbose "Using Unattended XML file [$unattendXML]" 
  58.  
  59. #    Open XML file 
  60. $Xml = [xml](get-content $UnattendXML
  61.  
  62. #    Change ComputerName 
  63. Write-Verbose "Setting VM ComputerName to: [$name]" 
  64. $Xml.unattend.settings.component | Where-Object { $_.Name -eq "Microsoft-Windows-Shell-Setup" } | 
  65.  ForEach-Object
  66.    if($_.ComputerName) { 
  67.      $_.ComputerName = $name 
  68.    } 
  69.  
  70. #    Change IP address 
  71. Write-Verbose "Setting VM ComputerName to: [$name]" 
  72. $Xml.unattend.settings.component | Where-Object { $_.Name -eq "Microsoft-Windows-TCPIP" } | 
  73.   ForEach-Object
  74.  
  75.     if($_.Interfaces) { 
  76.       $ht='#text' 
  77.       $_.interfaces.interface.unicastIPaddresses.ipaddress.$ht = $IPAddr 
  78.   } 
  79.  
  80. #    Change DNS Server address 
  81. #    Use obscure way to create the #TEXT node 
  82. Write-Verbose "Setting VM DNS address to: [$DNSSvr]" 
  83. $Xml.Unattend.Settings.Component | Where-Object { $_.Name -eq "Microsoft-Windows-DNS-Client" } | 
  84.   ForEach-Object
  85.       if($_.Interfaces) { 
  86.       $ht='#text' 
  87.       $_.Interfaces.Interface.DNSServerSearchOrder.Ipaddress.$ht = $DNSSvr 
  88.   } 
  89.  
  90. #    Save XML File on Mounted VHDX differencing disk 
  91. $xml.Save("$VHDVolume\Unattend.XML"
  92. Write-Verbose "Unattended XML file saved to vhd [$vhdvolume\unattend.xml]" 
  93.  
  94. #    Dismount VHDX  
  95. Write-Verbose "Dismounting disk image: [$Path]" 
  96. Dismount-DiskImage -ImagePath $path 
  97.  
  98. #    Update additional VM settings 
  99. Write-Verbose 'Setting additional VM settings' 
  100. Set-VM -Name $name -DynamicMemory 
  101. Set-VM -Name $name -MemoryMinimumBytes $VMMemory 
  102. Set-VM -Name $name -AutomaticStartAction Nothing 
  103. Set-Vm -Name $name -AutomaticStopAction ShutDown 
  104.  
  105. #    Show what has been created! 
  106. "VM Created:" 
  107. Get-VM -Name $name | fl * 
  108.  
  109. #    Start VM 
  110. Write-verbose "VM [$Name] being started" 
  111. Start-VM -Name $name 
  112.  
  113. #    Now work out and write how long it took to create the VM 
  114. $Finishtime = Get-Date 
  115. Write-Verbose ("Creating VB ($name) took {0} seconds" -f ($FinishTime - $Starttime).totalseconds) 
  116. # End of Create-VM function 
  117.  
  118.  
  119. ####################################################################################################### 
  120. #       CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS     # 
  121. #       CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS     # 
  122. #       CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS     # 
  123.  
  124. # Location of Server 2012 DVD Iso Image 
  125. $iso   = 'c:\Builds\9200.16384.120725-1247_x64frev_Server_Datacenter_VL_HRM_SSS_X64FREV_EN-US_DVD.iso' 
  126. # Where we put the reference VHDX 
  127. $ref   = 'c:\v3\Ref2012.vhdx' 
  128. # Path were VMs, VHDXs and unattend.txt files live 
  129. $path  = 'c:\V3' 
  130. # Location of Unattend.xml - first for workstation systems, second for domain joined systems  
  131. $una   = 'c:\V3\UnAttend.xml' 
  132. $unadj = 'c:\V3\UnAttend.dj.xml' 
  133.  
  134. #       CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS     # 
  135. #       CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS     # 
  136. #       CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS     # 
  137. ####################################################################################################### 
  138.  
  139. #   Now run the script to create the VMs as appropriate. 
  140. $Start = Get-Date 
  141. "Create-VM --- Started at: $Start" 
  142.  
  143. #################################################################### 
  144. # Comment out VMs you do NOT want to create then run the entire script 
  145. # To comment out a VM creation, just add a '#" at the start of the line.  
  146. #        Removing the comment line means you want to create that VM.  
  147. #        BE creful!  If you make a mistake, stop the script. Kill any VMs created, then remove the 
  148. #        storage for the VMs.  
  149. ####################################################################################################### 
  150.  
  151. #    Create the DC - NON-domained joined 
  152. # 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 
  153.  
  154. #    Remaining VMs use the domain-join version of unattend.xml 
  155. # 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 
  156. # 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 
  157. # 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 
  158. # 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 
  159.  
  160. #    DHCP 1,2 for advanced networking class 
  161. # 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 
  162. # 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 
  163.  
  164. #    Create a second DC for reskit.org for advanced class 
  165. # 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 
  166.  
  167.  
  168. #  script is all done - just say nice things and quit. 
  169. $Finish = Get-Date 
  170. "Create-VM --- Finished at: $Finish" 
  171. "Elapsed Time :  $(($Finish-$Start).totalseconds) seconds" 
  172.   
Technorati Tags: ,,,

Wednesday, 3 October 2012

Set-PowerShellAsShell

  1. <# 
  2. .Synopsis 
  3.    Creates a function to set PowerShell as GUI in Server 2012 
  4. .DESCRIPTION 
  5.    The function in this script sets PowerShell as the  
  6.    default shell in Server 2012. When the server is rebooted, 
  7.    it runs PowerShell.exe by default. When PowerShell starts, it 
  8.    displays the $PSVersionTable variable. 
  9. .NOTES 
  10.     File Name   : Set-PowerShellAsGui.ps1 
  11.     Author      : Thomas Lee - tfl@psp.co.uk 
  12.     Requires    : Server 2012 
  13. .LINK 
  14.     This script posted to: 
  15.         http://www.pshscripts.blogspot.com   
  16. .EXAMPLE 
  17.     Left as an exercise to the reader 
  18. #> 
  19.  
  20.  
  21. Function Set-PowerShellAsShell { 
  22.  
  23. [CmdletBinding()] 
  24. Param ( 
  25. [switch] $Reboot = $false 
  26.  
  27. # Create Registry Path variable 
  28. $RegPath =  "Microsoft.PowerShell.Core\Registry::"  
  29. $RegPath += "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\" 
  30. $RegPath += "Windows NT\CurrentVersion\winlogon" 
  31.  
  32. # Create splatted parameter hash table 
  33. $parm  =  @{Path    = $regpath}           # key 
  34. $parm +=  @{Name    = 'Shell'}            # value name 
  35. $parm +=  @{Value   = 'PowerShell.exe –NoExit 
               -Command "
    $psversiontable"'}   # value’s value 
  36.  
  37. # Set Registry value entry 
  38. Set-ItemProperty @parm  
  39.  
  40. # And restart to see PowerShell 
  41. if ($Reboot) {Restart-Computer -confirm} 

Tuesday, 7 August 2012

Show-Formatting1.ps1

  1. <#
  2. .SYNOPSIS
  3. MSDN Sample Recoded in PowerShell demonstrating formatting
  4. .DESCRIPTION
  5. This sample recodes an MSDN Sample into PowerShell that
  6. shows some of the options of formatting using ToString() and
  7. various .NET formatting strings
  8. .NOTES
  9. File Name : Show-Formatting1.ps1
  10. Author : Thomas Lee - tfl@psp.co.uk
  11. Requires : PowerShell Version 2.0
  12. .LINK
  13. This script posted to:
  14. http://www.pshscripts.blogspot.com
  15. MSDN sample posted to:
  16. http://msdn.microsoft.com/en-us/library/0c899ak8.aspx
  17. .EXAMPLE
  18. Psh> .\Show-Formatting1.ps1\
  19. 00123
  20. 1.20
  21. 01.20
  22. 01,20
  23. 0.6
  24. 1,234,567,890
  25. 1.234.567.890
  26. 1,234,567,890.1
  27. 1,234.57
  28. #>
  29. ## Start script
  30. [double] $value = 123;
  31. $value.ToString("00000")
  32. # Displays 00123
  33. $value = 1.2;
  34. $value.ToString("0.00", [System.Globalization.CultureInfo]::InvariantCulture)
  35. # Displays 1.20
  36. $value.ToString("00.00",[System.Globalization.CultureInfo]::InvariantCulture)
  37. # Displays 01.20
  38. $value.ToString("00.00",
  39. [System.Globalization.CultureInfo]::CreateSpecificCulture("da-DK"))
  40. # Displays 01,20
  41. $value = .56
  42. $value.ToString("0.0", [System.Globalization.CultureInfo]::InvariantCulture)
  43. # Displays 0.6
  44. $value = 1234567890
  45. $value.ToString("0,0", [System.Globalization.CultureInfo]::InvariantCulture)
  46. # Displays 1,234,567,890
  47. $value.ToString("0,0",
  48. [System.Globalization.CultureInfo]::CreateSpecificCulture("el-GR"))
  49. # Displays 1.234.567.890
  50. $value = 1234567890.123456;
  51. $value.ToString("0,0.0", [System.Globalization.CultureInfo]::InvariantCulture)
  52. # Displays 1,234,567,890.1
  53. $value = 1234.567890;
  54. $value.ToString("0,0.00", [System.Globalization.CultureInfo]::InvariantCulture)
  55. # Displays 1,234.57

Monday, 30 April 2012

Show-DnsConfiguration.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script shows the DNS Configuration  of NICs  
  4.     in your system 
  5. .DESCRIPTION 
  6.     This script is a re-write of an MSDN Sample  
  7.     using PowerShell./ The script gets all network 
  8.     active network interfaces then prints out that 
  9.     interfaces' DNS Properties. 
  10. .NOTES 
  11.     File Name  : Show-DnsConfiguration.ps1 
  12.     Author     : Thomas Lee - tfl@psp.co.uk 
  13.     Requires   : PowerShell Version 2.0 
  14. .LINK 
  15.     This script posted to: 
  16.         http://www.pshscripts.blogspot.com 
  17.     MSDN sample posted to: 
  18.          http://msdn.microsoft.com/en-us/library/system.net.networkinformation.networkinterface.getallnetworkinterfaces.aspx    
  19. .EXAMPLE 
  20.     Psh[C:\foo]> .\Show-DnsConfiguration.ps1 
  21.     Broadcom NetXtreme 57xx Gigabit Controller 
  22.       DNS suffix .............................. : cookham.net 
  23.       DNS enabled ............................. : False 
  24.       Dynamically configured DNS .............. : True 
  25.  
  26.     ... more interfaces snipped for brevity!     
  27. #> 
  28.  
  29. # Get the adapters than iterate over the collection and display DNS configuration 
  30. $adapters = [System.Net.NetworkInformation.NetworkInterface]::GetAllNetworkInterfaces() 
  31. ForEach ($adapter in $adapters)   { 
  32.   $properties = $adapter.GetIPProperties() 
  33.   $adapter.Description 
  34.   "  DNS suffix .............................. : {0}" -f $properties.DnsSuffix 
  35.   "  DNS enabled ............................. : {0}" -f $properties.IsDnsEnabled 
  36.   "  Dynamically configured DNS .............. : {0}" -f $properties.IsDynamicDnsEnabled 

Sunday, 29 April 2012

Show-NetworkInterfaces1.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script displays the NICs in a system and their physical
  4.     address. 
  5. .DESCRIPTION 
  6.     This script is a MSDN Sample recoded in PowerShell. The script
  7.     first gets all the interfaces on the system, then loops through
  8.     them displaying more information about them to the console.
  9.     Note the use of Console.Write in the loop at the end. Not quite
  10.     sure a better PowerShell equivalent other than creating a
  11.     string with all the bytes, then displaying that string. 
  12. .NOTES 
  13.     File Name  : Show-NetworkInterfaces1.ps1 
  14.     Author     : Thomas Lee - tfl@psp.co.uk 
  15.     Requires   : PowerShell Version 2.0 
  16. .LINK 
  17.     This script posted to: 
  18.         http://www.pshscripts.blogspot.com 
  19.     MSDN sample posted to: 
  20.         http://msdn.microsoft.com/en-us/library/system.net.networkinformation.physicaladdress.aspx    
  21. .EXAMPLE 
  22.     PSH[c:\foo]> .\Show-Networkinterfaces1.ps1 
  23. Interface information for Cookham8.cookham.net      
  24.   Number of interfaces .................... : 2 
  25. Broadcom NetXtreme 57xx Gigabit Controller 
  26. ========================================== 
  27.   Interface type .......................... : Ethernet 
  28.   Physical address ........................ : 00-1E-4F-95-5C-C4  
  29.  
  30. Microsoft ISATAP Adapter 
  31. ======================== 
  32.   Interface type .......................... : Tunnel 
  33.   Physical address ........................ : 00-00-00-00-00-00-00-E0  
  34.      
  35. #> 
  36.      
  37. # Get computer IP global properties 
  38. $ComputerProperties = [System.Net.NetworkInformation.IpGlobalProperties]::GetIPGlobalProperties() 
  39.  
  40. # Get the nics in this system 
  41. $nics = [System.Net.NetworkInformation.NetworkInterface]::GetAllNetworkInterfaces() 
  42.  
  43. # Show information header 
  44. "Interface information for {0}.{1}     " -f $ComputerProperties.HostName, $ComputerProperties.DomainName 
  45.   
  46. # Iterate through the NIcs ans outout per nic info 
  47. # First, check if interfaces are found 
  48.  if (!$nics -OR $nics.Length -LT 1) 
  49.     { 
  50.         "  No network interfaces found." 
  51.         return 
  52.     } 
  53.  
  54. # Here print out number of interfaces and interface details 
  55. [System.Console]::WriteLine("  Number of interfaces .................... : {0}" -f $nics.Length) 
  56. Foreach ($adapter in $nics
  57.     { 
  58.         $properties = $adapter.GetIPProperties();    
  59.         " ";"" 
  60.         "{0}" -F $adapter.Description 
  61.         "=" * $adapter.Description.Length 
  62.         "  Interface type .......................... : {0}" -F $adapter.NetworkInterfaceType 
  63.         [System.Console]::Write("  Physical address ........................ : "
  64.         $address = $adapter.GetPhysicalAddress(); 
  65.         $bytes = $address.GetAddressBytes() 
  66.         for($i = 0; $i -lt $bytes.Length; $i++) 
  67.         { 
  68.             # Display the physical address in hexadecimal. 
  69.             [system.Console]::Write("{0}" -f $bytes[$i].ToString("X2")) 
  70.             # Insert a hyphen after each byte, unless we are at the end of the  
  71.             # address. 
  72.             if ($i -NE $bytes.Length -1) 
  73.             { 
  74.                  [System.Console]::Write("-"
  75.             } 
  76.         } 
  77.         [System.Console]::WriteLine() 
  78.     } 

Tuesday, 7 February 2012

Test-FileOpen

  1. <# 
  2. .SYNOPSIS 
  3.     This script defines a function that tests to 
  4.     see if a file is open. 
  5. .DESCRIPTION 
  6.     This script used the System.Io.FileStream class  
  7.     and the FileInfo class to try to open a file 
  8.     stream for write. If it fails, we return $false
  9.     else we close the file and return $True 
  10. .NOTES 
  11.     File Name  : Test-FileOpen.ps1 
  12.     Author     : Thomas Lee - tfl@psp.co.uk 
  13.     Requires   : PowerShell Version 2.0 
  14. .LINK 
  15.     This script posted to: 
  16.         http://www.pshscripts.blogspot.com 
  17. .EXAMPLE 
  18.     Psh[Cookham8:C:\foo]> $file = New-Object -TypeName System.IO.FileInfo C:\foo\doc1.docx 
  19.     Psh[Cookham8:C:\foo]>Test-FileOpen $file 
  20.     True 
  21. #> 
  22.  
  23. Function Test-FileOpen { 
  24.  
  25. Param ( 
  26. $fileName = $(Throw '***** No File specified')  
  27.  
  28. $ErrorActionPreference = "SilentlyContinue" 
  29. [System.IO.FileStream] $fs = $file.OpenWrite();  
  30. if (!$?) {$true
  31. else {$fs.Dispose();$false
  32.  
  33. # Test the function 
  34. $file = New-Object -TypeName System.IO.FileInfo C:\foo\doc1.docx 
  35. Test-FileOpen $file 

Sunday, 22 January 2012

New-SpanishCulture.ps1


  1. <# 
  2. .SYNOPSIS 
  3.     This script creates a Spanish cultureinfo object with a traditional 
  4.      sort and another with an international sort. The script then compares them. 
  5. .DESCRIPTION 
  6.     This script re-implements an MSDN sample.  
  7. .NOTES 
  8.     File Name  : New-SpanishCulture.ps1 
  9.     Author     : Thomas Lee - tfl@psp.co.uk 
  10.     Requires   : PowerShell Version 2.0 
  11. .LINK 
  12.     This script posted to: 
  13.         http://www.pshscripts.blogspot.com 
  14.     MSDN sample posted to: 
  15.          http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.aspx    
  16. .EXAMPLE 
  17.     C:\foo> .\New-SpanishCulture.ps1 
  18.         PROPERTY                       INTERNATIONAL                                  TRADITIONAL               
  19.     CompareInfo                    CompareInfo - es-ES                            CompareInfo - es-ES_tradnl 
  20.     DisplayName                    Spanish (Spain)                                Spanish (Spain)           
  21.     EnglishName                    Spanish (Spain, International Sort)            Spanish (Spain, Traditional Sort) 
  22.     IsNeutralCulture               False                                          False                     
  23.     IsReadOnly                                                                    False                     
  24.     LCID                           3082                                           1034                      
  25.     Name                           es-ES                                          es-ES                     
  26.     NativeName                     Español (España, alfabetización internacional) Español (España, alfabetización tradicional) 
  27.     Parent                         es                                             es                        
  28.     TextInfo                       TextInfo - es-ES                               TextInfo - es-ES_tradnl   
  29.     ThreeLetterISOLanguageName     spa                                            spa                       
  30.     ThreeLetterWindowsLanguageName ESN                                            ESP                       
  31.     TwoLetterISOLanguageName       es                                             es                        
  32.  
  33.     Comparing [llegar] and [lugar] 
  34.        With myCIintl.CompareInfo.Compare: -1 
  35.        With myCItrad.CompareInfo.Compare: 1 
  36. #> 
  37.  
  38. # Create and initialize the CultureInfo which uses the international sort 
  39. $myCIintl = New-Object System.Globalization.CultureInfo "es-ES", $false 
  40.  
  41. # Create and initialize the CultureInfo which uses the traditional sort 
  42. $myCItrad = New-Object System.Globalization.CultureINfo 0x040A, $false 
  43.  
  44. # Display the properties of each culture. 
  45. "{0,-31}{1,-47}{2,-25}" -f "PROPERTY", "INTERNATIONAL", "TRADITIONAL" 
  46. "{0,-31}{1,-47}{2,-25}" -f "CompareInfo", $myCIintl.CompareInfo, $myCItrad.CompareInfo 
  47. "{0,-31}{1,-47}{2,-25}" -f "DisplayName", $myCIintl.DisplayName, $myCItrad.DisplayName 
  48. "{0,-31}{1,-47}{2,-25}" -f "EnglishName", $myCIintl.EnglishName, $myCItrad.EnglishName 
  49. "{0,-31}{1,-47}{2,-25}" -f "IsNeutralCulture", $myCIintl.IsNeutralCulture, $myCItrad.IsNeutralCulture 
  50. "{0,-31}{1,-47}{2,-25}" -f "IsReadOnly", $myCIintl.$IsReadOnly, $myCItrad.IsReadOnly 
  51. "{0,-31}{1,-47}{2,-25}" -f "LCID", $myCIintl.LCID, $myCItrad.LCID 
  52. "{0,-31}{1,-47}{2,-25}" -f "Name", $myCIintl.Name, $myCItrad.Name 
  53. "{0,-31}{1,-47}{2,-25}" -f "NativeName", $myCIintl.NativeName, $myCItrad.NativeName 
  54. "{0,-31}{1,-47}{2,-25}" -f "Parent", $myCIintl.Parent, $myCItrad.Parent 
  55. "{0,-31}{1,-47}{2,-25}" -f "TextInfo", $myCIintl.TextInfo, $myCItrad.TextInfo 
  56. "{0,-31}{1,-47}{2,-25}" -f "ThreeLetterISOLanguageName", $myCIintl.ThreeLetterISOLanguageName, $myCItrad.ThreeLetterISOLanguageName 
  57. "{0,-31}{1,-47}{2,-25}" -f "ThreeLetterWindowsLanguageName",$myCIintl.ThreeLetterWindowsLanguageName, $myCItrad.ThreeLetterWindowsLanguageName 
  58. "{0,-31}{1,-47}{2,-25}" -f "TwoLetterISOLanguageName", $myCIintl.TwoLetterISOLanguageName, $myCItrad.TwoLetterISOLanguageName 
  59. "" 
  60.  
  61. # Compare two strings using myCIintl 
  62. "Comparing [llegar] and [lugar]" 
  63. "   With myCIintl.CompareInfo.Compare: {0}" -f $myCIintl.CompareInfo.Compare("llegar", "lugar"
  64. "   With myCItrad.CompareInfo.Compare: {0}" -f $myCItrad.CompareInfo.Compare("llegar", "lugar"

Show-ChineeseParentCulture.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script displays the parent culture of each  
  4.     specific culture using the Chinese language. 
  5. .DESCRIPTION 
  6.     This script looks at each Chineese culture and displays 
  7.     the culture name and the parent.  
  8. .NOTES 
  9.     File Name  : Show-ChineeseParentCulture.ps1 
  10.     Author     : Thomas Lee - tfl@psp.co.uk 
  11.     Requires   : PowerShell Version 2.0 
  12. .LINK 
  13.     This script posted to: 
  14.         http://www.pshscripts.blogspot.com 
  15.     MSDN sample posted to: 
  16.          http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.aspx    
  17. .EXAMPLE 
  18.     C:\foo> .\Show-ChineeseParentCulture.ps1 
  19.     SPECIFIC CULTURE                                     PARENT CULTURE 
  20.     0x0804 zh-CN Chinese (Simplified, PRC)               0x0004 zh-CHS Chinese (Simplified) Legacy 
  21.     0x0C04 zh-HK Chinese (Traditional, Hong Kong S.A.R.) 0x7C04 zh-CHT Chinese (Traditional) Legacy 
  22.     0x1404 zh-MO Chinese (Traditional, Macao S.A.R.)     0x7C04 zh-CHT Chinese (Traditional) Legacy 
  23.     0x1004 zh-SG Chinese (Simplified, Singapore)         0x0004 zh-CHS Chinese (Simplified) Legacy 
  24.     0x0404 zh-TW Chinese (Traditional, Taiwan)           0x7C04 zh-CHT Chinese (Traditional) Legacy     
  25. #> 
  26.  
  27. # Display a header 
  28. "SPECIFIC CULTURE                                     PARENT CULTURE" 
  29.  
  30. # Determine the specific cultures that use the Chinese language, and displays the parent culture 
  31.  
  32. ForEach ($ci in [System.Globalization.CultureInfo]::GetCultures([System.Globalization.CultureTypes]::SpecificCultures))  { 
  33.   if ($ci.TwoLetterISOLanguageName -eq "zh"
  34.    { 
  35.     $s1 = "0x{0} {1} {2,-40}" -f $ci.LCID.ToString("X4"), $ci.Name, $ci.EnglishName 
  36.     $s2 = "0x{0} {1} {2}" -f $ci.Parent.LCID.ToString("X4"), $ci.Parent.Name, $ci.Parent.EnglishName 
  37.     "{0}{1}" -f $s1, $s2 
  38.   }