【问题标题】:Azure PowerShell - VM Tagging ReportAzure PowerShell - 虚拟机标记报告
【发布时间】:2021-05-26 01:45:37
【问题描述】:

我有兴趣查询 Azure 订阅以提供标记报告,该报告指示 VM 名称、Azure 区域以及每个 VM 的标记和标记值。

我当前的脚本为我提供了除每个标签的标签值之外的所有数据。我的输出显示的结果好像标签不存在。

有人对我的剧本有什么建议吗?谢谢!

参数( [参数(强制 = $true)] [字符串] $subscriptionName )

Select-AzSubscription -Subscription $subscriptionName

$allTags = 获取-AzTag

$allVMs = Get-AzVM

$vmInformation = @() foreach ($vm in $allVMs) {

$vmInformationObject = New-Object PSObject

$vmName = $vm.Name
$vmRegion = $vm.Location
$vmInformationObject | Add-Member -MemberType NoteProperty -Name "VM_Name" -Value $vmName
$vmInformationObject | Add-Member -MemberType NoteProperty -Name "VM_Region" -Value $vmRegion

$vm_tags = $vm.tags
foreach ($tag in $allTags) {
    $IfTagExists = $false
    foreach ($vmtag in $vm_tags) {
        if ($tag.name -like $vmtag.keys) {
            $IfTagExists = $true
            $vmInformationObject | Add-Member -MemberType NoteProperty -Name $tag.Name -Value $vmtag.$($tag.Name)
            break
        }
    }
    if ($IfTagExists -eq $false) {
        $vmInformationObject | Add-Member -MemberType NoteProperty -Name $tag.Name -Value "--"
    }
}
$vmInformation += $vmInformationObject }

$vm信息 |导出-Csv "path.csv" -NoTypeInformation -Force

【问题讨论】:

  • $tag.name -like $vmtag.keys 应该使用不同的运算符 --> $tag.name -in $vmtag.keys 作为示例。您正在将单个值与值列表进行比较。因此,您需要一个比较列表中项目的运算符。
  • 感谢您的帮助和建议。我修改了代码,结果在我的输出中填充了标记值。非常感谢!

标签: azure powershell tags virtual-machine tagging


【解决方案1】:

这里的问题是比较语句$tag.name -like $vmtag.keys。由于左侧值是标量而右侧值是集合或列表,因此您需要使用包含comparison operator。最简单的选项是-in-contains

# Option 1
# Use -in when singular item is on the LHS and collection is on RHS
if ($tag.name -in $vmtag.keys) {
            $IfTagExists = $true
            $vmInformationObject | Add-Member -MemberType NoteProperty -Name $tag.Name -Value $vmtag.$($tag.Name)
            break
}

# Option 2
# Use -contains when singular item is on the RHS and collection is on LHS
if ($vmtag.keys -contains $tag.name) {
            $IfTagExists = $true
            $vmInformationObject | Add-Member -MemberType NoteProperty -Name $tag.Name -Value $vmtag.$($tag.Name)
            break
}

【讨论】:

  • 谢谢!修改运营商解决了这个问题。我使用了您示例中的“选项 1”并进行了此更改: ($tag.name -in $vmtag.keys)
【解决方案2】:

我参加聚会有点晚了,我想提一下,如果您尝试将结果导出到 CSV 文件,您会遇到问题。根据 Microsoft Export-CSV 根据您提交的第一个对象的属性组织文件。如果其余对象不具有指定属性之一,则该对象的属性值为 null,由两个连续的逗号表示。如果其余对象具有其他属性,则这些属性值不会包含在文件中

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/export-csv?view=powershell-7.1

您的脚本也会运行得更慢,因为您使用的是变量 $allTags = Get-AzTag,而不是像变量 $ 中所见的那样仅过滤所有 VM 中的通用标签AllVMsTagKeys 在下面的脚本中,它将以更快、更简洁的方式执行您想要的所有操作,并将结果导出到桌面上的 CSV 文件:

$VMs = Get-AzVM -Status
$AllVMsTagKeys = $VMs.Tags.Keys | Sort-Object -Unique

  foreach ( $VM in $VMs ) {

    $Output = [PSCustomObject]@{
      VMName = $VM.Name
      VMRegion = $VM.Location
      ResourceGroup = $VM.ResourceGroupName
      PowerState = $VM.PowerState
    }    

    foreach ( $VMTagKey in $AllVMsTagKeys ) {

      Add-Member -InputObject $Output -Force -NotePropertyName $VMTagKey -NotePropertyValue $VM.Tags[$VMTagKey]

    }

    $Output
    $Output | Export-Csv -NoTypeInformation -Append -Path $env:USERPROFILE\Desktop\VMsWithTags.csv

  }

【讨论】:

    猜你喜欢
    • 2019-02-06
    • 1970-01-01
    • 2015-09-04
    • 2017-01-02
    • 1970-01-01
    • 2019-08-30
    • 2017-01-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多