Hello! Thank you so much for creating and maintaining/improving this script. It is used by me and I think it is one amazing script. One question - The is one line being added at the top of the CSV report which is emailed out by your script. The top of the report looks like this: #TYPE System.Object UserMessage UserName Name EmailAddress PasswordSet DaysToExpire ExpiresOn SendMail Is there a way to have the "#TYPE System.Object" removed the report? I located this info which may be of help to you: https://learn-p owershell.net/2 014/01/24/avoid ing-system-obje ct-or-similar-o utput-when-usin g-export-csv/ Thank you! Best, Chris
That looks quite convoluted to me. You can probably make this a bit prettier... $csvNew = "new-report.csv" $oldCSV = Get-Content "report.csv" $oldCSV = $oldCSV.trim("# TYPE System.Object") $oldCSV = $oldCSV | where { $_ -ne "" } New-Item $csvNew -ItemType file -Force Add-Content $csvNew $oldCSV
Can you tell me where in the latest version of your script I would need to place those lines? Thank you!
I did see other comments that this issue has happened to others, I don't know If I saw an answer. I can run the script from a PowerShell console, it sends email to users with expiring accounts, but when using it with task scheduler, it logs the email as "skipped interval" I am calling from a batch file, using this syntax cd C:\Pscript powershell.exe -file C:\Pscript\PasswordChangeNotif ication.ps1 -smtpServer [FQDN of internal server name] -expireInDays 10 -from "Password Change <help@mydomain. com>" -Logging -LogPath "c:\SMTPFiles" -reportTo report@mydomsin .com -interval 0,1,2,5,6,7,8,1 0 any comments to what is my error would assist greatly, thank you in advance
The only time I saw "Skipped interval" was when my user was within the 10 days but was on day 4 which is not specified in the -interval command. The log file should show you this. Also when you run from the TS that its running as the same user that was used through the other steps. Below is what is in my task scheduler, I had to remove the "" and replace them with '' inside the string while keeping the "" on the whole string. -command "C:\scripts\PasswordChangeNoti fication.ps1 -smtpServer smtp.mail.serve r.example -expireInDays 10 -from 'IT Support <support@mydoma in.com>' -Logging -LogPath 'c:\logFiles' -interval 1,2,3,5,7,10"
I think that may have solved it, the sending address was a group, I have changed it to a user that can be added to the TS, I have run the script from explorer, it would fail this way prior. thank you
Ran script and it is only finding 1 user object. I then tried running it as administrator and now it shows 28 user objects. However this domain has 100+ user accounts. It is also saying 0 users to notify when a manual report shows users will expire tomorrow. Thank you for your assistance.
Answering my own question.. Basically the script was working fine I had a misconception about the company 28 user objects were correct and so was the 0 notifications for my set time of 15 days. However I will share some new info I was able to change the smtp port for so this would work on an Azure server. Added in the Port parameter and moved the other parameters down. param( # $smtpServer Enter Your S MTP Server Host name or IP Addr ess [Parameter( Mandatory=$True ,Position=0)] [ValidateNo tNull()] [string]$sm tpServer, # $Port Ent er Your SMTP Se rver port number [Parameter( Mandatory=$True ,Position=1)] [ValidateNo tNull()] [string]$Po rt, # Notify Us ers if Expiry L ess than X Days [Parameter( Mandatory=$True ,Position=2)] [ValidateNo tNull()] [int]$expir eInDays, # From Addr ess, eg "IT Sup port <support@d omain.com>" [Parameter( Mandatory=$True ,Position=3)] ect. res of the Param( section is the same just the Position numbers moved up one. With this change the following command works. Powershell.exe -ExecutionPolic y Bypass c:\PasswordExpi re\PasswordChan geNotification. ps1 -smtpServer smtp.srvr.com -Port 2500 -expireInDays 15 -from support@techguy IT.com
I apologize if this has been answered before, but I did not see it posted here. I am unable to get the send mail function to work correctly. I've made a few different modifications, but I continue to get this error even with the 'stock' script downloaded from here. "An invalid character was found in the mail header: '<'." I can't seem to find which '<' is causing the send mail piece to have issues. Have you seen this before? Thanks!
Please disregard. I was simply missing the trailing '>' on the From address...
Hi! Just wanted to let you know that AWS doesnt' allow to modify default domain policy in their provisioned Windows AD. Instead they only give you permissions to administer only one OU so the Get-ADDefaultDomainPasswordPol icy always returns 42 days. Apparently they have something blocking the Get-AduserResul tantPasswordPol icy too! despite the domain level is windows2012r2 so I had to work around that and came to a solution like that: $defaultMaxPass wordAge = [convert]::ToIn t32((net accounts | ForEach-Object { if ($_ -match "^Maximum password age \(days\):\s+(\d +)$"){$Matches. 1}}),10) this always returns the correct policy set by the domain group policy applied to the OU server in. that also works great on the domain level windows2008
Hi Robert | I have configured your script and everything are working normally but my leader would like to customize your script must to CC. manager of user also. Could you recommend me or not ? Thanks.
Yes someone else asked that in the comments below, you need to capture the users manager from AD then get their email address and add that to the -cc
I have tried several time but not able to use this script properly do you have some video to understand?
https://www.youtube.com/user/r obtitlerequired Yes, all the videos for version 2.
Hello I will like to know how to pass parameters to script to send the report file to more then one person. I am using this -reportto FirstEmail@domain.com;SecoundE mail@domain.com but doesn't work.
Im fairly sure it is a comma separated list that Send-Mailmessage supports, not semi colon. However the parameter is expecting a string, so you may need to enclose your list inside quotes. -to "recipient1@dom ain.com,recipie nt2@domain2.com "
Hi Gary , at the bototm pf the script $reportBody = "Password Expiry Report Attached" try{ Send-Mailmessage -smtpServer $smtpServer -from $from -to $reportTo.Split (",") -subject $reportSubject -body $reportbody -bodyasHTML -priority High -Encoding $textEncoding -Attachments $logFile -ErrorAction Stop } catch{ $errorMessage = $_.Exception.Me ssage Write-Output $errorMessage } change $reportTo to $reportTo.Split (",") and add recipients by comma separated values on your task scheduler arguments like this : "C:\scripts\Pas swordChangeNoti fication.ps1 -smtpServer <your.smtp.serv er> -expireInDays 11 -from 'your@mail.com' -Logging -LogPath 'c:\logFiles' -reportTo 'rec1@domain.co m,rec2d@domain. com,rec3@domain .com' -interval 1,2,5,10" had the same issue and it is now solved. Robert if possible ammend that for your next version.
Hi I've been able to get the script running explicitly running from powershell itself and have followed the youtube guide for this. However when I try to execute my script with my custom AD user (everything is hosted on the domain controller) it gives me access denied. Task Scheduler failed to start instance "{instance id}" of "\Password expiry email" task for user "domain\passwordreminderuser" . Additional Data: Error Value: 2147942405. AND Task Scheduler failed to launch action "C:\Windows\Sys tem32\WindowsPo werShell\v1.0\" in instance "{instance id}" of task "\Password expiry email". Additional Data: Error Value: 2147942405. For my user it didn't matter if I have it set to logon as batch, logon as local service and/or allow logon locally. In addition I added it to domain\administ rators Group thinking that might be preventing it from executing Powershell however it does not work. 1) Under general: Run whether user is logged on or not and with Highest priveledges 2) General is set to run with my created passwordreminde ruser already with correct password entered. 3) I set the task settings to stop the existing instance of the task if its already running See imgur album of additional details including script i'm trying to execute https://imgur.c om/a/xJtmcp0 I've tried numerous things such as using my domain admin to execute the script and it keeps failing with the same error, running it as logged on user. Only thing I haven't done yet is reboot the server. And logs of course do not save when using task schedule.
Why is there an & at the begining of your command? Program/Script should just be 'powershell.exe' Please review, https://www.you tube.com/watch? v=xbzxWOarVuk
Hi thanks for the update and for continually working with users who implement your script. I've reverted my modifications and now have it matching your syntax. However when I go to execute it, the task completes successfully with the last run result being 0x1
Hi yes it works its way through the task scheduler process however when it executes it gives: Task Scheduler successfully completed task "\Password expirey email" , instance "{54455454-e18d-4a7c-82da-4906 0c7d08cf}" , action "powershell.exe " with return code 1.
I think I resolved it, looks like it might've been failing to execute due to execution policy settings. As per: https://stackoverflow.com/ques tions/13015245/ powershell-scri pt-wont-execute -as-a-windows-s cheduled-task -noninteractive -nologo -Command "&'D:\IT\Script s\PasswordChang eNotification.p s1' -smtpServer .mail.protectio n.outlook.com -expireInDays 21 -from 'IT Support <support@domain .com>' -reportTo email@domain.co m -interval 1,2,5,10,15 -testing -testrecipient account@clientd omain.com -logging -logPath 'D:\IT\Scripts\ Log Files'"
The script works fine. When running from a command via Powershell. But the same via the Task Scheduler won't fire the e-mail. And it does domething since the log file is updated. It's just the mail that is not being send as soon as I start it up via the Scheduler. Should I use -Command or -File ? -File "C:\admin\PasswordChangeNotifi cation.ps1" -expireInDays 21 -Logging -testing -testRecipient xxx@xxx.com -interval 1,3,7,8 The other parameters are in the PS1. But the default also works the same way. Works via PowerShell, also Scheduler, except the mail.