Automating duties in Azure can save important effort and time, particularly when deploying sources like digital machines (VMs). On this weblog put up, we’ll stroll by automating the deployment of a Home windows VM in Azure utilizing PowerShell. We’ll configure networking, safety, and set up IIS for an internet server multi function script.
Why Automate VM Deployment?
Automating duties comparable to digital machine deployment ensures consistency, reduces errors, and accelerates the setup course of. With PowerShell, you possibly can outline all the mandatory parameters and let the script deal with the deployment, as an alternative of manually creating every useful resource by way of the Azure Portal.
Right here’s a step-by-step breakdown of the PowerShell script used to deploy an Azure VM, configure networking, set safety guidelines, and set up IIS.
Right here’s the script when you simply need to obtain it.
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[string]$ResourceGroupName="Admissions",
[Parameter(Mandatory)]
[string]$Location = 'East US',
[Parameter(Mandatory)]
[string]$VMName="ADMISSIONSWEB",
[Parameter(Mandatory)]
[string]$VMSize="Standard_DS3_v2",
[Parameter(Mandatory)]
[string]$PublicIPName="Admissions-PubIp",
[Parameter(Mandatory)]
[string]$NICName="Admissions-vNIC",
[Parameter(Mandatory)]
[string]$OSDiskName="Admissions-OSDisk",
[Parameter(Mandatory)]
[string]$NSGName="ADMISSIONSWEB-NSG",
[Parameter(Mandatory)]
[string]$AdminUsername="adam",
[Parameter(Mandatory)]
[SecureString]$AdminPassword = (ConvertTo-SecureString 'P@$$w0rd12' -AsPlainText -Power)
)
$ErrorActionPreference="Cease"
operate CreateAzResource {
param(
[Parameter(Mandatory)]
[string]$ResourceType,
[Parameter(Mandatory)]
[string]$Identify,
[Parameter(Mandatory)]
[hashtable]$NewParameter
)
attempt {
$useful resource = & "Get-Az$ResourceType" -Identify $Identify -ResourceGroupName $NewParameter.ResourceGroupName
} catch {
if ($_.Exception.Message -match "underneath useful resource group '.*' was not discovered") {
$useful resource = & "New-Az$ResourceType" @NewParameter
} else {
throw $_
}
} lastly {
$useful resource
}
}
#area VM Configuration
$vmconfig = New-AzVMConfig -VMName $VMName -VMSize $VMSize
#endregion
#area Public IP Creation
$newPublicIpParams = @{
Identify = $PublicIPName
ResourceGroupName = $ResourceGroupName
AllocationMethod = 'Static'
Location = $Location
}
$publicIp = CreateAzResource -ResourceType PublicIpAddress -Identify $PublicIPName -NewParameter $newPublicIpParams
#endregion
#area Community Interface Configuration
$vNet = Get-AzVirtualNetwork -ResourceGroupName $ResourceGroupName
$subnetId = $vNet.Subnets[0].Id
$newVNicParams = @{
Identify = $NICName
ResourceGroupName = $ResourceGroupName
Location = $Location
SubnetId = $subnetId
PublicIpAddressId = $publicIp.Id
}
$vNic = CreateAzResource -ResourceType NetworkInterface -Identify $NICName -NewParameter $newVNicParams
#endregion
#area OS Configuration
$cred = New-Object System.Administration.Automation.PSCredential ($AdminUsername, $AdminPassword)
$newVmOsParams = @{
Home windows = $true
ComputerName = $VMName
Credential = $cred
EnableAutoUpdate = $true
VM = $vmconfig
}
$vm = Set-AzVMOperatingSystem @newVmOsParams
#endregion
#area Picture Configuration
$newSourceImageParams = @{
PublisherName="MicrosoftWindowsServer"
Provide="WindowsServer"
Skus="2019-Datacenter"
Model = 'newest'
VM = $vm
}
$vm = Set-AzVMSourceImage @newSourceImageParams
#endregion
#area Disk Configuration
$vm = Set-AzVMOSDisk -VM $vm -Identify $OSDiskName -CreateOption FromImage
#endregion
#area Community Interface Attachment
$vm = Add-AzVMNetworkInterface -VM $vm -Id $vNic.Id
#endregion
#area NSG Configuration
$newNsgParams = @{
ResourceGroupName = $ResourceGroupName
Location = $Location
Identify = $NSGName
}
$nsg = CreateAzResource -ResourceType NetworkSecurityGroup -Identify $NSGName -NewParameter $newNsgParams
$ruleExists = $nsg.SecurityRules.Identify -contains "Permit-RDP"
if (-not $ruleExists) Set-AzNetworkSecurityGroup
#endregion
#area Internet Site visitors Rule Configuration
$webRuleExists = $nsg.SecurityRules.Identify -contains "Permit-Internet"
if (-not $webRuleExists) Set-AzNetworkSecurityGroup
#endregion
$vNic.NetworkSecurityGroup = $nsg
$vNic | Set-AzNetworkInterface
#area VM Creation
$newVmParams = @{
ResourceGroupName = $ResourceGroupName
VM = $vm
Location = $Location
}
CreateAzResource -ResourceType VM -Identify $VMName -NewParameter $newVmParams
#endregion
#area IIS Set up
$installIIS = {
Set up-WindowsFeature -Identify Internet-Server -IncludeManagementTools
}
$installScript = [scriptblock]::create($installIIS)
Invoke-AzVMRunCommand -ResourceGroupName $ResourceGroupName -VMName $VMName -CommandId 'RunPowerShellScript' -ScriptString $installScript
#endregion
#area Output Public IP
$publicIp.IpAddress
Outline Parameters
The script begins by defining parameters that can be used all through the deployment. These embody fundamental data comparable to useful resource group, VM identify, location, and admin credentials. Utilizing parameters ensures the script is versatile and might be reused for various environments.
param(
[Parameter(Mandatory)]
[string]$ResourceGroupName="Admissions",
[Parameter(Mandatory)]
[string]$Location = 'East US',
[Parameter(Mandatory)]
[string]$VMName="ADMISSIONSWEB",
[Parameter(Mandatory)]
[string]$VMSize="Standard_DS3_v2",
[Parameter(Mandatory)]
[string]$PublicIPName="Admissions-PubIp",
[Parameter(Mandatory)]
[string]$NICName="Admissions-vNIC",
[Parameter(Mandatory)]
[string]$OSDiskName="Admissions-OSDisk",
[Parameter(Mandatory)]
[string]$NSGName="ADMISSIONSWEB-NSG",
[Parameter(Mandatory)]
[string]$AdminUsername="adam",
[Parameter(Mandatory)]
[SecureString]$AdminPassword = (ConvertTo-SecureString 'P@$$w0rd12' -AsPlainText -Power)
)
Creating Azure Assets
The `CreateAzResource` operate helps create varied Azure sources like public IPs, community interfaces, and community safety teams (NSGs). It makes an attempt to get a useful resource and creates it if it doesn’t exist, making the method extra environment friendly.
operate CreateAzResource {
param(
[Parameter(Mandatory)]
[string]$ResourceType,
[Parameter(Mandatory)]
[string]$Identify,
[Parameter(Mandatory)]
[hashtable]$NewParameter
)
attempt {
$useful resource = & "Get-Az$ResourceType" -Identify $Identify -ResourceGroupName $NewParameter.ResourceGroupName
} catch {
if ($_.Exception.Message -match "not discovered") {
$useful resource = & "New-Az$ResourceType" @NewParameter
} else {
throw $_
}
} lastly {
$useful resource
}
}
VM Configuration and Networking Setup
As soon as sources like public IP and digital networks are created, the script configures a digital machine (VM) utilizing `New-AzVMConfig`, attaches a community interface, and units up the working system and disk.
$vmconfig = New-AzVMConfig -VMName $VMName -VMSize $VMSize
$vNic = CreateAzResource -ResourceType NetworkInterface -Identify $NICName -NewParameter $newVNicParams
$vm = Set-AzVMOperatingSystem @newVmOsParams
$vm = Set-AzVMSourceImage @newSourceImageParams
$vm = Set-AzVMOSDisk -VM $vm -Identify $OSDiskName -CreateOption FromImage
Configuring Community Safety Guidelines
The script provides safety guidelines to the NSG to permit inbound RDP (Distant Desktop) and internet visitors on port 80 (HTTP). That is vital for managing the VM and internet hosting internet purposes.
$ruleExists = $nsg.SecurityRules.Identify -contains "Permit-RDP"
if (-not $ruleExists) Set-AzNetworkSecurityGroup
Putting in IIS
After the VM is created, the script installs IIS (Web Info Providers), which is able to enable the VM to behave as an internet server. That is finished by executing a PowerShell command on the VM.
$installIIS = {
Set up-WindowsFeature -Identify Internet-Server -IncludeManagementTools
}
Invoke-AzVMRunCommand -ResourceGroupName $ResourceGroupName -VMName $VMName -CommandId 'RunPowerShellScript' -ScriptString $installIIS
Output the Public IP
Lastly, the general public IP of the VM is outputted for simple entry to the net server.
Conclusion
With this PowerShell script, deploying a Home windows VM on Azure with configured networking, safety, and IIS setup turns into seamless. This stage of automation is invaluable for repetitive duties and ensures that your infrastructure is ready up constantly.
By following this information, you possibly can deploy VMs rapidly and focus extra on configuring purposes reasonably than spending time manually organising sources.