Public/JumpBox.ps1
function New-JumpBox { [CmdletBinding()] param( [string]$Username, [System.Management.Automation.PSCredential]$cred, [bool]$Linux, [Parameter(Mandatory, ValueFromPipeline)][pscustomobject]$Payload ) $Name = $Payload.Name $RGName = $Payload.RGName $Location = $Payload.Location $NameSuffix = $Payload.NameSuffix $NICName = "nic-$NameSuffix" # A virtual machine requires a network interface client (NIC). A NIC requires # a virtual network and subnet. Therefore we must provision these downstream # components first, then provision the NIC, after which we can provision the VM. # get the subnet inside the vnet where we want to host our VM $vnet = Get-AzVirtualNetwork -ResourceGroupName $RGName -Name $Payload.VnetName $JumpBoxSubnetId = $vnet.Subnets[1].Id # Create a virtual network card and associate with jumpbox subnet $JumpBoxNic = New-AzNetworkInterface -Name $NICName -ResourceGroupName $RGName -Location $Location -SubnetId $JumpBoxSubnetId # shorten the vmname if it is too long if ( $Name.length -gt 10 ) { $NameArray = $NameSuffix -split "-" $VMName = "vm" + $NameArray[0].Substring(0,4) + $NameArray[2] } else { $NameArray = $NameSuffix -split "-" $VMName = "vm" + $NameArray[0] + $NameArray[2] } # Validate virtual machine name follows naming conventions Test-UserInput -FCageName $VMName # Configure the jump box if ($Linux){ #if Linux flag is present, configure a Linux virtual machine $JumpBoxConfig = New-AzVMConfig -VMName $VMName -VMSize Standard_DS1_v2 | Set-AzVMOperatingSystem -Linux -ComputerName $VMName -Credential $cred | Set-AzVMSourceImage -PublisherName "Canonical" -Offer "UbuntuServer" -Skus "18.04-LTS" -Version latest | Add-AzVMNetworkInterface -Id $JumpBoxNic.Id } else { # else, default to a Windows virtual machine $JumpBoxConfig = New-AzVMConfig -VMName $VMName -VMSize Standard_DS1_v2 | Set-AzVMOperatingSystem -Windows -ComputerName $VMName -Credential $cred | Set-AzVMSourceImage -PublisherName "MicrosoftWindowsServer" -Offer "WindowsServer" -Skus "2016-Datacenter" -Version latest | Add-AzVMNetworkInterface -Id $JumpBoxNic.Id } New-AzVM -ResourceGroupName $RgName -Location $Location -VM $JumpBoxConfig # Create a payload that can be captured and printed out for the user $Payload | Add-Member -MemberType NoteProperty -Name 'VMName' -Value $VMName $Payload | Add-Member -MemberType NoteProperty -Name 'Username' -Value $Username $Payload | Add-Member -MemberType NoteProperty -Name 'NICName' -Value $NICName $Payload | Add-Member -MemberType NoteProperty -Name 'Linux' -Value $Linux [pscustomobject]$Payload } |