这是一个相当简单的PowerShell脚本,用于导入.csv文件,将标头用作变量,然后将值写入各个用户的AD帐户。
由于某种原因,脚本正在跳过整个ELSE
语句。我不知道是否将括号放在错误的位置,或者发生了什么事。有人可以帮我看看吗?我可以根据需要提供.csv文件,但我确实认为,这只是脚本有点过时的一种情况。
我们将不胜感激。
您可以DM我获取此文件的实际副本。
#-----------------------------------------------------------------------
#Section: Add headers to Delta 158 file.
#Operator will place two generic files in "D:\generic"
$delta158 = get-childitem D:\generic -Filter generic-2 * | select -exp name #Necessary line because get-content will not accept wild cards.
get-content -LiteralPath D:\generic\headers158.csv, D:\generic\$delta158 | out-file -encoding ascii D:\generic\hr158Delta.pip
#-----------------------------------------------------------------------
#Move error log files to archive
Move-Item -Path D:\generic\158ErrorLog * -Destination D:\generic\archive\
#Place a timestamp.
function Get-TimeStamp
{
return "[{0:MM/dd/yyyy} {0:HH:mm:ss}]" -f (Get-Date)
}
Get-TimeStamp
$tdate = get-date -UFormat % m- % d- % y #Today's date stored in format '06-25-2018'. One required field in delta file requires this format.
# Import AD Module
Import-Module ActiveDirectory
# cd generic:
write-Host 'Updating AD Attributes. Please wait...' -NoNewline -ForegroundColor Green -BackgroundColor Black
# Import .csv into variable $users.
$users = Import-Csv -Path "D:\generic\hr158Delta.pip" -Delimiter '|'
foreach ($user in $users)
{
$transactionDate = get-date $user."Transaction Date"
$emplstatus = $user."Employee Status"
$jobaction = $user."Job Action"
$jobactionreason = $user."Job Action Reason"
$usr = $user."User ID"
$usremplid = $user."Employee ID"
$ext1 = $user."Hire Date"
$ext3 = $user."Employee Type Description"
$ext3Else = $user."Employee Type"
$ext4 = $user."Employee Status"
$ext5 = $user."Job Code"
$ext9 = $user."Employee"
$ext9Else = $user."Employee Class"
$ext14 = $user."Badge Number1"
$fwrdaddr = $user."Facility Code"
$roomnum = $user."Facility Description"
$preflang = $user."Cost Center/Department"
$mgrid = $user."Manager User ID"
$empnum = $user."Employee ID"
$jobtitle = $user."Job Code Description"
$workphone = $user."Work Phone"
Write-Output $transactionDate
if ($transactionDate -le (get-date $tdate))
{
if (($emplstatus -eq "A") -or ($emplstatus -eq "L") -or ($emplstatus -eq "P") -or ($emplstatus -eq "S") -or ($emplstatus -eq "W"))
{
if ($user."Job Indicator" -eq "Y")
{
#Search in specified domain and update existing attributes.
#$usr = $user."User ID" #Moved from local to foreach scope variable.
Write-Output $usr
If ($ext1 -gt 1)
{
Set-ADUser $usr -Clear extensionAttribute1
Set-ADUser $usr -Add @{ extensionAttribute1 = $ext1 }
}
If ($ext3 -gt 1)
{
Set-ADUser $usr -Clear extensionAttribute3
Set-ADUser $usr -Add @{ extensionAttribute3 = $ext3 }
}
If ($ext3 -le 1)
{
Set-ADUser $usr -Clear extensionAttribute3
Set-ADUser $usr -Add @{ extensionAttribute3 = $ext3Else }
}
If ($ext4 -gt 1)
{
Set-ADUser $usr -Clear extensionAttribute4
Set-ADUser $usr -Add @{ extensionAttribute4 = $ext4 }
}
If ($ext5 -gt 1)
{
Set-ADUser $usr -Clear extensionAttribute5
Set-ADUser $usr -Add @{ extensionAttribute5 = $ext5 }
}
If ($ext9 -eq "EMP")
{
Set-ADUser $usr -Clear extensionAttribute9
Set-ADUser $usr -Add @{ extensionAttribute9 = $ext9 }
}
If ($ext9 -ne "EMP")
{
Set-ADUser $usr -Clear extensionAttribute9
Set-ADUser $usr -Add @{ extensionAttribute9 = $ext9Else }
}
If ($ext14 -gt 1)
{
Set-ADUser $usr -Clear extensionAttribute14
Set-ADUser $usr -Add @{ extensionAttribute14 = $ext14 }
}
If ($fwrdaddr -gt 1)
{
Set-ADUser $usr -Clear forwardingAddress
Set-ADUser $usr -Add @{ forwardingAddress = $fwrdaddr }
}
If ($roomnum -gt 1)
{
Set-ADUser $usr -Clear roomNumber
Set-ADUser $usr -Add @{ roomNumber = $roomnum }
}
If ($preflang -gt 1)
{
Set-ADUser $usr -Clear preferredLanguage
Set-ADUser $usr -Add @{ preferredLanguage = $preflang }
}
If ($empnum -gt 1)
{
Set-ADUser $usr -Clear employeeNumber
Set-ADUser $usr -Add @{ employeeNumber = $empnum }
}
If ($jobtitle -gt 1)
{
Set-ADUser $usr -Clear title
Set-ADUser $usr -Add @{ title = $jobtitle }
}
If ($workphone -gt 1)
{
Set-ADUser $usr -Clear telephoneNumber
Set-ADUser $usr -Add @{ telephoneNumber = $workphone }
}
If ($mgrid -gt 1)
{
Set-ADUser $usr -Clear manager
Set-ADUser $usr -manager $mgrid
}
} #Bracket for Third If Statment
} #Bracket for Second If Statement
} #Bracket for first If Statment
Else
{
if (($jobaction -eq "HIR") -or (($jobaction -eq "ADD") -and ($jobactionreason -eq "ADD"))) #I don't see any "ADDs" in the Job Action Reason column of 158 file.
{
if ($usr -eq $null)
{
$byEmplid = get-aduser -filter { (employeeNumber -Like $usremplid) }
$usr = $byEmplid.SamAccountName
}
Write-Output $usr
If ($ext1 -gt 1)
{
Set-ADUser $usr -Clear extensionAttribute1
Set-ADUser $usr -Add @{ extensionAttribute1 = $ext1 }
}
If ($ext3 -gt 1)
{
Set-ADUser $usr -Clear extensionAttribute3
Set-ADUser $usr -Add @{ extensionAttribute3 = $ext3 }
}
If ($ext3 -le 1)
{
Set-ADUser $usr -Clear extensionAttribute3
Set-ADUser $usr -Add @{ extensionAttribute3 = $ext3Else }
}
If ($ext4 -gt 1)
{
Set-ADUser $usr -Clear extensionAttribute4
Set-ADUser $usr -Add @{ extensionAttribute4 = $ext4 }
}
If ($ext5 -gt 1)
{
Set-ADUser $usr -Clear extensionAttribute5
Set-ADUser $usr -Add @{ extensionAttribute5 = $ext5 }
}
If ($ext9 -eq "EMP")
{
Set-ADUser $usr -Clear extensionAttribute9
Set-ADUser $usr -Add @{ extensionAttribute9 = $ext9 }
}
If ($ext9 -ne "EMP")
{
Set-ADUser $usr -Clear extensionAttribute9
Set-ADUser $usr -Add @{ extensionAttribute9 = $ext9Else }
}
If ($ext14 -gt 1)
{
Set-ADUser $usr -Clear extensionAttribute14
Set-ADUser $usr -Add @{ extensionAttribute14 = $ext14 }
}
If ($fwrdaddr -gt 1)
{
Set-ADUser $usr -Clear forwardingAddress
Set-ADUser $usr -Add @{ forwardingAddress = $fwrdaddr }
}
If ($roomnum -gt 1)
{
Set-ADUser $usr -Clear roomNumber
Set-ADUser $usr -Add @{ roomNumber = $roomnum }
}
If ($preflang -gt 1)
{
Set-ADUser $usr -Clear preferredLanguage
Set-ADUser $usr -Add @{ preferredLanguage = $preflang }
}
If ($empnum -gt 1)
{
Set-ADUser $usr -Clear employeeNumber
Set-ADUser $usr -Add @{ employeeNumber = $empnum }
}
If ($jobtitle -gt 1)
{
Set-ADUser $usr -Clear title
Set-ADUser $usr -Add @{ title = $jobtitle }
}
If ($workphone -gt 1)
{
Set-ADUser $usr -Clear telephoneNumber
Set-ADUser $usr -Add @{ telephoneNumber = $workphone }
}
If ($mgrid -gt 1)
{
Set-ADUser $usr -Clear manager
Set-ADUser $usr -manager $mgrid
}
}
}
}
Write-Host 'HR158 Sync Complete!' -ForegroundColor Green -BackgroundColor Black
#Place a timestamp.
function Get-TimeStamp
{
return "[{0:MM/dd/yyyy} {0:HH:mm:ss}]" -f (Get-Date)
}
Get-TimeStamp
#-----------------------------------------------------------------------
#Section: Move original Delta 158 file to archive.
start-sleep -s 10
Move-Item -Path D:\generic\$delta158 -Destination D:\generic\archive\
$file = "D:\generic\158ErrorLog" + (Get-Date).ToString("s").Replace(":", "-") + ".txt"
New-Item $file -ItemType file
$errorlog = get-childitem D:\generic -Filter 158errorlog * | select -exp name
#Error logging
$error | out-file -Encoding ascii D:\generic\$errorlog -append
start-sleep -s 10
Move-Item -Path D:\generic\158ErrorLog * -Destination D:\generic\archive\
#$error
#$Error[0].InvocationInfo
#-----------------------------------------------------------------------