问题描述
我想获取禁用了自动关闭功能的azure服务器的列表,我有以下脚本,但是脚本的问题是它在Subscription GUID下获取了RG的列表,但在每次循环后重复输出
Import-AzureRmContext -Path "$PSScriptRoot\AzureProfile.json"
Select-AzureRmSubscription -SubscriptionId {subscriptionId}
[array]$ResourceGroupArray = Get-AzureRMVm | Select-Object -Property ResourceGroupName,Name,VmId
foreach ($resourceGroup in $ResourceGroupArray){
$targetResourceId = (Get-AzureRmVM -ResourceGroupName $resourcegroup.ResourceGroupName -Name $resourceGroup.Name).Id
$shutdowninformation = (Get-AzureRmResource -ResourceGroupName $resourcegroup.ResourceGroupName -ResourceType Microsoft.DevTestLab/schedules -Expandproperties).Properties
Write-Host "ID: " $targetResourceId
$shutdowninformation
我想要的很简单,我希望在屏幕上显示VM名称及其自动关闭状态,以便我轻松找出哪些VM当前已禁用自动关闭。
任何与此相关的帮助都会有所帮助。
解决方法
您只需要使用以下方法获取microsoft.devtestlab/schedules
资源ID:
/subscriptions/{subscriptionId}/resourceGroups/{rgName}/providers/microsoft.devtestlab/schedules/shutdown-computevm-{vmName}
然后使用Get-AzVM
遍历所有VM,使用Get-AzResource
获取microsoft.devtestlab/schedules
资源,然后使用Format-Table
将VM名称和状态输出到表中。
$subscriptionId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
Set-AzContext -SubscriptionId $subscriptionId
& {
foreach ($vm in Get-AzVM) {
try {
$shutdownResource = Get-AzResource `
-ResourceId "/subscriptions/$subscriptionId/resourceGroups/$($vm.ResourceGroupName)/providers/microsoft.devtestlab/schedules/shutdown-computevm-$($vm.Name)" `
-ErrorAction Stop
[PSCustomObject]@{
VMName = $vm.Name
ShutdownStatus = $shutdownResource.Properties.status
}
}
catch {
[PSCustomObject]@{
VMName = $vm.Name
ShutdownStatus = $_.Exception.Message
}
}
}
} | Format-Table -AutoSize
要将上下文设置为正确的订阅,我们可以使用Set-AzContext
。
但是,以上使用的是最新的Az
模块。您可以使用等效的AzureRm
模块来做同样的事情。
$subscriptionId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
Set-AzureRmContext -SubscriptionId $subscriptionId
& {
foreach ($vm in Get-AzureRmVM) {
try {
$shutdownResource = Get-AzureRmResource `
-ResourceId "/subscriptions/$subscriptionId/resourceGroups/$($vm.ResourceGroupName)/providers/microsoft.devtestlab/schedules/shutdown-computevm-$($vm.Name)" `
-ErrorAction Stop
[PSCustomObject]@{
VMName = $vm.Name
ShutdownStatus = $shutdownResource.Properties.status
}
}
catch {
[PSCustomObject]@{
VMName = $vm.Name
ShutdownStatus = $_.Exception.Message
}
}
}
} | Format-Table -AutoSize
尽管我确实建议移至Az
模块,因为对AzureRm
的支持将在2020年12月结束。您可以阅读documentation以获得更多信息。
上面的代码应为您提供类似于以下内容的输出
VMName ShutdownStatus
------ --------------
vm1 Enabled
vm2 Disabled
更新
此处使用Call operator &作为脚本块运行for循环。您可以在about_Script_Blocks
中了解有关此内容的更多信息。
尝试类似的方法以获取所有VM的自动关闭状态。不必尝试获取循环内的调度,而是获取预订中的所有调度并根据VM的完整资源ID进行匹配。
[array]$VMArray = Get-AzureRMVm | Select-Object -Property ResourceGroupName,Name,VmId,Id
$ShutdownInformation = (Get-AzureRmResource -ResourceType Microsoft.DevTestLab/schedules -Expandproperties).Properties
foreach($vm in $VMArray) {
$ShutdownStatus = "Not Configured"
$Schedule = $ShutdownInformation | Where-Object { $_.targetResourceId -eq $vm.Id } | Select -First 1
if($Schedule -ne $null) {
$ShutdownStatus = $Schedule.status
}
Write-Host $vm.VmId $ShutdownStatus
}