Hi
In order to disable leavers I need to check if the accountexpirationdate is less then or equal to today
I run the following if statement in a foreach block
$mainCsvPath = "C:\Users\Public\Documents\CSV\offboarding"
$withemployeeID = import-csv $maincsvpath\problem.csv
$date = (get-date)
$today = [DateTime]::ParseExact($date,'MM/dd/yyyy',$null)
foreach($Luser in $withemployeeID ){
$AccountExpired = [DateTime]::ParseExact($Luser.AccountExpirationDate,'MM/dd/yyyy',$null)
$Employeenumber = $Luser.EmployeeNumber
if(!($employeenumber -eq "")){
if($accountexpired -eq ""){
[PSCustomObject]@{
EmployeeNumber = $Luser.EmployeeNumber
SamAccountName = $Luser.SamAccountName
EmailAddress = $Luser.EmailAddress
UserPrincipalName = $Luser.UserPrincipalName
AccountExpirationdate = $accountexpired
Manager = $Luser.Manager
Treatmentdate = $today
} | Export-csv -path $mainCsvPath\incorrectdata.csv -notype -Append
}Else{
if($accountexpired -lt $today){
$AccountExpired = $Luser.AccountExpirationdate
$accountexpired = (get-date $accountexpired).AddDays(-1)
$accountexpired = (get-date $accountexpired).tostring("yyyy-MM-dd")
[PSCustomObject]@{
EmployeeNumber = $Luser.EmployeeNumber
SamAccountName = $Luser.SamAccountName
EmailAddress = $Luser.EmailAddress
UserPrincipalName = $Luser.UserPrincipalName
AccountExpirationdate = $accountexpired
Manager = $Luser.Manager
Treatmentdate = $today
} | Export-csv -path $mainCsvPath\leaversFinal.csv -notype -Append
}Else{
if($accountexpired -eq $today){
$AccountExpired = $Luser.AccountExpirationdate
$accountexpired = (get-date $accountexpired).AddDays(-1)
$accountexpired = (get-date $accountexpired).tostring("yyyy-MM-dd")
[PSCustomObject]@{
EmployeeNumber = $Luser.EmployeeNumber
SamAccountName = $Luser.SamAccountName
EmailAddress = $Luser.EmailAddress
UserPrincipalName = $Luser.UserPrincipalName
AccountExpirationdate = $accountexpired
Manager = $Luser.Manager
Treatmentdate = $today
} | Export-csv -path $mainCsvPath\leaversFinal.csv -notype -Append
}
}
}
}
}
my csv looks like this
“EmployeeNumber”,“SamAccountName”,“EmailAddress”,“UserPrincipalName”,“AccountExpirationDate”,“Manager”,“Treatmentdate”
“600027",“samaccountname”,"user@mydomain.com”,"user@mydomain.com",“10/30/2022 12:00:00 AM”,“myManager”,"
but when I run the code it does not validate the if statement ($accountexpired -lt $today) as being True what am I doing wrong here?