Created
October 20, 2015 03:48
-
-
Save michaeltlombardi/f1f6c0d6b93a215efc07 to your computer and use it in GitHub Desktop.
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
Function New-NanoElasticsearchVM { | |
param ( | |
[PSCredential]$Credential = (Get-Credential -UserName Administrator -Message "Enter your password; DO NOT CHANGE THE USERNAME!"), | |
[string]$ComputerName = "nes01", | |
[string]$ISOPath = "$env:HOMEDRIVE$Env:HomePath\downloads\10514.0.150808-1529.TH2_RELEASE_SERVER_OEMRET_X64FRE_EN-US.ISO", | |
[string]$MediaPath = "C:\W2016Media", | |
[string]$NanoPath = "C:\nano", | |
[int64]$MemoryStartupBytes = (2*1024*1024*1024), | |
[string]$VMSwitchName = "vExternal" | |
) | |
#region Prepare VHD | |
# Mount the ISO, then copy it to a local folder, then dismount the ISO. | |
$MountedISO = Mount-DiskImage -ImagePath $ISOPath -PassThru | |
$MountedISOVolume = Get-DiskImage -ImagePath $MountedISO.ImagePath | Get-Volume | |
Copy-Item -Path "$($MountedISOVolume.DriveLetter):\" -Recurse -Container -Destination $MediaPath -Force | |
Dismount-DiskImage -ImagePath $ISOPath | |
# Also copy the Nano Server files to a separate directory. This is where the image will be built and saved. | |
Copy-Item -Path $MediaPath\NanoServer -Container -Recurse -Destination $NanoPath -Force | |
# Move into the folder containing the nano-server script files; New-NanoServerImage will fail otherwise. | |
Set-Location -Path $NanoPath | |
. .\new-nanoserverimage.ps1 | |
# Create the new nano-server image with guest drivers, reverse forwarders (I thought this would help with Java), | |
# remote management, and the admin password you passed in the beginning. | |
New-NanoServerImage -MediaPath $MediaPath ` | |
-BasePath .\Base ` | |
-TargetPath .\$ComputerName ` | |
-ComputerName $ComputerName ` | |
-GuestDrivers ` | |
-ReverseForwarders ` | |
-EnableRemoteManagementPort ` | |
-EnableIPDisplayOnBoot ` | |
-AdministratorPassword $Credential.Password | |
#endregion Prepare VHD | |
#region Provision and Configure VM | |
# Set the properties; in this case, nano-server doesn't seem to play nice | |
# with generation 2 virtual hardware. | |
$VirtualMachineParameters = @{ | |
"Name" = $ComputerName; | |
"BootDevice" = [string]"VHD" | |
"MemoryStartupBytes" = $MemoryStartupBytes; | |
"Generation" = [int32]1; | |
"SwitchName" = $VMSwitchName; | |
"VHDPath" = [string]"$NanoPath\$ComputerName\$ComputerName.vhd" | |
} | |
# Create the VM and start it. | |
New-VM @VirtualMachineParameters | Start-VM | |
# Add the VM to the trusted hosts list so it can be managed. | |
Set-Item WSMan:\localhost\Client\TrustedHosts -Value $ComputerName -Force | |
# So now, we turn off the firewall. Because that makes it work? | |
# I WILL LOOK INTO THIS LATER, I PROMISE. | |
Invoke-Command -VMName $ComputerName -Credential $Credential -ScriptBlock { | |
Get-NetFirewallProfile | Set-NetFirewallProfile –Enabled False | |
} | |
# Set a new PSSession with the VM. | |
$Session = New-PSSession -ComputerName $ComputerName -Credential $Credential | |
# Download JDK8. It has to be done this (dumb) way because Oracle insists on including a license agreement before allowing a download. | |
# Luckily StackOverflow saved me, per usual. http://stackoverflow.com/questions/24430141/downloading-jdk-using-powershell | |
$JDKSource = "http://download.oracle.com/otn-pub/java/jdk/8u60-b27/jdk-8u60-windows-x64.exe" | |
$JDKDestination = "C:\jdk.exe" | |
$JDKDownloadClient = new-object System.Net.WebClient | |
$JDKDownloadCookie = "oraclelicense=accept-securebackup-cookie" | |
$JDKDownloadClient.Headers.Add([System.Net.HttpRequestHeader]::Cookie, $JDKDownloadCookie) | |
$JDKDownloadClient.downloadFile($JDKSource, $JDKDestination) | |
#Copy the JDK installation file into the nano server. | |
Copy-Item -Path $JDKDestination -Destination C:\ -ToSession $Session | |
# Discovered the script for installing JDK silently at http://blag.koveras.org/2011/12/24/how-to-install-jdk-from-powershell-silently/ | |
$JDKInstallationScripBlock = { | |
$JDKInstallationDirectory = 'C:\Program Files\JDK\' | |
$JDKInstallationArguments = @( | |
'/s', | |
"/v/qn`" INSTALLDIR=\`"$JDKInstallationDirectory\`" REBOOT=Supress IEXPLORER=0 MOZILLA=0 /L \`"install.log\`"`"" | |
) | |
$JDKInstallationProcess = Start-Process "C:\jdk.exe" -ArgumentList $JDKInstallationArguments -Wait -PassThru | |
If($JDKInstallationProcess.ExitCode -ne 0) { Throw "ERROR; EXIT CODE $($JDKInstallationProcess.ExitCode)"} | |
} | |
# Run the installer. | |
Invoke-Command -Session $Session -ScriptBlock $JDKInstallationScripBlock | |
# Exit Code -1073741515 | |
# DLL not found... Great. No point in continuing on to attempt to install elasticsearch then... | |
#endregion Provision and Configure VM | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment