我有一个脚本,该脚本可以将400 vms的非托管磁盘转换为托管磁盘。我们的azure基础架构的构建方式使命名约定几乎与虚拟机的名称匹配或相同,例如,如果我有一个名为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循环以停止RG中的所有vm时,我得到以下错误:
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
}
我不确定我在做什么错,有人可以帮助我吗? 在此先感谢