如果“foreach”中的所有值都为 true

问题描述 投票:0回答:2

我正在编写一个 Powershell 脚本,用于查找所有 Azure 存储帐户,其中所有容器的“LastModifiedDate”时间均小于 6 个月。如果只有一个容器与此声明不符,则应忽略 ASA。

实现此目的的一种方法是获取一个存储帐户的“LastModifiedDate”数组,然后将其与 (Get-Date).AddDays(-180) 进行比较。问题是如何正确地做到这一点?如果数组中只有一个值(如果为 FALSE),则应忽略它。

我使用 BREAK 运算符实现了这一点,但我认为这不是一个好的解决方案。有人可以帮我解决这个问题吗?

function check_stores {

     $stores = Get-AzureRmResource -ODataQuery "`$filter=resourcetype eq 'Microsoft.Storage/storageAccounts'" $x = (Get-Date).AddDays(-180)

     foreach($store in $stores){

         $storename = $store.Name

         $names = (Get-AzureRmStorageContainer -ResourceGroupName $store.ResourceGroupName -StorageAccountName $store.Name).Name

         foreach($name in $names){

             $date = (Get-AzureRmStorageContainer -ResourceGroupName $store.ResourceGroupName -StorageAccountName $store.Name -Name $name).LastModifiedTime

             if($date -gt $x){
                  break
             }
         }

         if($x -gt $date){
             "Storage Account Name: $storename"

         }
     }

}

check_stores
powershell loops syntax equality-operator
2个回答
2
投票

正如评论的:这就是PowerShell的美妙之处,你不需要自己迭代,等式运算符会自己迭代左边的参数集合

当运算符的输入是标量值时,比较运算符 返回一个布尔值。当输入是值的集合时, 比较运算符返回任何匹配的值。如果没有 集合中的匹配项,比较运算符返回空数组。

由于我无法访问您的环境,因此我为此创建了一个通用答案:

$Dates = [DateTime[]]('2020-03-10', '2020-03-11', '2020-03-12', '2020-03-14')
$Date1 = [DateTime]'2020-03-09'
$Date2 = [DateTime]'2020-03-12'
$Date3 = [DateTime]'2020-03-15'

任何日期

检查是否有任何日期小于给定日期

If ($Dates -lt $Date1) {"Some dates are less then $Date1"} Else {"None of the dates are less then $Date1"}
If ($Dates -lt $Date2) {"Some dates are less then $Date2"} Else {"None of the dates are less then $Date2"}
If ($Dates -lt $Date3) {"Some dates are less then $Date3"} Else {"None of the dates are less then $Date3"}

产量:

None of the dates are less then 03/09/2020 00:00:00
Some dates are less then 03/12/2020 00:00:00
Some dates are less then 03/15/2020 00:00:00

所有日期

这意味着,如果您想检查所有日期是否在范围内,您将需要执行相反的操作(检查任何日期是否不大于或等于然后给定日期):

If (!($Dates -ge $Date1)) {"All dates are less then $Date1"} Else {"Some of the dates aren't less then $Date1"}
If (!($Dates -ge $Date2)) {"All dates are less then $Date2"} Else {"Some of the dates aren't less then $Date2"}
If (!($Dates -ge $Date3)) {"All dates are less then $Date3"} Else {"Some of the dates aren't less then $Date3"}

产量:

Some of the dates aren't less then 03/09/2020 00:00:00
Some of the dates aren't less then 03/12/2020 00:00:00
All dates are less then 03/15/2020 00:00:00

都是真的

一般来说,如果您想检查 是否所有值都为 true,您可以只检查 if none of the values are not true

$SomeTrue = $False, $True, $False, $True # (Not all true)
$AllTrue = $True, $True, $True, $True

!($SomeTrue -ne $True) # => All true?
False
!($AllTrue -ne $True) # => All true ?
True

1
投票

我从@iRon 那里偷了代码。我把它作为答案,因为在评论中是无法阅读的。她/他应该创建答案并获得荣誉:)

$AllAccounts = Get-AzStorageAccount
$AccountsWithAtLeaseOneModifiedObject =  ($AllAccounts | ? { ($_ | Get-AzStorageContainer).LastModified -gt (Get-Date).AddDays(-180)}).StorageAccountName
compare-object $AccountsWithAtLeaseOneModifiedObject $AllAccounts.StorageAccountName
© www.soinside.com 2019 - 2024. All rights reserved.