Sunday, 28 July 2013

Show-NumberGroupSizes.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script demonstrates the use of the NumberGroupSizes 
  4. .DESCRIPTION 
  5.     This script is a re-write of an MSDN sample, using PowerShell 
  6. .NOTES 
  7.     File Name  : Show-NumberGroupSizes.ps1 
  8.     Author     : Thomas Lee - tfl@psp.co.uk 
  9.     Requires   : PowerShell Version 3.0 
  10. .LINK 
  11.     This script posted to: 
  12.         http://www.pshscripts.blogspot.com 
  13.     MSDN sample posted to: 
  14.          http://msdn.microsoft.com/en-us/library/system.globalization.numberformatinfo.numbergroupsizes%28v=vs.100%29.aspx 
  15. .EXAMPLE 
  16.     PSH> .\Show-NumberGroupSizes.ps1 
  17.     123,456,789,012,345.00 
  18.     12,3456,7890,123,45.00 
  19.     1234567890,123,45.00 
  20. #> 
  21.  
  22. # Get Number Format 
  23. $nf  = New-Object System.Globalization.CultureInfo  "en-US", $False  
  24. $nfi = $nf.NumberFormat 
  25.  
  26. [Int64] $myInt = 123456789012345 
  27. $myInt.ToString( "N", $nfi ) 
  28.  
  29. # Display the same value with different groupings 
  30. [int[]] $MySizes1 = 2,3,4 
  31. [int[]] $MySizes2 = 2,3,0 
  32.  
  33. $nfi.NumberGroupSizes = $mySizes1 
  34. $myInt.ToString( "N",$nfi ) 
  35. $nfi.NumberGroupSizes = $mySizes2 
  36. $myInt.ToString( "N", $nfi )  

Friday, 28 June 2013

Show-CurrencyFormatting.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script re-implements an MSDN Sample showing the  
  4.     use of the NumberFormat class to nicely format things 
  5.     in this case, currency. 
  6. .DESCRIPTION 
  7.     This script iterates through the Windows cultures and 
  8.     displays those whose 2-letter ISO code is 'en' and  
  9.     displays how Windows formats currency in that culture.  
  10. .NOTES 
  11.     File Name  : Show-CurrencyFormatting.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.globalization.numberformatinfo.aspx 
  19. .EXAMPLE 
  20.     Psh > .\Show-CurrencyFormatting.ps1 
  21.     The currency symbol for 'English (United States)' is '$' 
  22.     The currency symbol for 'English (United Kingdom)' is '£' 
  23.     The currency symbol for 'English (Australia)' is '$' 
  24.     The currency symbol for 'English (Canada)' is '$' 
  25.     The currency symbol for 'English (New Zealand)' is '$' 
  26.     The currency symbol for 'English (Ireland)' is '€' 
  27.     The currency symbol for 'English (South Africa)' is 'R' 
  28.     The currency symbol for 'English (Jamaica)' is 'J$' 
  29.     The currency symbol for 'English (Caribbean)' is '$' 
  30.     The currency symbol for 'English (Belize)' is 'BZ$' 
  31.     The currency symbol for 'English (Trinidad and Tobago)' is 'TT$' 
  32.     The currency symbol for 'English (Zimbabwe)' is 'Z$' 
  33.     The currency symbol for 'English (Republic of the Philippines)' is 'Php' 
  34.     The currency symbol for 'English (Singapore)' is '$' 
  35.     The currency symbol for 'English (Malaysia)' is 'RM' 
  36.     The currency symbol for 'English (India)' is 'Rs.' 
  37.  
  38. #> 
  39.  
  40. #  Loop through all the specific cultures known to the CLR. 
  41. foreach ($ci in [System.Globalization.CultureInfo]::GetCultures([System.Globalization.CultureTypes]::SpecificCultures))  
  42.    { 
  43.      # Only show the currency symbols for cultures that speak English. 
  44.      if ($ci.TwoLetterISOLanguageName -eq "en") { 
  45.      # Display the culture name and currency symbol. 
  46.         $nfi = $ci.NumberFormat 
  47.         "The currency symbol for '{0}' is '{1}'" -f $ci.DisplayName, $nfi.CurrencySymbol 
  48.      } 
  49.    } 

Thursday, 20 June 2013

