Powershell script via task scheduler not work as expected

%3CLINGO-SUB%20id%3D%22lingo-sub-860685%22%20slang%3D%22en-US%22%3EPowershell%20script%20via%20task%20scheduler%20not%20work%20as%20expected%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-860685%22%20slang%3D%22en-US%22%3E%3CP%3EHi%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20a%20script%20that%20connects%20to%20Office%20365%2C%20reads%20a%20directory%20where%20.jpg%20files%20are%20stored%20(firstname.lastname%40mydomain.com)%20and%20uploads%20each%20picture%20to%20the%20users%20profile.%3C%2FP%3E%3CP%3EEither%20if%20the%20upload%20succcessful%20or%20not%2C%20the%20script%20sends%20an%20email.%3C%2FP%3E%3CP%3EIf%20I%20schedule%20this%20task%20via%20task%20scheduler%2C%20the%20script%20runs%20successful%2C%20but%20some%20commands%20are%20not%20executed.%3C%2FP%3E%3CP%3EHere's%20the%20script.%20I've%20appended%20%3CSTRONG%3E(ne)%3C%2FSTRONG%3E%20after%20each%20line%20that%20is%20not%20being%20executed%20when%20running%20via%20task%20scheduler%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EStart-Transcript%20-Path%20%22transcript.txt%22%20-Append%20%3CSTRONG%3E(ne)%3C%2FSTRONG%3E%3CBR%20%2F%3E%24password%20%3D%20get-content%20cred.txt%20%7C%20convertto-securestring%3CBR%20%2F%3E%24credentials%20%3D%20new-object%20-typename%20System.Management.Automation.PSCredential%20-argumentlist%20%22O365.Service.EU%40mydomain.com%22%2C%24password%3C%2FP%3E%3CP%3E%24ExSession%20%3D%20New-PSSession%20-ConfigurationName%20Microsoft.Exchange%20-ConnectionUri%20%3CA%20href%3D%22https%3A%2F%2Foutlook.office365.com%2Fpowershell-liveid%2F%3Fproxymethod%3Drps%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Foutlook.office365.com%2Fpowershell-liveid%2F%3Fproxymethod%3Drps%3C%2FA%3E%20-Credential%20%24credentials%20-Authentication%20Basic%20-AllowRedirection%3CBR%20%2F%3EImport-PSSession%20%24ExSession%20-AllowClobber%3C%2FP%3E%3CP%3E%24pictureList%20%3D%20Get-Item%20%22OutlookPicture%5C*%22%3CBR%20%2F%3E%24ErrorActionPreference%20%3D%20%22Stop%22%3C%2FP%3E%3CP%3Eforeach%20(%24picture%20in%20%24pictureList)%3CBR%20%2F%3E%7B%3CBR%20%2F%3Etry%3CBR%20%2F%3E%7B%3CBR%20%2F%3E%24name%20%3D%20%24picture.Name.ToLower()%3CBR%20%2F%3E%24len%20%3D%20%24name.LastIndexOf(%22.jpg%22)%3CBR%20%2F%3E%24mailAddress%20%3D%20%24name.Substring(0%2C%24len)%20%2B%20%22%40mydomain.com%22%3CBR%20%2F%3E%24mailbox%20%3D%20Get-Mailbox%20%24mailAddress%3CBR%20%2F%3ESet-UserPhoto%20-Identity%20%24mailAddress%20-PictureData%20(%5BSystem.IO.File%5D%3A%3AReadAllBytes(%24picture.VersionInfo.FileName))%20-confirm%3A%24false%3CBR%20%2F%3ESend-MailMessage%20-From%20%22o365.service.eu%40mydomain.com%22%20-To%20stefan%40mydomain.com%20-SmtpServer%20smtp.mydomain.com%20%60%3CBR%20%2F%3E-Subject%20%22Added%20picture%20for%20%24mailAddress%22%20%3CSTRONG%3E(ne)%3C%2FSTRONG%3E%3CBR%20%2F%3E%24date%20%3D%20Get-Date%3CBR%20%2F%3E%24date2%20%3D%20%24date.ToShortDateString()%20%2B%20%22%20-%20%22%20%2B%20%24date.ToShortTimeString()%3CBR%20%2F%3E%24date2%20%2B%20%22%20-%20Uploaded%20picture%20for%20%22%20%2B%20%24mailaddress%20%7C%20log.txt%20-append%20%3CSTRONG%3E(ne)%3C%2FSTRONG%3E%3CBR%20%2F%3ERemove-Item%20%24picture.VersionInfo.FileName%3CBR%20%2F%3E%7D%3CBR%20%2F%3Ecatch%3CBR%20%2F%3E%7B%3CBR%20%2F%3Ewrite-host%20%22Error%22%3CBR%20%2F%3E%24error%5B0%5D%20%7C%20out-file%20error.log%20-append%20%3CSTRONG%3E(ne)%3C%2FSTRONG%3E%3CBR%20%2F%3ESend-MailMessage%20-From%20%22o365.service.eu%40mydomain.com%22%20-To%20stefan%40mydomain.com%20-SmtpServer%20smtp.mydomain.com%20%60%3CBR%20%2F%3E-Subject%20%22Error%20on%20uploading%20profile%20picture%22%20-Body%20%24Error%5B0%5D%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%7D%3C%2FP%3E%3CP%3EStop-Transcript%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAny%20idea%3F%3C%2FP%3E%3CP%3EThanks%3C%2FP%3E%3CP%3EStefan%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-860685%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EOffice%20365%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-860833%22%20slang%3D%22en-US%22%3ERe%3A%20Powershell%20script%20via%20task%20scheduler%20not%20work%20as%20expected%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-860833%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F288795%22%20target%3D%22_blank%22%3E%40machineslave%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIf%20the%20same%20commands%20work%20fine%20from%20normal%20powershell%20console%20and%20if%20you%20face%20issue%20only%20in%20Task%20Scheduler%2C%20then%20the%20problem%20might%20be%20the%20user%20account%20that%20you%20have%20configured%20for%20the%20schedule%20task.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20command%26nbsp%3B%3CSPAN%3ESend-MailMessage%20accepts%20the%20parameter%20%3CSTRONG%3E-Credential.%20%3C%2FSTRONG%3EIf%20you%20don't%20provide%20this%20parameter%2C%20then%20the%20command%20runs%20in%20the%20current%20user%20privilege.%20In%20your%20case%2C%20the%20account%20that%20you%20set%20in%20the%20schedule%20task%20(%26nbsp%3B%3C%2FSPAN%3ETask%20-%26gt%3B%20Properties%20-%26gt%3B%20General%20tab%20-%26gt%3B%20Security%20Option%20-%26gt%3B%20When%20running%20the%20task%2C%20use%20the%20following%20account).%3C%2FP%3E%3CP%3E%3CBR%20%2F%3EBut%20I%20would%20recommend%20you%20to%20store%20your%20credentials%20in%20Windows%20Credentials%20Manager%20and%20retrieve%20it%20using%20PowerShell%20and%20use%20it%20in%26nbsp%3BSend-MailMessage%20command.%3C%2FP%3E%3CP%3EStore%20Credentials%3C%2FP%3E%3CPRE%3EInstall-Module%20-Name%20CredentialManager%0ANew-StoredCredential%20-Target%20%22MyUserInfo%22%20-UserName%20%22username%22%20-Password%20%22mypwd%22%3C%2FPRE%3E%3CP%3ERead%20the%20stored%20user%20credential%20from%20Windows%20Credentials%20manager%3C%2FP%3E%3CPRE%3E%24psCred%20%3D%20Get-StoredCredential%20-Target%20%22MyUserInfo%22%3C%2FPRE%3E%3CP%3EYou%20can%20change%20your%20script%20as%20shown%20in%20below%20command%3C%2FP%3E%3CPRE%3ESend-MailMessage%20-From%20%22o365.service.eu%40mydomain.com%22%20-To%20stefan%40mydomain.com%20-SmtpServer%20smtp.mydomain.com%20%60%0A-Subject%20%22Added%20picture%20for%20%24mailAddress%22%20-Credential%20%24psCred%3C%2FPRE%3E%3CP%3E%3CBR%20%2F%3EFor%20this%20line%20%3A%20%3CSTRONG%3EStart-Transcript%20-Path%20%22transcript.txt%22%20-Append%20(ne)%3C%2FSTRONG%3E%2C%20provide%20complete%20path%20%3A%3C%2FP%3E%3CPRE%3EStart-Transcript%20-Path%20%22C%3A%5C%5CLogFiles%5Ctranscript.txt%22%3C%2FPRE%3E%3CP%3ESame%20for%20other%20paths%3A%3C%2FP%3E%3CPRE%3E%24date2%20%3D%20%24date.ToShortDateString()%20%2B%20%22%20-%20%22%20%2B%20%24date.ToShortTimeString()%3CBR%20%2F%3E%24date2%20%2B%20%22%20-%20Uploaded%20picture%20for%20%22%20%2B%20%24mailaddress%20%7C%20%22C%3A%5C%5CLogFiles%5Clog.txt%22%20-append%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-860856%22%20slang%3D%22en-US%22%3ERe%3A%20Powershell%20script%20via%20task%20scheduler%20not%20work%20as%20expected%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-860856%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F38365%22%20target%3D%22_blank%22%3E%40Kevin%20Morgan%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20strange%20thing%20with%20send-message%20is%20that%20if%20an%20error%20occurs%2C%20the%20send-message%20within%20the%20catch%20path%20works%20fine%20without%20using%20the%20-credential%20parameter.%3C%2FP%3E%3CP%3EFor%20the%20Start-Transcript%20and%20out-file%20I%20use%20an%20UNC%20path%20(just%20shortened%20it%20in%20the%20provided%20code).%3C%2FP%3E%3CP%3EE.g.%3C%2FP%3E%3CP%3EStart-Transcript%20-Path%20%22%5C%5Cedc-derant202%5CEmployeePic%5CScripts%5Ctranscript.txt%22%20-Append%3C%2FP%3E%3CP%3E%24date2%20%2B%20%22%20-%20Uploaded%20picture%20for%20%22%20%2B%20%24mailaddress%20%7C%20Out-File%20%5C%5Cedc-derant202%5CEmployeePic%5CScripts%5Clog.txt%20-append%3C%2FP%3E%3CBLOCKQUOTE%3E%3CHR%20%2F%3E%3C%2FBLOCKQUOTE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-860861%22%20slang%3D%22en-US%22%3ERe%3A%20Powershell%20script%20via%20task%20scheduler%20not%20work%20as%20expected%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-860861%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F288795%22%20target%3D%22_blank%22%3E%40machineslave%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESeems%20you%20have%20missed%20the%20parameter%20%3CSTRONG%3E-Body%3C%2FSTRONG%3E%20in%20your%20inner%20query.%20Just%20provide%20some%20content%20for%20-Body%20parameter.%3C%2FP%3E%3CPRE%3ESend-MailMessage%20-From%20%22o365.service.eu%40mydomain.com%22%20-To%20stefan%40mydomain.com%20-SmtpServer%20smtp.mydomain.com%20%60%0A-Subject%20%22Added%20picture%20for%20%24mailAddress%22%20-Body%20%22Added%20picture%22%3C%2FPRE%3E%3CP%3E%3CSPAN%3EFor%20the%20Start-Transcript%2C%20ensure%20that%20the%20schedule%20task%20account%20has%20valid%20permission%20(write)%20in%20the%20UNC%20path.%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-860879%22%20slang%3D%22en-US%22%3ERe%3A%20Powershell%20script%20via%20task%20scheduler%20not%20work%20as%20expected%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-860879%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F38365%22%20target%3D%22_blank%22%3E%40Kevin%20Morgan%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%20a%20lot.%3C%2FP%3E%3CP%3EJust%20added%20the%20-Body%20parameter%20and%20also%20verified%20%2F%20resolved%20the%20permissions%20of%20the%20UNC%20path.%3C%2FP%3E%3CP%3ENow%20it%20works%20properly.%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
New Contributor

