У меня есть скрипт, который предполагает преобразование неуправляемых дисков 400 vms в управляемые диски.Наша инфраструктура Azure построена таким образом, что соглашения об именах почти совпадают с именами виртуальной машины или схоже с тем, что, вероятно, совпадают, например, если у меня есть vm с именем E1PrAcepyRg, он находится в группе ресурсов E1PrAcepyRg, поэтому я использую оператор ниже длясохранить имя RG внутри переменной, как показано ниже:
$VmCode = Read-Host "Partner/VM Code" (Will give a name of the VM)
$Rg = Get-AzureRmResourceGroup | Where-Object {$_.ResourceGroupName -like "*$VmCode*"} (this will store the name of the resource group)
Проблема в том, что когда я пытаюсь выполнить $ Rg, я ничего не получил, после этого, когда я запускаю цикл for, чтобы остановить все vms вRG Я получаю следующую ошибку:
Stop-AzVM : Cannot validate argument on parameter 'ResourceGroupName'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
At line:1 char:35
+ Stop-AzureRmVM -ResourceGroupName $Rg.ResourceGroupName -Name $Vm.Nam ...
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Stop-AzVM], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.Azure.Commands.Compute.StopAzureVMCommand
Весь сценарий здесь, как показано ниже -
$Global:ErrorActionPreference = 'Stop'
##########################
####GET DATA FROM USER####
##########################
$VmCode = Read-Host "Partner/VM Code"
$Rg = Get-AzureRmResourceGroup | Where {$_.ResourceGroupName -like "*$VmCode*"}
$Vms = Get-AzureRmVm | Where {$_.Name -like "*$VmCode*"}
""
""
################
####Stop VMs####
################
#Clear job queue
Get-Job | Remove-Job -Force
Write-Host "Stopping VMs to prepare for managed disk conversion..."
ForEach ($Vm in $Vms){
$Job = Stop-AzureRmVM -ResourceGroupName $Rg.ResourceGroupName -Name $Vm.Name -Force -AsJob #In this line i'm getting error not able to get Rg name.
$Job.Name = $Vm.Name
$VmName = $Vm.Name
Write-Host "...$VmName added to job queue."
}
Write-Host "Done."
""
Write-Host "Waiting for all VMs to stop..."
$VmsStopped = $False
While($VmsStopped -eq $False){
$Jobs = Get-Job
If ($Jobs -ne $null){
ForEach($Job in $Jobs){
If ($Job.State -eq "Completed"){
$JobName = $Job.Name
Write-Host "...$JobName stopped successfully."
Remove-Job $Job
}
ElseIf ($Job.State -eq "Failed"){
$JobName = $Job.Name
Write-Host "...failed to stop $JobName." -ForegroundColor Red
Receive-Job $Job
Remove-Job $Job
}
}
}
ElseIf ($Jobs -eq $null){
$VmsStopped = $True
}
}
Write-Host "Done."
""
$ContinueResponse = Read-Host "Continue with managed disk conversion (Yes/No)"
""
If ($ContinueResponse -like "Yes" -or $ContinueResponse -like "y"){
####################################
####Convert VMs to managed disks####
####################################
#Clear job queue
Get-Job | Remove-Job -Force
Write-Host "Converting VMs to managed disks..."
#Wait for VM status to update as stopped/deallocated
ForEach ($Vm in $Vms){
$VmRunning = $Null
While ($VmRunning -ne $False){
$Detail = Get-AzureRmVm -Status | Where {$_.Name -like "*$($Vm.Name)*"}
$VmStatus = $Detail.PowerState
If($VmStatus -like "*deallocated*"){
$VmRunning = $False
}
Else{
$VmRunning = $True
}
}
$Job = ConvertTo-AzureRmVMManagedDisk -ResourceGroupName $Rg.ResourceGroupName -Name $Vm.Name -AsJob
$Job.Name = $Vm.Name
$VmName = $Vm.Name
Write-Host "...$VmName added to job queue."
}
Write-Host "Done."
""
Write-Host "Waiting for managed disk conversion to finish..."
$VmsConverted = $False
While($VmsConverted -eq $False){
$Jobs = Get-Job
If ($Jobs -ne $null){
ForEach($Job in $Jobs){
If ($Job.State -eq "Completed"){
$JobName = $Job.Name
Write-Host "...$JobName converted successfully to managed disks."
Remove-Job $Job
}
ElseIf ($Job.State -eq "Failed"){
$JobName = $Job.Name
Write-Host "...$JobName failed managed disk conversion." -ForegroundColor Red
Receive-Job $Job
Remove-Job $Job
}
}
}
ElseIf ($Jobs -eq $null){
$VmsConverted = $True
}
}
Write-Host "Done."
""
#################
####Start VMs####
#################
#Clear job queue
Get-Job | Remove-Job -Force
Write-Host "Starting VMs..."
ForEach ($Vm in $Vms){
$Job = Start-AzureRmVM -ResourceGroupName $Rg.ResourceGroupName -Name $Vm.Name -AsJob
$Job.Name = $Vm.Name
$VmName = $Vm.Name
Write-Host "...$VmName added to job queue."
}
Write-Host "Done."
""
Write-Host "Waiting for VMs to start..."
$VmsStarted = $False
While($VmsStarted -eq $False){
$Jobs = Get-Job
If ($Jobs -ne $null){
ForEach($Job in $Jobs){
If ($Job.State -eq "Completed"){
$JobName = $Job.Name
Write-Host "...$JobName converted successfully to managed disks."
Remove-Job $Job
}
ElseIf ($Job.State -eq "Failed"){
$JobName = $Job.Name
Write-Host "...$JobName failed managed disk conversion." -ForegroundColor Red
Receive-Job $Job
Remove-Job $Job
}
}
}
ElseIf ($Jobs -eq $null){
$VmsStarted = $True
}
}
Write-Host "Done."
""
}
Else{
Write-Host "VMs will not be converted to managed disks. Existing script..."
Exit
}
Я не уверен, что я делаю неправильно, может кто-нибудь помочь мне с этим?Заранее спасибо