Last active
August 29, 2015 14:06
-
-
Save zhujo01/5c99289bc1b9e02574f5 to your computer and use it in GitHub Desktop.
Amazon Windows bootstrapping script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
######################################################################## | |
# AWS EC2 Windows Bootstrapping Script | |
# Supported OS: | |
# - Windows 2008 Server R2 SP1 (TESTED) | |
# - Windows 2008 Server (TO BE TESTED) | |
# - Windows 2012 Server (TO BE TESTED) | |
# Image Transformation Target Cloud | |
# - VMWare vSphere & VCD | |
# | |
# Coppyright (c) 2014, CliQr Technologies, Inc. All rights reserved. | |
######################################################################## | |
param( | |
[Parameter(Mandatory=$true)][string]$AccessId, | |
[Parameter(Mandatory=$true)][string]$SecretKey, | |
[Parameter(Mandatory=$true)][string]$AdminPass, | |
[Parameter(Mandatory=$true)][string]$ImageName, | |
[Parameter(Mandatory=$true)][string]$ProductKey | |
) | |
Set-StrictMode -Version Latest | |
Set-ExecutionPolicy Unrestricted | |
$log = 'c:\Bootstrap.txt' | |
$systemPath = [Environment]::GetFolderPath([Environment+SpecialFolder]::System) | |
$sysNative = [IO.Path]::Combine($env:windir, "sysnative") | |
#http://blogs.msdn.com/b/david.wang/archive/2006/03/26/howto-detect-process-bitness.aspx | |
$Is32Bit = (($Env:PROCESSOR_ARCHITECTURE -eq 'x86') -and ($Env:PROCESSOR_ARCHITEW6432 -eq $null)) | |
Add-Content $log -value "Is 32-bit [$Is32Bit]" | |
#http://msdn.microsoft.com/en-us/library/ms724358.aspx | |
$coreEditions = @(0x0c,0x27,0x0e,0x29,0x2a,0x0d,0x28,0x1d) | |
$IsCore = $coreEditions -contains (Get-WmiObject -Query "Select OperatingSystemSKU from Win32_OperatingSystem" | Select -ExpandProperty OperatingSystemSKU) | |
Add-Content $log -value "Is Core [$IsCore]" | |
cd $Env:USERPROFILE | |
Set-Location -Path $Env:USERPROFILE | |
[Environment]::CurrentDirectory=(Get-Location -PSProvider FileSystem).ProviderPath | |
Add-Content $log -value "Changed Administrator password" | |
net user Administrator $AdminPass | |
$client = new-object System.Net.WebClient | |
#.net 4 | |
if ((Test-Path "${Env:windir}\Microsoft.NET\Framework\v4.0.30319") -eq $false) | |
{ | |
$netUrl = if ($IsCore) {'http://download.microsoft.com/download/3/6/1/361DAE4E-E5B9-4824-B47F-6421A6C59227/dotNetFx40_Full_x86_x64_SC.exe' } ` | |
else { 'http://download.microsoft.com/download/9/5/A/95A9616B-7A37-4AF6-BC36-D6EA96C8DAAE/dotNetFx40_Full_x86_x64.exe' } | |
$client.DownloadFile( $netUrl, 'C:\Users\Administrator\dotNetFx40_Full.exe') | |
Start-Process -FilePath 'C:\Users\Administrator\dotNetFx40_Full.exe' -ArgumentList '/norestart /q /ChainingPackage ADMINDEPLOYMENT' -Wait -NoNewWindow | |
Add-Content $log -value "Found that .NET4 was not installed and downloaded / installed" | |
} | |
#configure powershell to use .net 4 | |
$config = @' | |
<?xml version="1.0" encoding="utf-8" ?> | |
<configuration> | |
<!-- http://msdn.microsoft.com/en-us/library/w4atty68.aspx --> | |
<startup useLegacyV2RuntimeActivationPolicy="true"> | |
<supportedRuntime version="v4.0" /> | |
<supportedRuntime version="v2.0.50727" /> | |
</startup> | |
</configuration> | |
'@ | |
if (Test-Path "${Env:windir}\SysWOW64\WindowsPowerShell\v1.0\powershell.exe") | |
{ | |
$config | Set-Content "${Env:windir}\SysWOW64\WindowsPowerShell\v1.0\powershell.exe.config" | |
Add-Content $log -value "Configured 32-bit Powershell on x64 OS to use .NET 4" | |
} | |
if (Test-Path "${Env:windir}\system32\WindowsPowerShell\v1.0\powershell.exe") | |
{ | |
$config | Set-Content "${Env:windir}\system32\WindowsPowerShell\v1.0\powershell.exe.config" | |
Add-Content $log -value "Configured host OS specific Powershell at ${Env:windir}\system32\ to use .NET 4" | |
} | |
#enable powershell servermanager cmdlets (only for 2008 r2 + above) | |
if ($IsCore) | |
{ | |
DISM /Online /Enable-Feature /FeatureName:MicrosoftWindowsPowerShell /FeatureName:ServerManager-PSH-Cmdlets /FeatureName:BestPractices-PSH-Cmdlets | |
Add-Content $log -value "Enabled ServerManager and BestPractices Cmdlets" | |
#enable .NET flavors - on server core only -- errors on regular 2008 | |
DISM /Online /Enable-Feature /FeatureName:NetFx2-ServerCore /FeatureName:NetFx2-ServerCore-WOW64 /FeatureName:NetFx3-ServerCore /FeatureName:NetFx3-ServerCore-WOW64 | |
Add-Content $log -value "Enabled .NET frameworks 2 and 3 for x86 and x64" | |
} | |
#7zip | |
$7zUri = if ($Is32Bit) { 'http://sourceforge.net/projects/sevenzip/files/7-Zip/9.22/7z922.msi/download' } ` | |
else { 'http://sourceforge.net/projects/sevenzip/files/7-Zip/9.22/7z922-x64.msi/download' } | |
$client.DownloadFile( $7zUri, 'C:\Users\Administrator\7z922.msi') | |
Start-Process -FilePath "msiexec.exe" -ArgumentList '/i "C:\Users\Administrator\7z922.msi" /norestart /q INSTALLDIR="c:\program files\7-zip"' -Wait | |
SetX Path "${Env:Path};C:\Program Files\7-zip" /m | |
$Env:Path += ';C:\Program Files\7-Zip' | |
Add-Content $log -value "Installed 7-zip from $7zUri and updated path" | |
#vc 2010 redstributable | |
$vcredist = if ($Is32Bit) { 'http://download.microsoft.com/download/5/B/C/5BC5DBB3-652D-4DCE-B14A-475AB85EEF6E/vcredist_x86.exe'} ` | |
else { 'http://download.microsoft.com/download/3/2/2/3224B87F-CFA0-4E70-BDA3-3DE650EFEBA5/vcredist_x64.exe' } | |
$client.DownloadFile( $vcredist, 'C:\Users\Administrator\vcredist.exe') | |
Start-Process -FilePath 'C:\Users\Administrator\vcredist.exe' -ArgumentList '/norestart /q' -Wait | |
del 'C:\Users\Administrator\vcredist.exe' | |
Add-Content $log -value "Installed VC++ 2010 Redistributable from $vcredist and updated path" | |
#vc 2008 redstributable | |
$vcredist = if ($Is32Bit) { 'http://download.microsoft.com/download/d/d/9/dd9a82d0-52ef-40db-8dab-95376989c03/vcredist_x86.exe'} ` | |
else { 'http://download.microsoft.com/download/d/2/4/d242c3fb-da5a-4542-ad66-f9661d0a8d19/vcredist_x64.exe' } | |
$client.DownloadFile( $vcredist, 'C:\Users\Administrator\vcredist.exe') | |
Start-Process -FilePath 'C:\Users\Administrator\vcredist.exe' -ArgumentList '/norestart /q' -Wait | |
del C:\Users\Administrator\vcredist.exe | |
Add-Content $log -value "Installed VC++ 2008 Redistributable from $vcredist and updated path" | |
Add-Content $log -value "Start to install toolchain" | |
#$client.DownloadFile('https://s3.amazonaws.com/osmosixdev-test/VMware-converter-en-5.5.2-1890136.exe', 'VMware-converter-en-5.5.2-1890136.exe') | |
#Start-Process -FilePath 'C:\Windows\System32\config\systemprofile\VMware-converter-en-5.5.2-1890136.exe' -ArgumentList '/S /v/qn' -Wait | |
$client.DownloadFile('https://s3.amazonaws.com/osmosixdev-test/VMware-ovftool-3.5.0-1274719-win.x86_64.msi', 'C:\Users\Administrator\VMware-ovftool-3.5.0-1274719-win.x86_64.msi') | |
Start-Process -FilePath 'msiexec' -ArgumentList '/quiet /qn /i C:\Users\Administrator\VMware-ovftool-3.5.0-1274719-win.x86_64.msi' -Wait | |
$client.DownloadFile('http://mage.cliqrqa.com/mage/workers/cliqr_installer.exe', 'C:\Users\Administrator\cliqr_installer.exe') | |
Start-Process -FilePath 'C:\Users\Administrator\cliqr_installer.exe' -ArgumentList '/CLOUDTYPE=vcd /CLOUDREGION=default /verysilent' -Wait | |
$client.DownloadFile( "https://s3.amazonaws.com/osmosixdev-test/qemu.zip", 'C:\Users\Administrator\qemu.zip') | |
&7z x 'C:\Users\Administrator\qemu.zip' `-o`"c:\program files`" | |
SetX Path "${Env:Path};C:\Program Files\qemu;C:\Program Files\VMware\VMware OVF Tool" /m | |
$Env:Path += ';C:\Program Files\qemu;C:\Program Files\VMware\VMware OVF Tool' | |
# Disable EC2 agent service | |
Set-Service -name Ec2Config -startupType Disabled | |
Set-Service -name xensvc -startupType Disabled | |
#Set-Service -name 'VM Tools' -startupType Automatic | |
#Set-Service -name 'AWSLiteAgent' -startupType Disabled | |
Start-Sleep -s 10 | |
# TODO: Call pre bundle script | |
# | |
# Install product key | |
if ($ProductKey -eq 'null') { | |
Add-Content $log -value "No product key provided" | |
} else { | |
Add-Content $log -value "A product key is provided" | |
Set-Content -value "$ProductKey" -path 'C:\temp\product.key' | |
} | |
# Install vmware tools | |
##$client.DownloadFile('https://s3.amazonaws.com/osmosixdev-test/myvmtools.zip', 'myvmtools.zip') | |
##&7z x myvmtools.zip | |
##Start-Process -FilePath 'msiexec' -ArgumentList '-i "C:\Users\Administrator\vmtools\VMware Tools64.msi" /qn ADDLOCAL=ALL REMOVE="Audio,VMXNet,VMXNet3,Buslogic,VSS,WYSE,VShield,PVSCSI" REBOOT=ReallySuppress /lv c:\install.log' -Wait | |
# Add vmtools installation script | |
Remote-Item "C:\Program Files\osmosix\bin\startup.cmd" | |
$client.DownloadFile('https://gist.github.com/zhujo01/db90b83e463373997f2c/raw', 'C:\Program Files\osmosix\bin\startup.cmd') | |
Start-Sleep -s 30 | |
# Bundle | |
Add-Content $log -value "Creating bundle..." | |
$client.DownloadFile('https://s3.amazonaws.com/osmosixdev-test/disk2vhd.exe', 'C:\Users\Administrator\disk2vhd.exe') | |
Start-Process -FilePath 'C:\Users\Administrator\disk2vhd.exe' -ArgumentList "c: z:\disk.vhd -accepteula" -Wait | |
Add-Content $log -value "Done." | |
# Resignature system disk | |
$dpCmd = @" | |
select disk 0 | |
uniqueid disk id=88888888 | |
"@ | |
$dpCmd | diskpart | |
# Attach vdh | |
$dpCmd = @" | |
select vdisk file=z:\disk.vhd | |
attach vdisk | |
online disk | |
"@ | |
$dpCmd | diskpart | |
# Install VMWare drivers | |
##$client.DownloadFile('https://s3.amazonaws.com/osmosixdev-test/vmdrivers.zip', 'vmdrivers.zip') | |
##&7z x vmdrivers.zip | |
# Unlock files | |
##gci -Path C:\Users\Administrator\Drivers -Recurse | Unblock-File | |
##dism /Image:d:\ /Add-Driver /Driver:C:\Users\Administrator\Drivers\ /Recurse /ForceUnsigned | |
Start-Sleep -s 10 | |
# Fix registry | |
reg load 'HKLM\p2v' d:\windows\system32\config\SYSTEM | |
Set-ItemProperty -Path HKLM:\p2v\ControlSet001\services\LSI_SCSI -Name Start -Value 0 | |
Set-ItemProperty -Path HKLM:\p2v\ControlSet001\services\LSI_SAS -Name Start -Value 0 | |
reg unload 'HKLM\p2v' | |
Start-Sleep -s 10 | |
# Detach vhd | |
$dpCmd = @" | |
select vdisk file=z:\disk.vhd | |
detach vdisk | |
"@ | |
$dpCmd | diskpart | |
# TODO: Call post bundle script | |
# | |
# Convert VHD to VMDK | |
&qemu-img convert -O vmdk z:\disk.vhd z:\disk.vmdk | |
# Convert VMDK | |
#$client.DownloadFile('https://s3.amazonaws.com/osmosixdev-test/vm.ovf', 'y:\vm.ovf') | |
$client.DownloadFile('https://gist.githubusercontent.com/zhujo01/26aa4ad88a06beb1a1aa/raw', 'z:\vm.tmp') | |
$capacity = (qemu-img info z:\disk.vmdk |select-string '^virtual size:' | foreach { $_.ToString().split(":(")[1]} | foreach { $_.ToString().split("G ")[1]}) | |
Add-Content $log -value "VM capacity: $capacity" | |
Get-Content 'z:\vm.tmp' | %{$_ -creplace '%VM_CAPACITY%', $capacity} | Set-Content 'z:\vm.ovf' | |
&ovftool z:\vm.ovf z:\$ImageName.ovf | |
# Bundle | |
Add-Content $log -value "Uploading bundle..." | |
$client.DownloadFile('http://mvnrepo.cliqr.com/install/gof3r.exe', 'C:\Users\Administrator\gof3r.exe') | |
$Env:AWS_ACCESS_KEY_ID = $AccessId | |
$Env:AWS_SECRET_ACCESS_KEY = $SecretKey | |
Start-Process -FilePath 'C:\Users\Administrator\gof3r.exe' -ArgumentList "put /p z:\$ImageName-disk1.vmdk /b osmosixdev-test /k $ImageName-disk1.vmdk /c 20"-Wait | |
Start-Process -FilePath 'C:\Users\Administrator\gof3r.exe' -ArgumentList "put /p z:\$ImageName.ovf /b osmosixdev-test /k $ImageName.ovf /c 20" -Wait | |
Add-Content $log -value "Done." | |
#Start-Sleep -m 300000 | |
Add-Content $log -value "Cleaning up..." | |
$Env:AWS_ACCESS_KEY_ID = "*" | |
$Env:AWS_SECRET_ACCESS_KEY = "*" | |
#cd C:\Users\Administrator | |
#Remove-Item *.* | |
# shutdown /s /f |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment