azure - 如何使用 PowerShell 获取禁用自动关机的 azure 服务器列表?
问题描述
我想获取禁用了自动关闭的 azure 服务器列表,我有以下脚本,但脚本的问题是它获取订阅 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 的输出以以下格式显示,
我想要的很简单,我希望虚拟机名称及其自动关闭状态显示在屏幕上,以便我轻松找出所有虚拟机当前已禁用自动关闭。
与此相关的任何帮助都会有所帮助。
解决方案
您只需要microsoft.devtestlab/schedules
使用以下方法获取资源 ID:
/subscriptions/{subscriptionId}/resourceGroups/{rgName}/providers/microsoft.devtestlab/schedules/shutdown-computevm-{vmName}
然后使用 遍历所有 VM Get-AzVM
,使用 获取microsoft.devtestlab/schedules
资源Get-AzResource
,然后使用 将 VM 名称和状态输出到表中Format-Table
。
$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
模块,因为对 2020 年 12 月的支持AzureRm
将结束。您可以阅读文档以获取有关此的更多信息。
上面的代码应该给你一个类似于下面的输出
VMName ShutdownStatus
------ --------------
vm1 Enabled
vm2 Disabled
更新
调用运算符 &在这里用于将 for 循环作为脚本块运行。您可以在 中阅读有关此内容的更多信息about_Script_Blocks
。
推荐阅读
- hive - 将数据从具有不同分隔符的文件加载到配置单元表
- amazon-s3 - 如何将 EBS 上 Rails 应用程序上的入站安全组限制为仅来自我的应用程序(在 S3 上)并防止其他服务器访问我的 API?
- oauth-2.0 - 使用 Microsoft Graph 创建日历事件:ErrorAccessDenied
- r - 以 polylineDecorator 为例的传单插件和传单代理
- python - 如何根据 OpenCV、Python 中的图像大小调整 cv2.putText 的文本大小?
- java - 为什么 Spring Metrics 显示初始堆大小大于最大大小?
- python-3.x - Pandas - 加入重复的行
- c - 使用 txt 文件填充循环双链表
- javascript - 如何在javascript中显示来自服务器的图像?
- linux - 无法从已安装的驱动器克隆存储库