如果"foreach"中的所有值都为true [英] If all values in 'foreach' are true

查看:86
本文介绍了如果"foreach"中的所有值都为true的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写Powershell脚本来查找所有 Azure存储帐户,其中所有容器"LastModifiedDate"均少于6个月.如果只有一个容器对本声明不正确,则应忽略ASA.

I'm writing a Powershell script for finding all Azure Storage Accounts in which all containers 'LastModifiedDate' is less than 6 months. If ONLY one container is FALSE to this statement ASA should be ignored.

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

One way how to achieve this was to get an array of 'LastModifiedDate' for one Storage Account and then compare it with (Get-Date).AddDays(-180). The question is how to do this correctly? It should be ignored if only one value from an array if FALSE.

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

I achieved this with BREAK operator but I think this is not a good solution. Could anyone help me with this issue?

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的优点,您无需重复自己,

As commented: This is the beauty of PowerShell, you don't need to iterate yourself, the equality operators will iterate over the left argument collection by themself

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

When the input to an operator is a scalar value, comparison operators return a Boolean value. When the input is a collection of values, the comparison operators return any matching values. If there are no matches in a collection, comparison operators return an empty array.

由于我无权访问您的环境,因此我为此创建了一个常见答案:

As I do not have access to your environment, I have created a common answer for this:

$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'

任何日期

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

Any date

Check if any date is less then the given date

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

所有日期

这意味着如果要检查 所有 日期是否在该范围内,则需要执行相反的操作(检查 any 日期小于等于给定日期的 ):

All dates

This implies that if you want to check whether all dates falls within the range you will need to do the opposite (check if any date is not greater or equal then the given date):

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

全部为真

通常来说,如果要检查所有值是否均为真,则可以仅检查如果所有值 none 不为 true :

All True

Generally speaking, if you want to check if all values are true, you might just check for 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

这篇关于如果"foreach"中的所有值都为true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