Show-CurrencyGroupSize.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script reimplements a code sample from MSDN in PowerShell. 
  4.     This sample formats and display currency using standard and 
  5.     different currency groupings.  
  6. .DESCRIPTION 
  7.     This script displays a currency using standard, then two 
  8.     custom CurrencyGroupSizes. 
  9. .NOTES 
  10.     File Name  : Show-CurrencyGroupSize.ps1 
  11.     Author     : Thomas Lee - tfl@psp.co.uk 
  12.     Requires   : PowerShell Version 2.0 
  13. .LINK 
  14.     This script posted to: 
  15.         http://www.pshscripts.blogspot.com 
  16.     MSDN sample posted to: 
  17.         http://msdn.microsoft.com/en-us/library/system.globalization.numberformatinfo.currencygroupsizes.aspx 
  18. .EXAMPLE 
  19.     PSH:> .\Show-CurrencyGroupSize.ps1 
  20.     Default numeric format string "C" 
  21.     $123,456,789,012,345.00 
  22.  
  23.     Display with array = 2,3,4 
  24.     $12,3456,7890,123,45.00 
  25.  
  26.     Display with array = 2,3,0 
  27.     $1234567890,123,45.00 
  28. #> 
  29.  
  30. #     Get a NumberFormatInfo associated with the en-US culture. 
  31. $fi = new-object System.Globalization.CultureInfo "en-US", false 
  32. $nfi = $fi.NumberFormat 
  33.  
  34. #     Display a value with the default separator (".") 
  35. "Default numeric format string `"C`"" 
  36. [Int64] $myInt = 123456789012345 
  37. $myInt.ToString( "C", $nfi ) 
  38.  
  39. #    Display the same value with different groupings. 
  40. [int[]] $mySizes1 = (2,3,4) 
  41. $mySizes = 2,3,0 
  42.  
  43. "";"Display with array = 2,3,4" 
  44. $nfi.CurrencyGroupSizes = $mySizes1 
  45. $myInt.ToString( "C", $nfi ) 
  46.  
  47. "";"Display with array = 2,3,0" 
  48. $nfi.CurrencyGroupSizes = $mySizes2 
  49. $myInt.ToString( "C", $nfi ) 

Thursday, 16 May 2013

Show-CurrencyDecimalDigits.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script demonstrates the use of the CurrencyDecimalDigits 
  4. .DESCRIPTION 
  5.     This script is a re-write of an MSDN sample, using PowerShell 
  6. .NOTES 
  7.     File Name  : Show-CurrencyDecimalDigits.ps1 
  8.     Author     : Thomas Lee - tfl@psp.co.uk 
  9.     Requires   : PowerShell Version 3.0 
  10. .LINK 
  11.     This script posted to: 
  12.         http://www.pshscripts.blogspot.com 
  13.     MSDN sample posted to: 
  14.          http://msdn.microsoft.com/en-us/library/system.globalization.numberformatinfo.currencydecimaldigits%28v=vs.100%29.aspx 
  15. .EXAMPLE 
  16.     PSH> .\Show-CurrencyDecimalDigits.ps1 
  17.     ($1,234.00) 
  18.     ($1,234.0000) 
  19. #> 
  20.  
  21. # Get Number Format 
  22. $nf  = New-Object System.Globalization.CultureInfo  "en-US", $False  
  23. $nfi = $nf.NumberFormat 
  24.  
  25. # Display a negative value with the default number of decimal digits (2). 
  26. [Int64] $myInt = -1234 
  27. $myInt.ToString( "C", $nfi ) 
  28.  
  29. # Displays the same value with four decimal digits. 
  30. $nfi.CurrencyDecimalDigits = 4 
  31. $myInt.ToString( "C", $nfi ) 

Wednesday, 17 April 2013

Show-NumberDecimalSeparator.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script demonstrates the use of the NumberDecimalSeparator
  4. .DESCRIPTION 
  5.     This script is a re-write of an MSDN sample, using PowerShell 
  6. .NOTES 
  7.     File Name  : Show-NumberDecimaleparatort.ps1 
  8.     Author     : Thomas Lee - tfl@psp.co.uk 
  9.     Requires   : PowerShell Version 3.0 
  10. .LINK 
  11.     This script posted to: 
  12.         http://www.pshscripts.blogspot.com 
  13.     MSDN sample posted to: 
  14.          http://msdn.microsoft.com/en-us/library/b74zyt45%28v=vs.100%29.aspx 
  15. .EXAMPLE 
  16.     PSH> .\Show-NumberDecimalSeparator.ps1 
  17.     123,456,789.00 
  18.     123,456,789 00 
  19. #> 
  20.  
  21. # Get Number Format 
  22. $nf  = New-Object System.Globalization.CultureInfo  "en-US", $False  
  23. $nfi = $nf.NumberFormat 
  24.  
  25. [Int64] $myInt = 123456789 
  26. $myInt.ToString( "N", $nfi ) 
  27.  
  28. $nfi.NumberDecimalSeparator = " " 
  29. $myInt.ToString( "N", $nfi ) 

Wednesday, 13 March 2013

Show-CurrencyGroupSeparator.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script demonstrates the use of the CurrencyGroupSeparator 
  4. .DESCRIPTION 
  5.     This script is a re-write of an MSDN sample, using PowerShell 
  6. .NOTES 
  7.     File Name  : Show-CurrencyGroupSeparator.ps1 
  8.     Author     : Thomas Lee - tfl@psp.co.uk 
  9.     Requires   : PowerShell Version 3.0 
  10. .LINK 
  11.     This script posted to: 
  12.         http://www.pshscripts.blogspot.com 
  13.     MSDN sample posted to: 
  14.          http://msdn.microsoft.com/en-us/library/system.globalization.numberformatinfo.currencygroupseparator%28v=vs.100%29.aspx 
  15. .EXAMPLE 
  16.     PSH> .\Show-NumberGroupSizes.ps1 
  17.     $123,456,789,012,345.00 
  18.     $123 456 789 012 345.00 
  19. #> 
  20.  
  21. # Get Number Format 
  22. $nf  = New-Object System.Globalization.CultureInfo  "en-US", $False  
  23. $nfi = $nf.NumberFormat 
  24.  
  25. # Display a value with the default separator (","). 
  26. [Int64] $myInt = 123456789012345 
  27. $myInt.ToString( "C", $nfi ) 
  28.  
  29. # Displays the same value with a blank as the separator. 
  30. $nfi.CurrencyGroupSeparator = " " 
  31. $myInt.ToString( "C", $nfi ) 

Sunday, 24 February 2013

New-ZipFromDirectory.ps1


  1. <# 
  2. .SYNOPSIS 
  3.     Creates a new zip file from an existing folder 
  4. .DESCRIPTION 
  5.     This script uses the .NET 4.5 zipfile class  
  6.     to create a zip file, getting contents from  
  7.     a folder. 
  8. .NOTES 
  9.     File Name  : New-ZipfromDirectory 
  10.     Author     : Thomas Lee - tfl@psp.co.uk 
  11.     Requires   : PowerShell Version 3.0 and .NET 4.5 
  12. .LINK 
  13.     This script posted to: 
  14.         http://www.pshscripts.blogspot.com 
  15. .EXAMPLE 
  16.     Psh> C:\foo\new-zip.ps1 
  17.     Zip file created: 
  18.      
  19.     Directory: C:\foo 
  20.          
  21.     Mode                LastWriteTime     Length Name 
  22.     ----                -------------     ------ ---- 
  23.     -a---         2/24/2013   3:00 PM     291182 ScriptLib.ZIP 
  24.  
  25. #> 
  26.  
  27. # Load the compression namespace 
  28. # and yes, I know this usage is obsolete - but it works. 
  29. # Ignore the output 
  30. [System.Reflection.Assembly]::LoadWithPartialName('System.IO.Compression.FileSystem') | Out-Null  
  31.  
  32. # Create a type accellerator for the zipfile class 
  33. [System.Type] $TypeAcceleratorsType=[System.Management.Automation.PSObject].Assembly.GetType('System.Management.Automation.TypeAccelerators',$True,$True) 
  34. $TypeAcceleratorsType::Add('Zipfile','System.IO.Compression.Zipfile') 
  35.  
  36. # Now create a zip file 
  37. # Set target zip flie and source folder 
  38. $Folder  = 'E:\PowerShellScriptLib' 
  39. $Zipfile = 'C:\foo\ScriptLib.ZIP' 
  40.  
  41. # Ensure file does NOT exist and fodler DOES exist 
  42. If (Test-Path $zipfile -EA -0) { 
  43.    Throw "$Zipfile exists - not safe to continue"} 
  44. If (!(Test-Path $folder)) { 
  45.    Throw "$Folder does not seem to exist"} 
  46.     
  47. # Now create the Zip file 
  48. Try { 
  49.   [Zipfile]::CreateFromDirectory( $folder, $zipfile) 
  50.   "Zip file created:";ls $zipfile} 
  51. Catch { 
  52.   "Zip File NOT created" 
  53.   $Error[0]} 

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