Reputation: 447
I am trying to write a Powershell script that identifies users who haven't logged in for 90 days, but I keep getting this error message:
Cannot find an overload for "op_Subtraction" and the argument count: "2". At first I thought it was a variable type mismatch but looking at the variables for subtraction it looks fine.
PS C:\> $today.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True DateTime System.ValueType
PS C:\> $users[198].LastLogonDate.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True DateTime System.ValueType
$today = Get-Date
$days = 90
$users = Get-ADUser -Properties * -Filter *
foreach ($i in $users)
{
$difference = $today - $i.LastLogonDate
#Write-Host $i.Name + $difference.Days
if ($difference.Days -ge $days){Write-Host $i.name " hasn't logged on in 90 days"}
elseif ($i.LastLogonDate -eq $null) {Write-Host $i.name " has null value"}
else {Write-Host " No Value"}
}
Thoughts??
Thanks!!
Upvotes: 5
Views: 16767
Reputation: 126852
How about this:
Search-ADAccount -AccountInactive -TimeSpan "90" -UsersOnly
Upvotes: 0
Reputation: 12321
You get the error for users who have never logged on. The LastLogonDate property is null, therefore you can't subtract it from $today. To prevent the error, check whether the property is null first, in an if statement, and only attempt the subtraction otherwise.
foreach ($i in $users) {
if ($i.LastLogonDate -eq $null) {
Write-Host $i.name " has null value"
} else {
$difference = $today - $i.LastLogonDate
if ($difference.Days -ge $days) {
Write-Host $i.name " hasn't logged on in 90 days"
} else {
Write-Host " No Value"
}
}
}
BTW, I'm not quite sure in which cases you intended to output the " No Value" message, but that's going to be displayed for any users who have logged on in the last 90 days.
Upvotes: 1