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"  

1 comment:

Sudhir Chekuri said...

Nice post. Got a lot of important stuff from this blog.
dba kings