ipsec600
asked on
Powershell Script
Hi Experts,
I am trying to run the following powershell script to generate report for users who have not logged in for last 30 days and accordingly it will report to their manager and helpdesk. But encountering the error " Get-ADUser : Cannot validate argument on parameter 'identify'. The argument is null. Supply a non-null argument and try the command again
PS Script:
I am trying to run the following powershell script to generate report for users who have not logged in for last 30 days and accordingly it will report to their manager and helpdesk. But encountering the error " Get-ADUser : Cannot validate argument on parameter 'identify'. The argument is null. Supply a non-null argument and try the command again
PS Script:
Import-Module ActiveDirectory
$90Days = (get-date).adddays(-30)
$expiring = (get-date).adddays(7)
$Today = get-date
$users = Get-ADUser -SearchBase "OU=Users,DC=test,DC=com" -filter * -Properties lastlogondate, Manager, AccountExpirationDate | Where-Object {($_.lastlogondate -lt (get-date).adddays(-30)) -AND ($_.enabled -eq $true) -AND $_.DistinguishedName -notmatch 'OU=Service Accounts,OU=Users,DC=test,DC=com' -AND $_.DistinguishedName -notmatch 'OU=Meeting Room,OU=Users,DC=test,DC=com'}
$messageGeneral = "Not logged in over 30 Days! and have no Manager associated:`r`n`r`n"
$smtpserver = "mailserver.test.com"
$users.Count
$expiring
foreach ($user in $users)
{
$manager = "NO MANAGER SPECIFIED"
$manager = (Get-ADUser $user.Manager -Properties mail).mail
if ($manager -eq "NO MANAGER SPECIFIED")
{
$messageGeneral += $user.Name + "`tLAST LOGON: " + $user.lastlogondate + "`r`n"
}
if($manager -ne "NO MANAGER SPECIFIED")
{
if($manager -eq "rocky@test.com")
{
$manager = "metals@test.com"
}
if($manager -eq "icon@test.com")
{
$manager = "metals@test.com"
}
if($user.AccountExpirationDate -ne $null)
{
$ExpiryDate = $user.AccountExpirationDate
$AccountAgeLeft = $ExpiryDate-$Today
$DaysLeft = $AccountAgeLeft.days
$DaysLeft
}
if(($DaysLeft -lt 7) -or ($user.AccountExpirationDate -eq $null))
{
$emailMsg = "Dear Manager,<br><br>"
$emailMsg += "If you are not responsible for this account please forward accordingly.<br>"
$emailFrom = "helpdesk@test.com"
$Subject = "Helpdesk Requires Your Response - A Direct Report Has Not Logged In Within The Last 30 Days"
$message = New-Object System.Net.Mail.MailMessage "helpdesk@test.com", $manager
$message.Subject = $Subject
$message.cc.Add("CChelpdesk@test.com")
$message.IsBodyHTML = $true
$message.Body = $emailMsg
$smtp = new-object Net.Mail.SmtpClient($smtpserver)
$smtp.Send($message)
}
}
}
$Subject = "Users with No Manager - No Login in Last 30 Days"
$smtp = new-object Net.Mail.SmtpClient($smtpserver)
$smtp.Send($emailFrom, "helpdesk@test.com", $Subject, $messageGeneral)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
@Alicia W. - $user is the current object in the foreach looping through $users. It is defined by the foreach statement.
Ok, thanks.
I'm new to PowerShell so I wasn't familiar with how to use the "ForEach" statements.
I'm new to PowerShell so I wasn't familiar with how to use the "ForEach" statements.
ASKER
Apology for the delay response since I was out for a long vacation and didn't get chance to login. Thank you Guys for your excellent support.
you can accomplish the same using free ad info reporting tool mentioned below.
http://www.cjwdev.com/Software/ADReportingTool/Info.html
http://www.cjwdev.com/Software/ADReportingTool/Info.html
So the powershell script is working or not ?
foreach ($user in $users)
I don't see a variable set up for "$user" in your script. Did you copy the complete script into your question?
For example, $user is on line 13:
$manager = (Get-ADUser $user.Manager -Properties mail).mail
and, on line 16:
$messageGeneral += $user.Name + "`tLAST LOGON: " + $user.lastlogondate + "`r`n"