Skip to content

Instantly share code, notes, and snippets.

@slide

slide/init_script.ps1

Last active Jul 9, 2019
Embed
What would you like to do?
# Image publish: MicrosoftWindowsServer
# Image offer: WindowsServer
# Image Sku: 2019-Datacenter-with-Containers
# Init Script:
Set-ExecutionPolicy Unrestricted
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$baseDir = 'c:\azurecsdir'
$JDKUrl = 'https://github.com/AdoptOpenJDK/openjdk11-binaries/releases/download/jdk-11.0.3%2B7/OpenJDK11U-jre_x64_windows_hotspot_11.0.3_7.zip'
$destinationJDKZipPath = "$baseDir\adoptOpenJDK.zip"
$javaHome = "$baseDir\jdk-11.0.3+7-jre"
$vsBuildToolsUrl = 'https://download.visualstudio.microsoft.com/download/pr/aab801bf-dcd0-4d7c-8552-a0c3b4fee032/5a2cee2a57d38e90f6a555044782097f/vs_buildtools.exe'
$destinationVSBuiltToolsExePath = "$baseDir\vs_buildtools.exe"
$VSToolsPath = "$baseDir\vstools\MSBuild\Current\Bin\"
$GITUrl = 'https://github.com/git-for-windows/git/releases/download/v2.10.0.windows.1/MinGit-2.10.0-64-bit.zip'
$destinationGITZipPath = "$baseDir\MinGit.zip"
$GITPath = "$baseDir\git\cmd\"
# Function to get path of script file
function Get-ScriptPath
{
return $MyInvocation.ScriptName;
}
function DownloadFile($url, $targetFile)
{
$uri = New-Object "System.Uri" "$url"
$request = [System.Net.HttpWebRequest]::Create($uri)
$request.set_Timeout(15000) #15 second timeout
$response = $request.GetResponse()
$totalLength = [System.Math]::Floor($response.get_ContentLength()/1024)
$responseStream = $response.GetResponseStream()
$targetStream = New-Object -TypeName System.IO.FileStream -ArgumentList $targetFile, Create
$buffer = new-object byte[] 10KB
$count = $responseStream.Read($buffer,0,$buffer.length)
$downloadedBytes = $count
while ($count -gt 0) {
$targetStream.Write($buffer, 0, $count)
$count = $responseStream.Read($buffer,0,$buffer.length)
$downloadedBytes = $downloadedBytes + $count
Write-Progress -activity "Downloading file '$($url.split('/') | Select -Last 1)'" -status "Downloaded ($([System.Math]::Floor($downloadedBytes/1024))K of $($totalLength)K): " -PercentComplete ((([System.Math]::Floor($downloadedBytes/1024)) / $totalLength) * 100)
}
Write-Progress -activity "Finished downloading file '$($url.split('/') | Select -Last 1)'"
$targetStream.Flush()
$targetStream.Close()
$targetStream.Dispose()
$responseStream.Dispose()
}
# Checking if this is first time script is getting executed, if yes then downloading JDK, Git and VS Tools
If(-not((Test-Path $destinationJDKZipPath)))
{
md -Path $baseDir -Force
DownloadFile $JDKUrl $destinationJDKZipPath
$shell_app = new-object -com shell.application
$zip_file = $shell_app.namespace($destinationJDKZipPath)
$javaInstallDir = $shell_app.namespace($baseDir)
$javaInstallDir.Copyhere($zip_file.items())
DownloadFile $GITUrl $destinationGITZipPath
md -Path "$baseDir\git" -Force
$zip_file = $shell_app.namespace($destinationGITZipPath)
$gitInstallDir = $shell_app.namespace("$baseDir\git")
$gitInstallDir.Copyhere($zip_file.items())
& "$GITPath\git" config --system core.autocrlf false
& "$GITPath\git" config --system core.longpaths true
# install the VS2019 Build Tools, this gets us msbuild and signtool for signing
md -Path "$baseDir\vstools" -Force
DownloadFile $vsBuildToolsUrl $destinationVSBuiltToolsExePath
Start-Process -FilePath "$destinationVSBuiltToolsExePath" -ArgumentList "--installPath", "$baseDir\vstools", "--passive", "--wait", "--norestart", "--add", "Microsoft.VisualStudio.Component.Windows10SDK.17763", "--remove", "Microsoft.VisualStudio.Component.Windows10SDK.10240", "--remove", "Microsoft.VisualStudio.Component.Windows10SDK.10586", "--remove", "Microsoft.VisualStudio.Component.Windows10SDK.14393", "--remove", "Microsoft.VisualStudio.Component.Windows81SDK" -Wait -PassThru | Out-Null
# update the system path to include Git, Java, VSTools and the Windows kits (for signtool)
$oldPath = (Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH).path
$newPath = "$oldPath;$javaHome\bin;$GITPath;$VSToolsPath;C:\Program Files (x86)\Windows Kits\10\bin\10.0.17763.0\x64"
Set-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH -Value $newPath
# setup JAVA_HOME environment variable
New-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name JAVA_HOME -Value $javaHome
# enable .NET 3.5 feature
Add-WindowsFeature NET-Framework-Core
}
Write-Host 'Done Init Script.'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment