Created
January 20, 2015 10:44
-
-
Save devlead/2439b15ad2d960af5641 to your computer and use it in GitHub Desktop.
PowerShell Script to easily provision a dev environment in Azure using Chocolatey
This file contains hidden or 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
Param( | |
[string]$SubscriptionName = ((Get-AzureSubscription|?{$_.SubscriptionName -like "*MSDN*"})[0].SubscriptionName), | |
[string]$AzureVMImageName = "03f55de797f546a1b29d1b8d66be687a__Visual-Studio-2013-Ultimate-Update4-AzureSDK-2.5-Win8.1-x64" | |
) | |
function InstallWinRMCert($serviceName, $vmname) | |
{ | |
$winRMCert = (Get-AzureVM -ServiceName $serviceName -Name $vmname | select -ExpandProperty vm).DefaultWinRMCertificateThumbprint | |
$AzureX509cert = Get-AzureCertificate -ServiceName $serviceName -Thumbprint $winRMCert -ThumbprintAlgorithm sha1 | |
$certTempFile = [IO.Path]::GetTempFileName() | |
$AzureX509cert.Data | Out-File $certTempFile | |
# Target The Cert That Needs To Be Imported | |
$CertToImport = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2 $certTempFile | |
$store = New-Object System.Security.Cryptography.X509Certificates.X509Store "Root", "LocalMachine" | |
$store.Certificates.Count | |
$store.Open([System.Security.Cryptography.X509Certificates.OpenFlags]::ReadWrite) | |
$store.Add($CertToImport) | |
$store.Close() | |
Remove-Item $certTempFile | |
} | |
Add-AzureAccount | |
$sw = [System.Diagnostics.StopWatch]::StartNew() | |
#Configurarion | |
$newMachine = @{ | |
Machine = ("t" + [Guid]::NewGuid().ToString("N").Substring(0,14)); | |
Username = ("U" + [Guid]::NewGuid().ToString("N").Substring(0,14)); | |
Password = ("&%!" + [Guid]::NewGuid().ToString("N") + "@"); | |
AzureVMImageName = $AzureVMImageName; | |
Location = "North Europe"; | |
InstanceSize = "Standard_D3"; | |
LogFile = "AzureMachines.txt"; | |
SubscriptionName = $SubscriptionName | |
} | |
#Store & output new machine config | |
$newMachine|Out-File -Append -FilePath $newMachine.LogFile | |
$newMachine|Format-List | |
#Encrypt credentials for PowerShell Remoting | |
$secpasswd = ConvertTo-SecureString $newMachine.Password -AsPlainText -Force | |
$credential = New-Object System.Management.Automation.PSCredential ($newMachine.Username, $secpasswd) | |
# Create Machine | |
New-AzureStorageAccount -StorageAccountName $newMachine.Machine -Location $newMachine.Location | |
Set-AzureSubscription -SubscriptionName $newMachine.SubscriptionName -CurrentStorageAccountName $newMachine.Machine | |
New-AzureVMConfig -Name $newMachine.Machine -InstanceSize $newMachine.InstanceSize -ImageName $newMachine.AzureVMImageName| | |
Add-AzureProvisioningConfig -Windows -AdminUsername $newMachine.Username -Password $newMachine.Password | | |
Add-AzureEndpoint -Name "PowerShell" -Protocol tcp -LocalPort 5986 -PublicPort 5986 | | |
Add-AzureEndpoint -Name "Remote Desktop" -Protocol tcp -LocalPort 3389 -PublicPort 3389 | | |
New-AzureVM -ServiceName $newMachine.Machine -Location $newMachine.Location -WaitForBoot | |
#Store & output remoting uri | |
$uri = Get-AzureWinRMUri -ServiceName $newMachine.Machine -Name $newMachine.Machine | |
$uri.ToString() |Out-File -Append -FilePath $newMachine.LogFile; | |
# Install Machine Managment Certificate | |
InstallWinRMCert $newMachine.Machine $newMachine.Machine | |
#Install Chocolatey | |
Invoke-Command -ConnectionUri $uri.ToString() -Credential $credential -ScriptBlock { | |
Set-ExecutionPolicy Unrestricted | |
iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1')) | |
} | |
Invoke-Command -ConnectionUri $uri.ToString() -Credential $credential -ScriptBlock { | |
#Install GIT | |
choco install poshgit | |
#Install SysInternals tools | |
choco install sysinternals | |
#Install Cake Buildsystem Bootstrapper | |
choco install cake-bootstrapper | |
#Install Sublime | |
choco install SublimeText3 | |
choco install sublimetext3.powershellalias | |
#Install Browsers | |
choco install google-chrome-x64 | |
choco install firefox | |
#Make PowerShell Default Shell | |
REG Add HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced /V DontUsePowerShellOnWinX /T REG_DWORD /D 0 /F | |
} | |
$sw.Stop() | |
Get-AzureRemoteDesktopFile -Name $newMachine.Machine -ServiceName $newMachine.Machine -Launch | |
$newMachine |Format-List | |
$sw |Format-List |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Step 0, How to install and configure Azure PowerShell:
http://azure.microsoft.com/en-us/documentation/articles/install-configure-powershell/