遍历 azure 对象并在标签上过滤它们
loop through azure objects and filter them on tags
我有一个问题,最近我们开始在 Azure 中使用标记,需要通过标记列出特定对象。下面是用于查找资源的脚本。在这两种情况下,当使用 match 和 notmatch 时,我们都会在结果中找到一个特定的对象。这是搜索标记资源时的奇怪行为。您还使用哪些其他方式来完成任务?
PS C:\WINDOWS\system32> $KeyName = 'Department'
PS C:\WINDOWS\system32> $NewKeyValue = "PROD, Data"
PS C:\WINDOWS\system32> $AzSqlServer = Get-AzSqlServer
PS C:\WINDOWS\system32> if($AzSqlServer)
>> {
>> foreach ($server in $AzSqlServer )
>> {
>> $SQLDatabase = Get-AzSqlDatabase -ServerName $server.ServerName -ResourceGroupName $server.ResourceGroupName| Where-Object {$_.tags.Values -notmatch "PROD, Data"}
>> write-output $SQLDatabase.DatabaseName
>> }
>> }
DBname1
DBname2
DBname3
DBname4
master
master
master
**DBname5**
PS C:\WINDOWS\system32>
PS C:\WINDOWS\system32> $KeyName = 'Department'
PS C:\WINDOWS\system32> $NewKeyValue = "PROD, Data"
PS C:\WINDOWS\system32> $AzSqlServer = Get-AzSqlServer
PS C:\WINDOWS\system32> if($AzSqlServer)
>> {
>> foreach ($server in $AzSqlServer )
>> {
>> $SQLDatabase = Get-AzSqlDatabase -ServerName $server.ServerName -ResourceGroupName $server.ResourceGroupName| Where-Object {$_.tags.Values -match "PROD, Data"}
>> write-output $SQLDatabase.DatabaseName
>> }
>> }
**DBname5**
这里的问题是对列表使用 -notmatch
和 -match
运算符会产生意外结果。切换到 -notcontains
和 -contains
以获得完全匹配将产生所需的结果。
$AzSqlServer = Get-AzSqlServer
if ($AzSqlServer) {
foreach ($server in $AzSqlServer) {
$SQLDatabase = Get-AzSqlDatabase -ServerName $server.ServerName -ResourceGroupName $server.ResourceGroupName |
Where-Object {$_.tags.Values -notcontains "PROD, Data"}
$SQLDatabase.Databasename
}
}
使用 -notcontains
时,将对列表的项目进行整体比较。如果没有与目标项目匹配的项目,该列表将仅 return true
。当对列表使用 -notmatch
时,列表中与目标项不匹配的项将被 returned。任何 returned 项目都会导致 True
导致 boolean
语句。以下是您的体验的简化示例:
# Notice how both boolean statements return True
PS> 1,2,3 -notmatch 2
1
3
PS> [bool](1,2,3 -notmatch 2)
True
PS> 1,2,3 -match 2
2
PS> [bool](1,2,3 -match 2)
True
# Now using -notcontains and -contains
PS> 1,2,3 -notcontains 2
False
PS> 1,2,3 -contains 2
True
我有一个问题,最近我们开始在 Azure 中使用标记,需要通过标记列出特定对象。下面是用于查找资源的脚本。在这两种情况下,当使用 match 和 notmatch 时,我们都会在结果中找到一个特定的对象。这是搜索标记资源时的奇怪行为。您还使用哪些其他方式来完成任务?
PS C:\WINDOWS\system32> $KeyName = 'Department'
PS C:\WINDOWS\system32> $NewKeyValue = "PROD, Data"
PS C:\WINDOWS\system32> $AzSqlServer = Get-AzSqlServer
PS C:\WINDOWS\system32> if($AzSqlServer)
>> {
>> foreach ($server in $AzSqlServer )
>> {
>> $SQLDatabase = Get-AzSqlDatabase -ServerName $server.ServerName -ResourceGroupName $server.ResourceGroupName| Where-Object {$_.tags.Values -notmatch "PROD, Data"}
>> write-output $SQLDatabase.DatabaseName
>> }
>> }
DBname1
DBname2
DBname3
DBname4
master
master
master
**DBname5**
PS C:\WINDOWS\system32>
PS C:\WINDOWS\system32> $KeyName = 'Department'
PS C:\WINDOWS\system32> $NewKeyValue = "PROD, Data"
PS C:\WINDOWS\system32> $AzSqlServer = Get-AzSqlServer
PS C:\WINDOWS\system32> if($AzSqlServer)
>> {
>> foreach ($server in $AzSqlServer )
>> {
>> $SQLDatabase = Get-AzSqlDatabase -ServerName $server.ServerName -ResourceGroupName $server.ResourceGroupName| Where-Object {$_.tags.Values -match "PROD, Data"}
>> write-output $SQLDatabase.DatabaseName
>> }
>> }
**DBname5**
这里的问题是对列表使用 -notmatch
和 -match
运算符会产生意外结果。切换到 -notcontains
和 -contains
以获得完全匹配将产生所需的结果。
$AzSqlServer = Get-AzSqlServer
if ($AzSqlServer) {
foreach ($server in $AzSqlServer) {
$SQLDatabase = Get-AzSqlDatabase -ServerName $server.ServerName -ResourceGroupName $server.ResourceGroupName |
Where-Object {$_.tags.Values -notcontains "PROD, Data"}
$SQLDatabase.Databasename
}
}
使用 -notcontains
时,将对列表的项目进行整体比较。如果没有与目标项目匹配的项目,该列表将仅 return true
。当对列表使用 -notmatch
时,列表中与目标项不匹配的项将被 returned。任何 returned 项目都会导致 True
导致 boolean
语句。以下是您的体验的简化示例:
# Notice how both boolean statements return True
PS> 1,2,3 -notmatch 2
1
3
PS> [bool](1,2,3 -notmatch 2)
True
PS> 1,2,3 -match 2
2
PS> [bool](1,2,3 -match 2)
True
# Now using -notcontains and -contains
PS> 1,2,3 -notcontains 2
False
PS> 1,2,3 -contains 2
True