provision.ps1 2.79 KB
Newer Older
1

2
Start-Transcript -Path C:\provision.log -Append
3

4 5 6 7
Function SetupPhase1 {
  Cscript $env:WinDir\System32\SCregEdit.wsf /AU 1
  Net stop wuauserv
  Net start wuauserv
8

9
  Set-MpPreference -DisableRealtimeMonitoring $true
10

11
  New-ItemProperty -Path HKCU:\Software\Microsoft\ServerManager -Name DoNotOpenServerManagerAtLogon -PropertyType DWORD -Value "1" -Force
12

Stefan Scherer's avatar
Stefan Scherer committed
13
  Write-Host "Installing Chocolatey"
14 15 16 17
  iex (wget 'https://chocolatey.org/install.ps1' -UseBasicParsing)
  choco feature disable --name showDownloadProgress
  choco install -y git
  choco install -y packer
Stefan Scherer's avatar
Stefan Scherer committed
18
  choco install -y vagrant
19

Stefan Scherer's avatar
Stefan Scherer committed
20
  Write-Host "Installing Hyper-V"
21 22 23
  Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V -All -NoRestart
  Install-WindowsFeature Hyper-V-Tools
  Install-WindowsFeature Hyper-V-PowerShell
24

Stefan Scherer's avatar
Stefan Scherer committed
25
#Write-Host Install all Windows Updates
26 27 28 29
#Get-Content C:\windows\system32\en-us\WUA_SearchDownloadInstall.vbs | ForEach-Object {
#  $_ -replace 'confirm = msgbox.*$', 'confirm = vbNo'
#} | Out-File $env:TEMP\WUA_SearchDownloadInstall.vbs
#"a`na" | cscript $env:TEMP\WUA_SearchDownloadInstall.vbs
30

Stefan Scherer's avatar
Stefan Scherer committed
31
  Write-Host "Rebooting"
32 33
  Restart-Computer
}
34

35
Function SetupPhase2 {
36

Stefan Scherer's avatar
Stefan Scherer committed
37 38 39
  Write-Host "Installing Vagrant plugins"
  vagrant plugin install vagrant-reload

Stefan Scherer's avatar
Stefan Scherer committed
40
  Write-Host "Adding NAT"
41 42 43
  New-VMSwitch -SwitchName "packer-hyperv-iso" -SwitchType Internal
  New-NetIPAddress -IPAddress 192.168.0.1 -PrefixLength 24 -InterfaceIndex (Get-NetAdapter -name "vEthernet (packer-hyperv-iso)").ifIndex
  New-NetNat -Name MyNATnetwork -InternalIPInterfaceAddressPrefix 192.168.0.0/24
44

Stefan Scherer's avatar
Stefan Scherer committed
45
  Write-Host "Adding DHCP scope"
46 47 48
  Install-WindowsFeature DHCP -IncludeManagementTools
  Add-DhcpServerv4Scope -Name "Internal" -StartRange 192.168.0.10 -EndRange 192.168.0.250 -SubnetMask 255.255.255.0 -Description "Internal Network"
  Set-DhcpServerv4OptionValue -ScopeID 192.168.0 -DNSServer 8.8.8.8 -Router 192.168.0.1
49

Stefan Scherer's avatar
Stefan Scherer committed
50 51 52
  Write-Host "Allow Packer http server"
  New-NetFirewallRule -DisplayName "Allow Packer" -Direction Inbound -Program "C:\ProgramData\chocolatey\lib\packer\tools\packer.exe" -RemoteAddress LocalSubnet -Action Allow

Stefan Scherer's avatar
Stefan Scherer committed
53
  Write-Host "Disabling autologon"
54
  New-ItemProperty -Path "HKCU:\Software\Microsoft\Windows NT\CurrentVersion\Winlogon" -Name AutoAdminLogon -PropertyType DWORD -Value "0" -Force
55

Stefan Scherer's avatar
Stefan Scherer committed
56
  Write-Host "Removing scheduled job"
57 58
  Unregister-ScheduledJob -Name NewServerSetupResume -Force
}
59

60 61 62 63 64 65 66 67 68 69 70 71
if (!(Test-Path c:\ProgramData\chocolatey)) {
  $pwd = ConvertTo-SecureString -String $Password -AsPlainText -Force
  $cred = New-Object System.Management.Automation.PSCredential($Username, $pwd)
  $AtStartup = New-JobTrigger -AtStartup
  Register-ScheduledJob -Name NewServerSetupResume `
                        -Credential $cred `
                        -Trigger $AtStartup `
                        -ScriptBlock { c:\provision.ps1 }
  SetupPhase1
} else {
  SetupPhase2
}