微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Powershell:在 foreach 循环中计数

如何解决Powershell:在 foreach 循环中计数

我有一个查询 AD 用户证书的 Powershell 脚本。我想检查并通知用户他的 VPN 证书是否即将到期。到目前为止,脚本运行良好,但我有一些实例,其中用户已经拥有 2 个 VPN 证书,如果是这种情况,我不想通知(在步骤“### 的脚本中,如果计数小于则执行后续步骤” 2”)。我试图将“.count”添加到一些变量中,但由于它在 foreach 中,它总是给我一个“1”作为匹配。我不知道如何实现这一点,请帮忙。这是脚本:

param (
[string]$queryDN       = '...DC=com',[string]$VPNcertname   = 'VPN OID',[int]$days             = 14
)

# Make decision what to check
$userpath          = $queryDN
$cert2check        = $VPNcertname

# Begin script
$users = (Get-ADGroupMember -Identity $userpath).distinguishedname
foreach ($dude in $users) {
 $user = Get-ADUser $dude -Property Certificates
 $Certificatelist = $user.Certificates | foreach {
  New-Object System.Security.Cryptography.X509Certificates.X509Certificate2 $_
 }
   ForEach($cert in $Certificatelist){
    $querycertificate = if($cert.EnhancedKeyUsageList.Where({$_.FriendlyName -eq $cert2check})){
### Execute next steps if count is less then 2
     $expirationDate = $Cert.NotAfter
       if ($expirationDate -lt [datetime]::Today.AddDays($days)) {
        write-host The $cert2check certificate for user: $user.UserPrincipalName`,expires: $expirationDate. This is less than: $days days and should be changed soon.
      }
     }
   }
}

谢谢

解决方法

谢谢@Vesper。 This 正是我要找的:

$CertList2=$Certificatelist|where {$_.EnhancedKeyUsageList.FriendlyName -eq $cert2check}; if ($CertList2.count -gt 1) {...}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。