没有错误,但PowerShell代码无法正常工作

时间:2015-02-04 18:40:12

标签: powershell csv

这个脚本没有错误,但是我摸不着头脑,因为我检查了csv数据,并且名字和姓氏匹配得很好(因此不应该将错误写入日志)。然而,它总是这样做,而不是跳过写入日志。所以,我必须注释掉“继续”行,否则脚本将无法完成。

另外,如果从今天开始,将来的重新雇用日期为8天,我想启用用户对象,但这似乎也不起作用(对我来说没有明显的错误)。有人可以检查一下我的逻辑吗?这不是完整的脚本,只是我找不到任何问题的区域。感谢

`$CSVLine = "C:\scripts\adp\Test ADP Import spec.csv"
$ErrorLog = "C:\scripts\adp\ADPProcessErrors.csv"
[xml]$DivLocFile = Get-Content -Path "C:\Scripts\adp\DivLocData.xml"
$DocumentRoot = $DivLocFile.DocumentElement
$UserLocationCode = @{}
$a = Get-Date

ForEach ($row in (Import-Csv $CSVLine)) {

    #Skips over Terminiated employees
If ($CSVLine.EmployeeStatus -eq "Terminated") { Continue }
    #Gets the user's First and Last name
    $User = Get-ADUser -LDAPFilter ("(sAMAccountName=" + $Row.sAMAccountName + ")") -Properties *

    If ($User -eq $Null) {
        #If there is no First and Last name this will throw an error and go on to the next record.
        ($Row.sAMAccountName + ",AD Object Not Found") | Out-File -FilePath $ErrorLog -Append

        Continue
         }  
        #Checks whether First or Last names don't match
        If (($User.givenName -ne $CSVLine.'First Name') -or ($User.sn -ne $CSVLine.'Last Name')) { 

        ($Row.sAMAccountName + ",givenName and sn property mismatch") | Out-File -FilePath $ErrorLog -Append

        #Continue
         }
        #Enables a disabled user if they have a hire date 8 days in advance of today's date. 
        If (($CSVLine.'Date of hire/rehire' -gt ($a)) -and ($CSVLine.'Date of hire/rehire') -le ($a.AddDays(8)) -and (!($User.Enabled)))  {

        (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
        }`

1 个答案:

答案 0 :(得分:0)

看起来很多你的IF语句都在使用$ CSVLine,看起来他们应该使用$ row