Good morning People of the Powershell!
My question isn't as simple as it sounds. To "pre-disable" users in our environment, we run a script which changes their password, removes their group memberships, gives someone fullaccess to their mailbox, and sets up an out of office
message for their email. The script is crafted by a program I created, and we have it run at a set time with Task Scheduler. In addition to the above tasks, it runs another set of cmdlets the results of which are then emailed to us and serve to
let us know whether the script was successful. So for example, we change the password and then get the results of passwordlastset. We remove their group memberships, then get their group memberships. Set the out of office, get the out of
office.
We've just started doing this and for the most part it has worked wonderfully. But on two occasions, the password failed to be changed. It makes no sense to me and I'm hoping that someone here will have a suggestion.
Here's an example of the cmdlet: Set-ADAccountPassword -Identity DBLACKLE -Reset -NewPassword (ConvertTo-SecureString -AsPlainText "Phoenix4" -Force)
On both of these two occasions, in an attempt to determine what went wrong I ran the passwordlastset cmdlet again to verify I got the same results. I did. I would then copy/paste the exact cmdlet from the script into Powershell and it would complete
successfully. So I don't believe it's a time-out issue nor a problem with the cmdlet syntax or with the chosen password - if it was, it would fail when I ran it later. If it matters, this is the first cmdlet in the script - it runs right after
with import-ad and whatnot.
It's certainly possible, though unlikely, that the user is logged in at the time this is run, but I don't think that would cause it to fail?
I'm hoping someone here will have experience with this particular cmdlet and will know what if any common issues there might be. And/or I'm open to suggestions on how to capture the results of this particular cmdlet. When I set this up I wanted
to capture results for each cmdlet but for some reason that didn't work out; that's why I run all the "gets" later and stuff them into a txt file.
Here's the full text of the script in case it's useful:
import-module activedirectory
Add-PSSnapin Quest.ActiveRoles.ADManagement
New-PSDrive -name H -psprovider FileSystem -root \\napvmfsr01\users\dcarnes\virtualgray\
Set-ADAccountPassword -Identity DBLACKLE -Reset -NewPassword (ConvertTo-SecureString -AsPlainText "Phoenix4" -Force)
Add-MailboxPermission -Identity 'CN=Blackl\, Donald L.,ou=Phoenix,ou=QBUsers,dc=na,dc=qb,dc=llp' -User 'NA\JBRACCIA' -AccessRights 'FullAccess'
Set-MailboxAutoReplyConfiguration DBLACKLE -AutoReplyState enabled -ExternalAudience all -InternalMessage "This is an automated reply: ___ no longer practices law at ___. ___'s messages will be monitored by his former assistant, J B, for 30 days (5/30/14 until 6/30/14). Messages related to client matters will be forwarded to ___, Phoenix Chair of the Corporate Services group. If you would like your message or any other matter addressed by another ___ attorney, please contact either that attorney directly or contact ___ at ___. Thank you." -ExternalMessage "You get the idea."
Get-Content \\napvmfsr01\users\dcarnes\virtualgray\oktodeleteDBLACKLE.txt | ForEach-Object {
Remove-AdGroupMember -Identity $_ -Member DBLACKLE -Confirm:$false
}
get-aduser -identity DBLACKLE -properties passwordlastset | select passwordlastset | out-file \\napvmfsr01\users\dcarnes\virtualgray\ResultsDBLACKLE.txt
Get-MailboxPermission -Identity DBLACKLE -User "JBRACCIA" | out-file -Append \\napvmfsr01\users\dcarnes\virtualgray\ResultsDBLACKLE.txt
Get-ADPrincipalGroupMembership DBLACKLE | Select Name | out-file -Append \\napvmfsr01\users\dcarnes\virtualgray\ResultsDBLACKLE.txt
Get-MailboxAutoReplyConfiguration DBLACKLE | select internalmessage | format-table -wrap -autosize | out-file -Append \\napvmfsr01\users\dcarnes\virtualgray\ResultsDBLACKLE.txt
function sendMail{
Write-Host "Sending Email"
#SMTP server name
$smtpServer = "___"
#Creating a Mail object
$msg = new-object Net.Mail.MailMessage
#Creating SMTP server object
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
#Email structure
$msg.From = "___"
$msg.ReplyTo = "___"
$msg.To.Add("___")
$msg.subject = "Pre-Disable Results for DBLACKLE"
$msg.body = Get-Content \\napvmfsr01\users\dcarnes\virtualgray\ResultsDBLACKLE.txt
#Sending email
$smtp.Send($msg)
}
#Calling function
sendMail