This article has not been completed yet. However, it may already contain helpful Information and therefore it has been published at this stage.
First create an Azure Automation account in the region of your choice.
Now that we have our Azure Automation account, its time to set up the System Managed Identity and grant it the following roles:
- Virtual Machine Contributor (to deallocate the Virtual Machine)
After the successful creation and configuration of the automation account, go to the specific VM that the user will use and add a tag. For example, specify whether you want to control the VM externally.
Now create the scripts for the VM start and the VM stop actions.
Start VM:
try
{
"Logging in to Azure..."
Connect-AzAccount -Identity
}
catch {
Write-Error -Message $_.Exception
throw $_.Exception
}
$TagName = "Remote Control"
$TagValue = "true"
$VMs = Get-AzResource -TagName $TagName -TagValue $TagValue | Where-Object -FilterScript {$_.ResourceType -like 'Microsoft.Compute/virtualMachines'}
$VMs
foreach ($VM in $VMs) {
Start-AzVM -ResourceGroupName $VM.ResourceGroupName -Name $VM.Name -Verbose
}
Stop VM:
try
{
"Logging in to Azure..."
Connect-AzAccount -Identity
}
catch {
Write-Error -Message $_.Exception
throw $_.Exception
}
$TagName = "Remote Control"
$TagValue = "true"
$VMs = Get-AzResource -TagName $TagName -TagValue $TagValue | Where-Object -FilterScript {$_.ResourceType -like 'Microsoft.Compute/virtualMachines'}
foreach ($VM in $VMs) {
Stop-AzVM -ResourceGroupName $VM.ResourceGroupName -Name $VM.Name -Force -Verbose
}
Next step is to upload the scripts into the Automation account to create the runbooks.
Set an appropriate expiration date and make sure you have copied the webhook URL. You will not have access to the URL after you hit OK.
Follow the same procedure for the Stop VM script.
Finally, you can create the PowerShell scripts (one to start and one to stop the VM) for the user to call the webhooks with them.
Invoke-WebRequest -Method Post -Uri <webhookurl>