使用 PowerShell 列出所有带有标记名和标记值的 azure 资源
List all azure resources with tagnames and tagvalues using PowerShell
我正在尝试获取所有 Azure VirtualMachines 和 Azure SqlDatabases 的 OperatingHours 标记详细信息。
以下是资源中 appID 的可能性以及我需要在输出中相应打印的值:
- 如果 OperatingHours 标签本身不存在于任何资源中,则显示 "Tag not present"
- 如果存在 OperatingHours 标签但包含 null 或空字符串,则显示 "NULL/EMPTY"
- 如果 OperatingHours 标签与任何其他值一起出现,则显示该值。
我需要单独处理选项 (2) 还是打印 OperatingHours 的任何正常值。
经过长时间的努力,我创建了以下脚本:
$ErrorOccured = $false
$resources = Get-AzureRmResource |
Where-Object {($_.ResourceType -eq "Microsoft.Compute/virtualMachines") -or ($_.ResourceType -eq "Microsoft.Sql/servers/databases")} |
foreach {
new-object psobject -Property @{
ResourceName = $_.ResourceName;
ResourceType = $_.ResourceType;
OperatingHours= try {
$ErrorActionPreference = 'SilentlyContinue';
($_ | select -expand Tags).OperatingHours; }
catch {
$ErrorOccured = $true ; }
if ($ErrorOccured)
{ "Tag not present" }
else {
($_ | select -expand Tags).OperatingHours;
$ErrorOccured = $false };}
}
$resources | Format-Table
当 运行 这个脚本时,我收到以下错误:
At line:13 char:58
+ }
+ ~
The hash literal was incomplete.
At line:20 char:2
+ }
+ ~
Unexpected token '}' in expression or statement.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : IncompleteHashLiteral
如果我用以下代码替换 OperatingHours 语句,则脚本 运行 成功。但是这样做,我无法满足上述选项(1)。
Operating Hours = if (!($_ | select -expand Tags).OperatingHours)
{"Tag not present"}
else {($_ | select -expand Tags).OperatingHours} ;
请告诉我如何更正此问题并获得所需的输出。
谢谢
经过大量的尝试和严格的测试,我找到了我要找的东西:
OperatingHours = if ( ($_ | select -expand Tags).OperatingHours -ieq $null )
{"TAG NOT PRESENT"}
elseif ( ($_ | select -expand Tags).OperatingHours -ieq '')
{"NULL/EMPTY"}
else
{($_ | select -expand Tags).OperatingHours } ;
我把原来脚本中的 OperatingHours 语句替换成上面的。
解决方案看起来很简单,找到它后我想我怎么会早点错过它,但这就是学习过程的全部内容,对吧?
我正在尝试获取所有 Azure VirtualMachines 和 Azure SqlDatabases 的 OperatingHours 标记详细信息。 以下是资源中 appID 的可能性以及我需要在输出中相应打印的值:
- 如果 OperatingHours 标签本身不存在于任何资源中,则显示 "Tag not present"
- 如果存在 OperatingHours 标签但包含 null 或空字符串,则显示 "NULL/EMPTY"
- 如果 OperatingHours 标签与任何其他值一起出现,则显示该值。
我需要单独处理选项 (2) 还是打印 OperatingHours 的任何正常值。
经过长时间的努力,我创建了以下脚本:
$ErrorOccured = $false
$resources = Get-AzureRmResource |
Where-Object {($_.ResourceType -eq "Microsoft.Compute/virtualMachines") -or ($_.ResourceType -eq "Microsoft.Sql/servers/databases")} |
foreach {
new-object psobject -Property @{
ResourceName = $_.ResourceName;
ResourceType = $_.ResourceType;
OperatingHours= try {
$ErrorActionPreference = 'SilentlyContinue';
($_ | select -expand Tags).OperatingHours; }
catch {
$ErrorOccured = $true ; }
if ($ErrorOccured)
{ "Tag not present" }
else {
($_ | select -expand Tags).OperatingHours;
$ErrorOccured = $false };}
}
$resources | Format-Table
当 运行 这个脚本时,我收到以下错误:
At line:13 char:58
+ }
+ ~
The hash literal was incomplete.
At line:20 char:2
+ }
+ ~
Unexpected token '}' in expression or statement.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : IncompleteHashLiteral
如果我用以下代码替换 OperatingHours 语句,则脚本 运行 成功。但是这样做,我无法满足上述选项(1)。
Operating Hours = if (!($_ | select -expand Tags).OperatingHours)
{"Tag not present"}
else {($_ | select -expand Tags).OperatingHours} ;
请告诉我如何更正此问题并获得所需的输出。
谢谢
经过大量的尝试和严格的测试,我找到了我要找的东西:
OperatingHours = if ( ($_ | select -expand Tags).OperatingHours -ieq $null )
{"TAG NOT PRESENT"}
elseif ( ($_ | select -expand Tags).OperatingHours -ieq '')
{"NULL/EMPTY"}
else
{($_ | select -expand Tags).OperatingHours } ;
我把原来脚本中的 OperatingHours 语句替换成上面的。
解决方案看起来很简单,找到它后我想我怎么会早点错过它,但这就是学习过程的全部内容,对吧?