Hi,

 

I have a script that connects to Office 365, reads a directory where .jpg files are stored (firstname.lastname@mydomain.com) and uploads each picture to the users profile.

Either if the upload succcessful or not, the script sends an email.

If I schedule this task via task scheduler, the script runs successful, but some commands are not executed.

Here's the script. I've appended (ne) after each line that is not being executed when running via task scheduler:

 

Start-Transcript -Path "transcript.txt" -Append (ne)
$password = get-content cred.txt | convertto-securestring
$credentials = new-object -typename System.Management.Automation.PSCredential -argumentlist "O365.Service.EU@mydomain.com",$password

$ExSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/?proxymethod=rps -Credential $credentials -Authentication Basic -AllowRedirection
Import-PSSession $ExSession -AllowClobber

$pictureList = Get-Item "OutlookPicture\*"
$ErrorActionPreference = "Stop"

foreach ($picture in $pictureList)
{
try
{
$name = $picture.Name.ToLower()
$len = $name.LastIndexOf(".jpg")
$mailAddress = $name.Substring(0,$len) + "@mydomain.com"
$mailbox = Get-Mailbox $mailAddress
Set-UserPhoto -Identity $mailAddress -PictureData ([System.IO.File]::ReadAllBytes($picture.VersionInfo.FileName)) -confirm:$false
Send-MailMessage -From "o365.service.eu@mydomain.com" -To stefan@mydomain.com -SmtpServer smtp.mydomain.com `
-Subject "Added picture for $mailAddress" (ne)
$date = Get-Date
$date2 = $date.ToShortDateString() + " - " + $date.ToShortTimeString()
$date2 + " - Uploaded picture for " + $mailaddress | log.txt -append (ne)
Remove-Item $picture.VersionInfo.FileName
}
catch
{
write-host "Error"
$error[0] | out-file error.log -append (ne)
Send-MailMessage -From "o365.service.eu@mydomain.com" -To stefan@mydomain.com -SmtpServer smtp.mydomain.com `
-Subject "Error on uploading profile picture" -Body $Error[0]
}
}

