I'm having an struggle with two scripts, the scripts makes a check on users AD values and runs perfect from Powershell as the user.
when i then add the scripts to Task Scheduler, the one script runs as intended and send a e-mail, the other script just opens a notepad with the csv file, but does not send the e-mail, it does not throw an exception on the send mail process, so i'm quite confused,
as they both work in PS but only the one works on Task Scheduler.
The working script:
# Time / Date Info
$start = [datetime]::Now
$midnight = $start.Date.AddDays(1)
$timeToMidnight = New-TimeSpan -Start $start -end $midnight.Date
$midnight2 = $start.Date.AddDays(2)
$timeToMidnight2 = New-TimeSpan -Start $start -end $midnight2.Date
try{
Import-Module ActiveDirectory -ErrorAction Stop
}
catch{
Write-Warning "Unable to load Active Directory PowerShell Module"
}
$reportTo='it@DOMAINNAME.com'
$reportFrom='IT (DO NOT REPLY) <donotreply@DOMAINNAME.com>'
$smtpServer='192.168.1.123'
$textEncoding = [System.Text.Encoding]::UTF8
$worksfolder="C:\Scripts\UserLastLogonCheck"
if ( !(test-path "$worksfolder")) {
Write-Verbose "Folder [$($worksfolder)] does not exist, creating"
new-item $worksFolder -type directory -Force
}
$ou1="OU=Users,OU=DMN,DC=DOMAINNAME,DC=com"
Get-ADUser -Filter {Enabled -eq $TRUE} -SearchBase $OU1 -Properties Name,SamAccountName,LastLogonDate | Where {($_.LastLogonDate -lt (Get-Date).AddDays(-60)) -and ($_.LastLogonDate -ne $NULL)} | Sort | Select Name,SamAccountName,LastLogonDate | export-csv C:\Scripts\UserLastLogonCheck\userslastlogondateolderthan60days.csv -nti -Encoding UTF8
$date = Get-Date
$logfile = "C:\Scripts\UserLastLogonCheck\userslastlogondateolderthan60days.csv"
if($reportTo)
{
$reportSubject = "Users with last logon older than 60 days"
$reportBody ="<font face=""calibri"">
Script run date: $date<p> Attached is list of AD users within domain: Millwardbrown.dk with last logon date older than 60 days from today.</font>"
try{
Send-Mailmessage -smtpServer $smtpServer -from $reportFrom -to $reportTo -subject $reportSubject -body $reportbody -bodyasHTML -priority High -Encoding $textEncoding -Attachments $logFile -ErrorAction Stop
}
catch{
$errorMessage = $_.Exception.Message
Write-Output $errorMessage
}
}
$stop = [datetime]::Now
$runTime = New-TimeSpan $start $stop
Write-Output "Script Runtime: $runtime"
# End
The problematic script:
# Time / Date Info
$start = [datetime]::Now
$midnight = $start.Date.AddDays(1)
$timeToMidnight = New-TimeSpan -Start $start -end $midnight.Date
$midnight2 = $start.Date.AddDays(2)
$timeToMidnight2 = New-TimeSpan -Start $start -end $midnight2.Date
try{
Import-Module ActiveDirectory -ErrorAction Stop
}
catch{
Write-Warning "Unable to load Active Directory PowerShell Module"
}
$reportTo='it@DOMAINNAME.com'
$reportFrom='IT (DO NOT REPLY) <donotreply@DOMAINNAME.com>'
$smtpServer='192.168.1.123'
$textEncoding = [System.Text.Encoding]::UTF8
$worksfolder="C:\Scripts\UserLastPasswordChangeCheck"
if ( !(test-path "$worksfolder")) {
Write-Verbose "Folder [$($worksfolder)] does not exist, creating"
new-item $worksFolder -type directory -Force
}
$ou1="OU=Users,OU=DMN,DC=DOMAINNAME,DC=com"
Get-ADUser -Filter {Enabled -eq $TRUE} -SearchBase $OU1 -Properties Name,SamAccountName,PasswordLastSet,PasswordNeverExpires | Where {($_.PasswordLastSet -lt (Get-Date).AddDays(-60)) -and ($_.PasswordLastSet -ne $NULL)} | Sort | Select Name,SamAccountName,PasswordLastSet,PasswordNeverExpires | export-csv C:\Scripts\UserLastPasswordChangeCheck\userlastpasswordchangeolderthan60days.csv -nti -Encoding UTF8
$date = Get-Date
$logfile = "C:\Scripts\UserLastPasswordChangeCheck\userlastpasswordchangeolderthan60days.csv"
if($reportTo)
{
$reportSubject = $reportSubject = "Users with last password change older than 60 days"
$reportBody ="<font face=""calibri"">
Script run date: $date<p> Attached is list of AD users within domain: Millwardbrown.dk with last password change older than 60 days from today.</font>"
try{
Send-Mailmessage -smtpServer $smtpServer -from $reportFrom -to $reportTo -subject $reportSubject -body $reportbody -bodyasHTML -priority High -Encoding $textEncoding -Attachments $logFile -ErrorAction Stop
}
catch{
$errorMessage = $_.Exception.Message
Write-Output $errorMessage
}
}
$stop = [datetime]::Now
$runTime = New-TimeSpan $start $stop
Write-Output "Script Runtime: $runtime"
# End