Last active
March 19, 2018 18:50
-
-
Save gravcat/66f0728a5d1192ef8ba33a6bb8905c58 to your computer and use it in GitHub Desktop.
enables you to quickly create a single azure vm with all the basic required supporting infrastructure
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
| <# ----------------------------------------------------------------------------- | |
| vm_create_and_run_cse.ps1 | |
| .Description | |
| Create an adhoc VM completely standalone, and run CSE if you'd like. | |
| Get you some OSes! : https://docs.microsoft.com/en-us/azure/virtual-machines/windows/cli-ps-findimage | |
| Publishers | |
| - MicrosoftWindowsServer | |
| - Canonical | |
| - MicrosoftVisualStudio (w10) | |
| Offers | |
| - WindowsServer | |
| - UbuntuServer | |
| - windows # (w10) | |
| SKUs | |
| - 2012-R2-Datacenter | |
| - 2016-Datacenter | |
| - 16.04.0-LTS | |
| - Windows-10-N-x64 | |
| .Usage | |
| .\vm_create_adhoc_machine_cse.ps1 | |
| ----------------------------------------------------------------------------- #> | |
| param ( | |
| # | |
| # operating system parameters | |
| # | |
| [String] | |
| [ValidateSet("w10","ws2008","ws2012","ws2016","ubuntu14","ubuntu16")] | |
| $defaults, | |
| [String] | |
| $location = 'westus', | |
| [String] | |
| [ValidateSet("windows","linux")] | |
| $osType = 'windows', | |
| [String] | |
| $publisherName = "MicrosoftVisualStudio", | |
| [String] | |
| $offer = "windows", | |
| [String] | |
| $sku = "Windows-10-N-x64", | |
| [String] | |
| $version = "latest", | |
| # | |
| # machine OS credentials | |
| # - password is autogenerated if not passed in | |
| # | |
| [String] | |
| $userName = "nucleus", | |
| [String] | |
| $password, | |
| [String] | |
| $sshPubKeyPath, | |
| # | |
| # azure resource parameters | |
| # | |
| [String] | |
| $resourceGroupName, | |
| [String] | |
| $vmName, | |
| # 2 cores, 7GB RAM | |
| [String] | |
| $vmSize = "Standard_DS2_v2", | |
| # (optional) public URL of script to execute on the newly created machine | |
| [String] | |
| $postDeployScriptUri | |
| ) | |
| $ErrorActionPreference = 'Stop' | |
| if ($defaults) { | |
| if ($defaults -eq "w10") { | |
| Write-Output "Defaults selected for Windows 10" | |
| $osType = 'windows' | |
| $publisherName = "MicrosoftVisualStudio" | |
| $sku = "Windows-10-N-x64" | |
| $offer = "windows" | |
| } | |
| if ($defaults -eq "ws2008") { | |
| Write-Output "Defaults selected for Windows Server 2008 R2 SP1" | |
| $osType = 'windows' | |
| $publisherName = "MicrosoftWindowsServer" | |
| $sku = "2008-R2-SP1" | |
| $offer = "WindowsServer" | |
| } | |
| if ($defaults -eq "ws2012") { | |
| Write-Output "Defaults selected for Windows Server 2012 R2 Datacenter" | |
| $osType = 'windows' | |
| $publisherName = "MicrosoftWindowsServer" | |
| $sku = "2012-R2-Datacenter" | |
| $offer = "WindowsServer" | |
| } | |
| if ($defaults -eq "ws2016") { | |
| Write-Output "Defaults selected for Windows Server 2016 Datacenter" | |
| $osType = 'windows' | |
| $publisherName = "MicrosoftWindowsServer" | |
| $sku = "2016-Datacenter" | |
| $offer = "WindowsServer" | |
| } | |
| if ($defaults -eq "ubuntu14") { | |
| Write-Output "Defaults selected for Ubuntu 14.04.5 LTS" | |
| $osType = 'linux' | |
| $publisherName = "Canonical" | |
| $sku = "14.04.5-LTS" | |
| $offer = "UbuntuServer" | |
| } | |
| if ($defaults -eq "ubuntu16") { | |
| Write-Output "Defaults selected for Ubuntu 16.04 LTS" | |
| $osType = 'linux' | |
| $publisherName = "Canonical" | |
| $sku = "16.04-LTS" | |
| $offer = "UbuntuServer" | |
| } | |
| } | |
| function New-RandomComplexPassword ($length=12) | |
| { | |
| $Assembly = Add-Type -AssemblyName System.Web | |
| $password = [System.Web.Security.Membership]::GeneratePassword($length,2) | |
| return $password | |
| } | |
| # Auth to Azure if not already | |
| try { | |
| $null = Get-AzureRmResourceGroup | |
| } | |
| catch { | |
| Login-AzureRmAccount | |
| } | |
| # generate some IDs if none are provided | |
| if (!($resourceGroupName)) { | |
| $rngNum = Get-Random -Minimum 100 -Maximum 999 | |
| $resourceGroupName = $env:username + $rngNum | |
| } | |
| if (!($vmName)) { | |
| $vmName = "$resourceGroupName-vm" | |
| } | |
| Write-Output "Creating resource group $resourceGroupName" | |
| $null = New-AzureRmResourceGroup -Name $resourceGroupName -Location $location | |
| Write-Output "Creating storage account" | |
| $storAcc = New-AzureRmStorageAccount ` | |
| -ResourceGroupName $resourceGroupName ` | |
| -Name ($resourceGroupName + "boot") ` | |
| -SkuName "Standard_LRS" ` | |
| -Kind "Storage" ` | |
| -Location $location | |
| Write-Output "Creating subnet configuration" | |
| $subnetConfig = New-AzureRmVirtualNetworkSubnetConfig -Name "$resourceGroupName-subnet" -AddressPrefix 192.168.1.0/24 | |
| Write-Output "Creating vnet" | |
| $vnet = New-AzureRmVirtualNetwork ` | |
| -ResourceGroupName "$resourceGroupName" ` | |
| -Location $location ` | |
| -Name "$resourceGroupName-vnet" ` | |
| -AddressPrefix 192.168.0.0/16 ` | |
| -Subnet $subnetConfig | |
| Write-Output "Creating public IP" | |
| $pip = New-AzureRmPublicIpAddress ` | |
| -ResourceGroupName "$resourceGroupName" ` | |
| -Location $location ` | |
| -AllocationMethod Static ` | |
| -IdleTimeoutInMinutes 4 ` | |
| -Name "$vmName-ip" | |
| Write-Output "Creating NSG and rules" | |
| $nsgRuleRDP = New-AzureRmNetworkSecurityRuleConfig ` | |
| -Name "$resourceGroupName-rdp" ` | |
| -Protocol Tcp ` | |
| -Direction Inbound ` | |
| -Priority 1500 ` | |
| -SourceAddressPrefix * ` | |
| -SourcePortRange * ` | |
| -DestinationAddressPrefix * ` | |
| -DestinationPortRange 3389 ` | |
| -Access Allow | |
| $nsgRuleWinRM = New-AzureRmNetworkSecurityRuleConfig ` | |
| -Name "$resourceGroupName-winrm" ` | |
| -Protocol Tcp ` | |
| -Direction Inbound ` | |
| -Priority 1600 ` | |
| -SourceAddressPrefix * ` | |
| -SourcePortRange * ` | |
| -DestinationAddressPrefix * ` | |
| -DestinationPortRange 5986 ` | |
| -Access Allow | |
| $nsgRuleSSH = New-AzureRmNetworkSecurityRuleConfig ` | |
| -Name "$resourceGroupName-ssh" ` | |
| -Protocol Tcp ` | |
| -Direction Inbound ` | |
| -Priority 1700 ` | |
| -SourceAddressPrefix * ` | |
| -SourcePortRange * ` | |
| -DestinationAddressPrefix * ` | |
| -DestinationPortRange 22 ` | |
| -Access Allow | |
| # Create a network security group | |
| $nsg = New-AzureRmNetworkSecurityGroup ` | |
| -ResourceGroupName $resourceGroupName ` | |
| -Location $location ` | |
| -Name "$resourceGroupName-nsg" ` | |
| -SecurityRules $nsgRuleRDP, $nsgRuleSSH, $nsgRuleWinRM | |
| Write-Output "Creating vNIC and associating with NSG/IP" | |
| $nic = New-AzureRmNetworkInterface ` | |
| -Name "$vmName-nic" ` | |
| -ResourceGroupName $resourceGroupName ` | |
| -Location $location ` | |
| -SubnetId $vnet.Subnets[0].Id ` | |
| -PublicIpAddressId $pip.Id ` | |
| -NetworkSecurityGroupId $nsg.Id | |
| Write-Output "Creating credentials" | |
| if (!($password)) | |
| { | |
| # create a password if one was not provided | |
| $password = New-RandomComplexPassword | |
| } | |
| $securePassword = ConvertTo-SecureString $password -AsPlainText -Force | |
| $cred = New-Object System.Management.Automation.PSCredential($username,$securePassword) | |
| Write-Output "Creating vmConfig object to describe VM" | |
| if ($osType -eq "windows") { | |
| $vmConfig = New-AzureRmVMConfig ` | |
| -VMName $vmName ` | |
| -VMSize $vmSize | ` | |
| Set-AzureRmVMOperatingSystem ` | |
| -Windows ` | |
| -ComputerName $vmName ` | |
| -Credential $cred ` | |
| -ProvisionVMAgent ` | |
| -EnableAutoUpdate | ` | |
| Set-AzureRmVMSourceImage ` | |
| -PublisherName $publisherName ` | |
| -Offer $offer ` | |
| -Skus $sku ` | |
| -Version $version | Add-AzureRmVMNetworkInterface -Id $nic.Id | |
| } | |
| elseif ($osType -eq "linux") { | |
| # if an ssh key is provided, configure the OS to use it properly | |
| if ($sshPubKeyPath) { | |
| $sshPubKey = Get-Content $sshPubKeyPath | |
| $vmConfig = New-AzureRmVMConfig ` | |
| -VMName $vmName ` | |
| -VMSize $vmSize | ` | |
| Set-AzureRmVMOperatingSystem ` | |
| -Linux ` | |
| -ComputerName $vmName ` | |
| -Credential $cred ` | |
| -DisablePasswordAuthentication | ` | |
| Set-AzureRmVMSourceImage ` | |
| -PublisherName $publisherName ` | |
| -Offer $offer ` | |
| -Skus $sku ` | |
| -Version $version | Add-AzureRmVMNetworkInterface -Id $nic.Id | |
| Add-AzureRmVMSshPublicKey -VM $vmConfig -KeyData $sshPubKey -Path "/home/$userName/.ssh/authorized_keys" | |
| } | |
| # otherwise, simply configure as normal where the password will be configured | |
| else { | |
| $vmConfig = New-AzureRmVMConfig ` | |
| -VMName $vmName ` | |
| -VMSize $vmSize | ` | |
| Set-AzureRmVMOperatingSystem ` | |
| -Linux ` | |
| -ComputerName $vmName ` | |
| -Credential $cred | ` | |
| Set-AzureRmVMSourceImage ` | |
| -PublisherName $publisherName ` | |
| -Offer $offer ` | |
| -Skus $sku ` | |
| -Version $version | Add-AzureRmVMNetworkInterface -Id $nic.Id | |
| } | |
| } | |
| else { throw "Unknown osType: $osType" } | |
| Write-Output "Specifing Azure marketplace image we base our machine upon" | |
| $vmConfig = Set-AzureRmVMSourceImage ` | |
| -VM $vmConfig ` | |
| -PublisherName $publisherName ` | |
| -Offer $offer ` | |
| -Skus $sku ` | |
| -Version $version | |
| Write-Output "Creating OS disk and provide it a place to live" | |
| $blobPath = "vhds/$vmName.vhd" | |
| $osDiskUri = $storAcc.PrimaryEndpoints.Blob.ToString() + $blobPath | |
| $vmConfig = Set-AzureRmVMOSDisk ` | |
| -VM $vmConfig ` | |
| -Name $vmName ` | |
| -VhdUri $osDiskUri ` | |
| -CreateOption "fromImage" | |
| Write-Output "Creating VM based on vmConfig object $(get-date)" | |
| $null = New-AzureRmVM ` | |
| -ResourceGroupName $resourceGroupName ` | |
| -Location $location ` | |
| -VM $vmConfig | |
| Write-Output "VM created, $(get-date)" | |
| if (-Not ([String]::isNullOrEmpty($postDeployScriptUri))) { | |
| Write-Output "Custom Script Extension on new VM to execute bootstrap script" | |
| Set-AzureRmVMCustomScriptExtension ` | |
| -resourceGroupName $resourceGroupName ` | |
| -VMName $vmName ` | |
| -Location $location ` | |
| -FileUri $postDeployScriptUri ` | |
| -Run $postDeployScriptUri.Split('/')[-1] ` | |
| -Name $postDeployScriptUri.Split('/')[-1] | |
| } | |
| # Get pub IP | |
| $ip = (Get-AzureRmPublicIpAddress -ResourceGroupName $resourceGroupName -Name "$vmName-ip").IpAddress | |
| Write-Output ` | |
| "+++++++++++++++++++++++++" ` | |
| " " ` | |
| "Adhoc machine creation completed!" ` | |
| " " ` | |
| "Resource Group: $resourceGroupName" ` | |
| "Machine: $vmName / $ip" ` | |
| "OS: $offer, $sku" ` | |
| "Password: $password" ` | |
| " " ` | |
| "When you're ready to retire this machine and its supporting resources, run the following:" ` | |
| "Remove-AzureRmResourceGroup -Name $resourceGroupName" ` | |
| " " ` | |
| "+++++++++++++++++++++++++" |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To get a Windows 10 machine online, execute the following:
.\vm_create_adhoc_machine_cse.ps1 -publisherName 'MicrosoftVisualStudio' -offer 'windows' -sku 'Windows-10-N-x64'Azure authentication is interactive (and only a very basic check is involved)