Azure 如何将哈希表查询结果输出到OutGridView?

Azure 如何将哈希表查询结果输出到OutGridView?,azure,powershell,Azure,Powershell,我希望使用Powershell将哈希表结果导出到Out GridView中 下面脚本的目的是将Azure VM标记导出到Out GridView,它会抛出类似以下空白结果的错误: 控制台上的错误: Out-GridView : Syntax error in PropertyPath 'Syntax error in Binding.Path '[ Product] ' ... '(Tag)'.'. At line:46 char:19 + $Output | Out-Grid

我希望使用Powershell将哈希表结果导出到Out GridView中

下面脚本的目的是将Azure VM标记导出到Out GridView,它会抛出类似以下空白结果的错误:

控制台上的错误:

Out-GridView : Syntax error in PropertyPath 'Syntax error in Binding.Path '[ Product] ' ... '(Tag)'.'.
At line:46 char:19
+         $Output | Out-GridView #Export-Csv -Path c:\temp\1a.csv -appe ...
+                   ~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [Out-GridView], InvalidOperationException
    + FullyQualifiedErrorId : ManagementListInvocationException,Microsoft.PowerShell.Commands.OutGridViewCommand
这是在全局管理员角色下执行的实际脚本:

<#
.AUTHOR: https://stackoverflow.com/users/13390556/lukasz-g
#>
$Subscription = Get-AzSubscription | Out-GridView -Title 'Select subscription' -OutputMode 'Multiple'

# Initialise output array
$Output = @()

if ($Subscription) {
    foreach ($item in $Subscription) {
        $item | Select-AzSubscription

        # Collect all the resources or resource groups (comment one of below)
        $Resource = Get-AzResource
        #$Resource = Get-AzResourceGroup

        # Obtain a unique list of tags for these groups collectively
        $UniqueTags = $Resource.Tags.GetEnumerator().Keys | Get-Unique -AsString | Sort-Object | Select-Object -Unique | Where-Object { $_ -notlike "hidden-*" }

        # Loop through the resource groups
        foreach ($ResourceGroup in $Resource) {
            # Create a new ordered hashtable and add the normal properties first.
            $RGHashtable = New-Object System.Collections.Specialized.OrderedDictionary
            $RGHashtable.Add("Name", $ResourceGroup.ResourceGroupName)
            $RGHashtable.Add("Location", $ResourceGroup.Location)
            $RGHashtable.Add("Id", $ResourceGroup.ResourceId)
            $RGHashtable.Add("ResourceType", $ResourceGroup.ResourceType)

            # Loop through possible tags adding the property if there is one, adding it with a hyphen as it's value if it doesn't.
            if ($ResourceGroup.Tags.Count -ne 0) {
                $UniqueTags | Foreach-Object {
                    if ($ResourceGroup.Tags[$_]) {
                        $RGHashtable.Add("[$_] (Tag)", $ResourceGroup.Tags[$_])
                    }
                    else {
                        $RGHashtable.Add("[$_] (Tag)", "-")
                    }
                }
            }
            else {
                $UniqueTags | Foreach-Object { $RGHashtable.Add("[$_] (Tag)", "-") }
            }

            # Update the output array, adding the ordered hashtable we have created for the ResourceGroup details.
            $Output += New-Object psobject -Property $RGHashtable
        }

        # Sent the final output to CSV
        $Output | Out-GridView #Export-Csv -Path c:\temp\1a.csv -append -NoClobber -NoTypeInformation -Encoding UTF8 -Force
    }
}

$Subscription=Get-AzSubscription | Out-GridView-标题“选择订阅”-输出模式“多”
#初始化输出阵列
$Output=@()
if(订阅费){
foreach($Subscription中的项目){
$item |选择订阅
#收集所有资源或资源组(评论以下内容之一)
$Resource=获取资源
#$Resource=获取资源组
#获取这些组的唯一标记列表
$UniqueTags=$Resource.Tags.GetEnumerator().Keys | Get Unique-AsString | Sort Object | Select Object-Unique |其中Object{$|-notlike“hidden-*”}
#循环浏览资源组
foreach($Resource中的ResourceGroup){
#创建一个新的有序哈希表,并首先添加普通属性。
$RGHashtable=新对象System.Collections.Specialized.OrderedDictionary
$RGHashtable.Add(“名称”,$ResourceGroup.ResourceGroupName)
$RGHashtable.Add(“位置”,$ResourceGroup.Location)
$RGHashtable.Add(“Id”,$ResourceGroup.ResourceId)
$RGHashtable.Add(“ResourceType”,$ResourceGroup.ResourceType)
#通过可能的标记循环添加属性(如果有),如果没有,则使用连字符作为其值添加属性。
如果($ResourceGroup.Tags.Count-ne 0){
$UniqueTags | Foreach对象{
if($ResourceGroup.Tags[$\uz]){
$RGHashtable.Add(“[$\](标记)”,$ResourceGroup.Tags[$\])
}
否则{
$RGHashtable.Add(“[$”(标记)”,“-”)
}
}
}
否则{
$UniqueTags | Foreach对象{$RGHashtable.Add(“[$”(标记)”,“-”}
}
#更新输出数组,添加我们为ResourceGroup详细信息创建的有序哈希表。
$Output+=新对象psobject-属性$RGHashtable
}
#将最终输出发送到CSV
$Output | Out GridView#Export Csv-Path c:\temp\1a.Csv-append-NoClobber-NoTypeInformation-Encoding UTF8-Force
}
}
在上面的代码中,您试图添加如下内容:

在输出中

删除了所有内容,我用简单的语句进行了测试

$Output = @()
$RGHashtable = New-Object System.Collections.Specialized.OrderedDictionary
$RGHashtable.Add("[Testing] (Name)", "Temporary")
$Output += New-Object psobject -Property $RGHashtable
$Output | Out-GridView
我也遇到了同样的错误

经过两次测试后,了解到只有当字符串中存在组合“
[SomeString](SomeString)
”--
[…](…)
时才会发生错误

Out GridView正在尝试分析“
[]()
”并因此产生错误

您可以在代码中尝试以下任意一种组合:

$RGHashtable.Add("[$_] [Tag]", $ResourceGroup.Tags[$_])
 

这将解决您的问题


<>你的代码中有3个实例,如果我没有错的话。

@ SeiRoSoeStaseStudio——如果有帮助的话,请考虑把它标记为解决方案。或者,如果你有任何疑问请告诉我。
$RGHashtable.Add("[$_] [Tag]", $ResourceGroup.Tags[$_])
 
$RGHashtable.Add("{$_} (Tag)", $ResourceGroup.Tags[$_])
$RGHashtable.Add("[$_] [Tag]", $ResourceGroup.Tags[$_])