gpt4 book ai didi

azure - 如果 'foreach' 中的所有值均为 true

转载 作者:行者123 更新时间:2023-12-03 01:00:49 25 4
gpt4 key购买 nike

我正在编写一个 Powershell 脚本,用于查找所有容器“LastModifiedDate”小于 6 个月的所有 Azure 存储帐户。如果只有一个容器对此语句不成立,则应忽略 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 的美妙之处,你不需要自己迭代,equality operators将自行迭代左侧参数集合

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

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

$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

全部正确

一般来说,如果您想检查是否所有值都为真,您可以只检查如果没有 正确:

$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

关于azure - 如果 'foreach' 中的所有值均为 true,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60654412/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com