У меня есть скрипт, который автоматизирует изменение размера диска управляемой ОС в Azure. В настоящее время мне нужно жестко указать имя виртуальной машины в двух местах ниже, помеченных как «VM NAME HERE», чтобы она работала.
Я пытаюсь выяснить, как я могу взять ввод для $ vm и использовать его для сопоставления с регулярным выражением вместо жесткого кодирования имен.
Возможно ли это сделать?
# Set the required variables with user input
$vm = Read-Host -Prompt 'Input the name of the target VM'
$rg = Read-Host -Prompt 'Input the name of your Resource Group'
$size = Read-Host -Prompt 'How many GB should the disk be changed to'
$new_snap = Read-Host -Prompt 'Please name the backup snapshot for your OS Disk'
# Locate the OS Disk information based on the name of the VM
$disk_id = (az vm list --query "[].{ name:name, os:storageProfile.osDisk.managedDisk.id }")
$os_disk = $disk_id | Select-String -Pattern '("VM NAME HERE"[_\w\d]+)' | ForEach-Object {
$_.Matches.Groups[1].Value }
$snap = $disk_id | Select-String -Pattern '([\.\/\-\w\d]+"VM NAME HERE"[_\w\d]+)' | ForEach-Object {
$_.Matches.Groups[1].Value }
# Create Snapshot
Write-Output "Creating Snapshot: $new_snap"
az snapshot create --resource-group $rg --name $new_snap --source $snap
# Deallocate Target VM
Write-Output "Deallocating VM: $vm"
az vm deallocate --resource-group $rg --name $vm
# Resize Managed Disk
Write-Output "Updating OS Disk size to $size GB"
az disk update --name $os_disk --resource-group $rg --size-gb $size
# Restart Target VM
Write-Output "Restarting VM: $vm"
az vm start --resource-group $rg --name $vm