【问题标题】:Where statement in foreach loop, comparing datesforeach 循环中的 where 语句,比较日期
【发布时间】:2019-01-29 10:07:02
【问题描述】:

尝试导入包含姓名、电子邮件、终止日期、停止日期的用户列表,并首先检查停止日期或终止日期是否已过。

尝试添加 [datetime] 并使用 get-date $user.'stop date' 但没有任何运气。 它似乎可以使用以下代码而没有相同的问题,或者我得到相同的错误,但它确实检查并写出其中一个值更大:

$StopFolder = Get-ChildItem C:\test\123\*.csv |sort LastWriteTime -descending|select -first 1
$Stoplist = Import-Csv $StopFolder -delimiter ';'

$CurrentDate = Get-Date
foreach($Date in $Stoplist){
if($CurrentDate -eq (get-date $Date.'Stop Date')){write-host Equal}

if($CurrentDate -gt (get-date $Date.'Stop Date')){write-host Greater}

if($CurrentDate -lt (get-date $Date.'Stop Date')){write-host Less}}

但同样的方法似乎不适用于以下内容,并且无法真正弄清楚原因。我想我需要将它转换为日期,但不确定为什么它在上面而不是下面起作用,也不知道如果 get-date 不起作用,如何准确地转换它。

$StopFolder = Get-ChildItem C:\test\123\*.csv |sort LastWriteTime -descending|select -first 1
$Stoplist = Import-Csv $StopFolder -delimiter ';'
$CurrentDate = Get-Date

foreach($User in $Stoplist|where($_.'stop date' -lt $CurrentDate)){

try{
    $Usermail = $User.'e-mail address'
    $Username = get-aduser -Filter "EmailAddress -eq '$Usermail'" -properties Enabled


        if($Username.enabled){
        echo $Username 'still exists and is NOT disabled' >> C:\NotDisabled.txt
        }

        if($Username.enabled -eq $false){
        echo $Username 'still exists and is disabled' >> C:\NotDeleted.txt 
        }
}
catch{continue}
}

预期结果是仅在当前用户停止日期小于当前日期时才启动循环。目前没有任何反应,删除 where 部分,其余部分似乎运行良好。

非常感谢任何帮助。

编辑: CSV 日期是这样的:

停止日期

01-02-2023
21-09-2019
21-01-2019
01-01-2019
01-01-2019

【问题讨论】:

  • $user替换$_?或者在$stoplist | where... 周围加上括号。我不得不承认这是在foreach 中创造性地使用where,因为where 是一种foreach,只是带有过滤器......
  • 这完全取决于 CSV 中使用的日期格式。如果您向我们展示这一点,我们可以告诉您如何将 $_.'stop date' 转换为有效的 DateTime 对象,以便与 $CurrentDate 进行比较。
  • 已经试过了,没有错误但没有输出。文件中的值为停止日期:01-02-2023 21-09-2019 21-01-2019 01-01-2019 01-01-2019 感谢到目前为止的帮助

标签: powershell


【解决方案1】:

编辑:错误不仅在逻辑中,而且是错字:| where 需要花括号 > | where {} 不是括号。


'stop date' 创建一个日期:

(get-date -date $_.'stop date')

一行:

foreach($User in $Stoplist|where{(get-date -date $_.'stop date') -lt $CurrentDate}){...}

这里$Stoplist|where{(get-date -date $_.'stop date') -lt $CurrentDate}是一个单元,可以用括号封装:

foreach($User in ($Stoplist|where{(get-date -date $_.'stop date') -lt $CurrentDate}) ){...}

但在$User in $Stoplist 周围没有括号,管道| 仅指最后一个对象$Stoplist

【讨论】:

  • 谢谢,但不幸的是同样的结果:“Get-Date:无法将参数'Date'绑定到目标。异常设置“Date”:“无法将null转换为“System.DateTime”类型。“”
  • Get-Date -Date "21-09-2019" 有效... 检查您的 .csv 文件是否正确导入。检查$Stoplist[0].'stop date' 并测试Get-Date -Date $Stoplist[0].'stop date'
  • @MFR1988 从例子中我们可以看出CSV中的日期格式是dd-MM-yyyy。但是,您当前的系统区域设置可以默认为另一种格式,例如MM-dd-yyyy,在这种情况下(Get-Date -Date $_.'stop date') 将不起作用。这样做会更安全[datetime]::ParseExact($User_.'stop date', 'dd-MM-yyyy', $null)
  • 您的第一条评论很到位。但是,在您的代码中,您没有使用它。您应该将$_ 更改为$User。就像现在的 foreach 一样,$_ 没有任何意义;是$null
  • 没有。我的意思是将$_.'stop date' 替换为$User.'stop date'。该代码使用foreach($User in $Stoplist) {...},而不是$Stoplist | ForEach-Object {...}。然后也使用[datetime]::ParseExact($User_.'stop date', 'dd-MM-yyyy', $null) 并且 Bob 是你的叔叔。
猜你喜欢
  • 2017-09-16
  • 1970-01-01
  • 2023-03-14
  • 2012-01-02
  • 1970-01-01
  • 1970-01-01
  • 2021-10-09
  • 2011-03-10
  • 1970-01-01
相关资源
最近更新 更多