0
votes

The following code I want to use to email out a list of people whos ad account is expiring. However I am receiving this error:

Search-ADAccount : The term 'Search-ADAccount' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At C:\Users\georgeh\Desktop\ADAccount Test.PS1:8 char:18 + $Message.body = Search-ADAccount -AccountExpiring -TimeSpan "30" | Select-Objec ... + ~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (Search-ADAccount:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException

Search-ADAccount : The term 'Search-ADAccount' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At C:\Users\test\Desktop\ADAccount Test.PS1:15 char:17 + $Message.body = Search-ADAccount -AccountExpiring -TimeSpan "30" | Select-Object ... + ~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (Search-ADAccount:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException

Code:

$smtpServer = "test.local" 
$smtpFrom = "[email protected]" 
$smtpTo = "[email protected]"; 
$messageSubject = "These users AD Accounts are expiring"
$Message = New-Object System.Net.Mail.mailmessage $smtpFrom, $smtpTo 
$Message.Subject = $messageSubject
$content = 'TEST'

$Message.body =  Search-ADAccount -AccountExpiring -TimeSpan "30" |
        Select-Object Name,AccountExpirationDate |
        Sort-Object AccountExpirationDate |
        ConvertTo-HTML -Head $style 

$Message.IsBodyHtml = $true

$style = "<style>BODY{font-family: Arial; font-size: 10pt;}" 
$style = $style + "TABLE{border: 1px solid red; border-collapse: collapse;}" 
$style = $style + "TH{border: 1px solid red; background: #dddddd; padding: 5px; }" 
$style = $style + "TD{border: 1px solid red; padding: 5px; }" 
$style = $style + "</style>" 

$Message.body = Search-ADAccount -AccountExpiring -TimeSpan "30" |
    Select-Object Name,AccountExpirationDate |
    Sort-Object AccountExpirationDate |
    ConvertTo-HTML -Head $style 

$smtp = new-Object Net.Mail.SmtpClient($smtpServer) 

if ($content) {
    $smtp.Send($message) 
}
1
What Windows version are you running on your PC? Make sure your PC is up to date with WMF and.NET and that you can contact a domain controller running ADWS.Scepticalist
When running this: Search-ADAccount -AccountExpiring -TimeSpan "30" | Select-Object Name,AccountE xpirationDate | Sort-Object AccountExpirationDate | on its own it returns results. When trying to incorporate into the email section it then fails.glh
Check all the whitespace in "$message.body =" for unknown/illegal characters - the script you have there works fine for me. If in doubt take out the line breaks and put it all on one line.Scepticalist
What's up with this code tho? For instance this bit in the first error message At C:\Users\georgeh\Desktop\ADAccount Test.PS1:8 char:18 + $Message.body and compared to this bit in the second error At C:\Users\test\Desktop\ADAccount Test.PS1:15 char:17 + $Message.body is kind of confusing.notjustme

1 Answers

2
votes

All of your syntax looks good. Are you sure the the ActiveDirectory module is installed on the computer that's running this script?

Since you commented that the script runs outside of a variable. For testing only, try running that command outside of the variable but still in this script?