Stop-Transcript

 

Any idea?

Thanks

Stefan

 

 

4 Replies
Highlighted

@machineslave 

 

If the same commands work fine from normal powershell console and if you face issue only in Task Scheduler, then the problem might be the user account that you have configured for the schedule task.

 

The command Send-MailMessage accepts the parameter -Credential. If you don't provide this parameter, then the command runs in the current user privilege. In your case, the account that you set in the schedule task ( Task -> Properties -> General tab -> Security Option -> When running the task, use the following account).


But I would recommend you to store your credentials in Windows Credentials Manager and retrieve it using PowerShell and use it in Send-MailMessage command.

Store Credentials

Install-Module -Name CredentialManager
New-StoredCredential -Target "MyUserInfo" -UserName "username" -Password "mypwd"

Read the stored user credential from Windows Credentials manager

$psCred = Get-StoredCredential -Target "MyUserInfo"

You can change your script as shown in below command

Send-MailMessage -From "o365.service.eu@mydomain.com" -To stefan@mydomain.com -SmtpServer smtp.mydomain.com `
-Subject "Added picture for $mailAddress" -Credential $psCred


For this line : Start-Transcript -Path "transcript.txt" -Append (ne), provide complete path :

Start-Transcript -Path "C:\\LogFiles\transcript.txt"

Same for other paths:

$date2 = $date.ToShortDateString() + " - " + $date.ToShortTimeString()
$date2 + " - Uploaded picture for " + $mailaddress | "C:\\LogFiles\log.txt" -append

 

Highlighted

@Kevin Morgan 

The strange thing with send-message is that if an error occurs, the send-message within the catch path works fine without using the -credential parameter.

For the Start-Transcript and out-file I use an UNC path (just shortened it in the provided code).

E.g.

Start-Transcript -Path "\\edc-derant202\EmployeePic\Scripts\transcript.txt" -Append

$date2 + " - Uploaded picture for " + $mailaddress | Out-File \\edc-derant202\EmployeePic\Scripts\log.txt -append


Highlighted

@machineslave 

 

Seems you have missed the parameter -Body in your inner query. Just provide some content for -Body parameter.

Send-MailMessage -From "o365.service.eu@mydomain.com" -To stefan@mydomain.com -SmtpServer smtp.mydomain.com `
-Subject "Added picture for $mailAddress" -Body "Added picture"

For the Start-Transcript, ensure that the schedule task account has valid permission (write) in the UNC path.

Highlighted

@Kevin Morgan 

Thanks a lot.

Just added the -Body parameter and also verified / resolved the permissions of the UNC path.

Now it works properly.