【发布时间】: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