Hi,
Please replace the double quotes in <span class="math-inline"> with single quotes.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
hi All,
am looking for a powershell script which can send email to our helpdesk with all the list of users whose accounts are expiring in next 7 or 10 days. the list or table should need to have Name, SamAccount, Expiry Date, Manager.
Here is the script i have but getting error while executing the script.
Scirpt:
============================================================
$helpdeskEmail = "******@mydomain.com"
$expiringAccounts = = Search-ADAccount -AccountExpiring -UsersOnly -TimeSpan 10.00:00:00 | Get-ADUser -Properties Enabled, Manager, AccountExpirationDate, LastLogonDate | Select-Object Name,Manager,AccountExpirationDate,LastLogonDate,UserPrincipalName
$body = "The following Active Directory accounts will expire within the next 10 days:\n\n"
$body += "<table>
<thead>
<tr><th>Name</th><th>SAMAccountName</th><th>Expiration Date</th><th>UserPrincipalName</th><th>Manager</th></tr>
</thead>
<tbody>"
foreach ($account in $expiringAccounts) {
# Get manager details
$manager = Get-ADUser -Identity ($account.Manager)
$body += "<tr>
<td>$account.Name</td>
<td><span class="math-inline">account.SamAccountName</td>
<td></span>($account.AccountExpirationDate).ToShortDateString()</td>
<td>$account.UserPrincipalName</td>
<td>$manager.Name</td>
</tr>"
}
$body += "</tbody></table>"
Send-MailMessage -To $helpdeskEmail -From "******@mydomain.com" -Subject "Active Directory Accounts Expiring Soon" -Body $body -SmtpServer "smtp.office365.com"
========================================================================
Error:
PS C:> C:\Users\ABCUser\Desktop\AccountExpiry.ps1
At C:\Users\ABCUser\Desktop\AccountExpiry.ps1:24 char:18
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unexpected token 'math-inline">account.SamAccountName</td>
<td></span>($account.AccountExpirationDate).ToShortDateString()</td>
<td>$account.UserPrincipalName</td>
<td>$manager.Name</td>
</tr>"' in expression or statement.
+ CategoryInfo : ParserError: (:) [], ParseException
+ FullyQualifiedErrorId : UnexpectedToken
Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question. To protect privacy, user profiles for migrated questions are anonymized.
Hi,
Please replace the double quotes in <span class="math-inline"> with single quotes.
hi Ian,
thank you for the suggestion, however the script only gave the data of one user whos account is expired already and the employee is reporting to the account which was running the script.
also the formatting is an issue
this is how it looked
The following Active Directory accounts will expire within the next 10 days:\n\n<table> <thead> <tr><th>Name</th><th>SAMAccountName</th><th>Expiration Date</th><th>UserPrincipalName</th><th>Manager</th></tr></thead>
<tbody><tr>
The script should get all the accounts expires in 10 days. Check the result of the command
Search-ADAccount -AccountExpiring -UsersOnly -TimeSpan 10.00:00:00
and see if it gets only one AD account.
Also note that you need to use “=” instead of "==" in the "$expiringAccounts = = Search-ADAccount" line.
The output is exactly the same as what you added to $body. What do you expect the output to be like?