Powershell foreach循环中的Where语句,比较日期

Powershell foreach循环中的Where语句,比较日期,powershell,Powershell,尝试导入包含姓名、电子邮件、终止日期、停止日期的用户列表,并首先检查是否已过停止日期或终止日期 尝试添加[datetime]并使用get date$user.“stop date”,但没有成功。 它似乎可以在没有相同问题的情况下使用下面的代码,或者我得到了相同的错误,但它确实检查并写出其中一个值更大: $StopFolder = Get-ChildItem C:\test\123\*.csv |sort LastWriteTime -descending|select -first 1 $Sto

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

尝试添加[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

编辑:错误不仅在逻辑中,而且是输入错误:
|其中
需要大括号>
|其中{}
不是括号


从“停止日期”创建日期

(get-date -date $_.'stop date')
一行:

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

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

但是如果$Stoplist中的
$User周围没有括号,管道
只引用最后一个对象
$Stoplist

$User
替换
$
?或者将括号括在
$stoplist |中…
。我必须承认,这是在
foreach
中创造性地使用
where
,因为
where
是一种带有过滤器的
foreach
。这一切都取决于CSV中使用的日期格式。如果您向我们展示了这一点,我们可以告诉您如何将
$.'stop date'
转换为有效的DateTime对象,以便与
$CurrentDate
进行比较。已经尝试过了,没有错误,但没有输出。文件中停止日期的值为:01-02-2023 21-09-2019 21-01-2019 01-01-2019感谢您的帮助,但遗憾的是,相同的结果是:“Get date:无法将参数“date”绑定到目标。异常设置“date”:“无法将null转换为类型”System.DateTime”。“
Get date-date”21-09-2019”
有效。。。检查您的.csv文件,并检查其是否正确导入。检查
$Stoplist[0]。“停止日期”
并测试
获取日期-日期$Stoplist[0]。“停止日期”
@MFR1988我们可以从示例中看到,CSV中的日期格式为
dd-MM-yyyy
。但是,您当前的系统区域设置可以默认为另一种格式,例如
MM dd yyyy
,在这种情况下,
(Get Date-Date$.'stop Date')
将不起作用。做
[datetime]:ParseExact($User.'stop date','dd-MM-yyyy',$null)会更安全些。
。您的第一条评论是正确的。但是,在您的代码中,您没有使用它。您应该将
$\uuu
更改为
$User
。因为foreach现在是,
$\uu
没有意义;是
$null
否。我的意思是将
$.'stop date'
替换为
$User.'stop date'
。代码使用的是
foreach($Stoplist中的用户){…}
,而不是
$Stoplist | foreach对象{…}
。然后也使用
[datetime]::ParseExact($User.'stop date','dd-MM-yyyy',$null)
和Bob的叔叔。