我似乎不明白为什么这个脚本不起作用。没有错误,但是$User对象的求值结果不是$True。目标是如果另一个系统显示重新雇用日期小于或等于8天,则启用重新雇用用户的AD帐户。
$CSVLine = "C:scriptsadpTest ADP Import spec.csv"
$ErrorLog = "C:scriptsadpADPProcessErrors.csv"
$a = Get-Date
ForEach ($row in (import-csv $CSVLine)) {
$User = Get-ADUser -LDAPFilter ("(sAMAccountName=" + $Row.sAMAccountName + ")") -Properties *
#Write-Host ("User:" + $user.samaccountname + " enabled =" + $user.enabled + " ")
If ((($User.Enabled -eq $False)) -and ($Row.'Date of hire/rehire' -gt $a) -and (($Row.'Date of hire/rehire') -le ($a.AddDays(8))) ) {
(Set-ADUser -Enabled $True -Identity $User)
("SID:" + $Row.sAMAccountName + ", User:[" + $User.givenName + " " + $User.sn + "] Re-Hire date is in range. Account enabled.") | Out-File -FilePath $ErrorLog -Append
}
}
Write-Host ("CSV Object: " + $Row.'Date of hire/rehire'.GetType())
Write-Host ("CSV Object Value:" + $Row.'Date of hire/rehire' + " " )
Write-Host ("User:" + $user.samaccountname + " enabled =" + $user.enabled + " ")
这些日期可能需要转换为[datetime]/dates,以便对$a
进行计算。最有可能的做法是把它们当作字符串,而不是按字母顺序进行比较。
PS C:Usersmcameron> "b" -gt "a"
True
无法准确告诉您没有样本日期该怎么办,因此我们可以查看格式。从评论中,如果你的日期格式像"2015年8月2日",那么一个简单的演员阵容就会解决这个问题。
PS C:Usersmcameron> [datetime]"2/8/2015"
Sunday, February 08, 2015 12:00:00 AM
将您的if
更新为以下内容:
If ((($User.Enabled -eq $False)) -and ([datetime]($Row.'Date of hire/rehire') -gt $a) -and ([datetime]($Row.'Date of hire/rehire') -le ($a.AddDays(8))) ){
#.....Process
}
如果没有输入文件的样本,这有点难说,但如果没有错误,我会首先检查$Row.sAMAccountName
是否对循环中的任何内容进行了评估。就像您注释掉的Write-Host
行一样,用于$user
属性。
干杯。