PowerShell script running as different user in Windows Task Scheduler
NickName:Jeff Fol Ask DateTime:2017-10-11T05:34:59

PowerShell script running as different user in Windows Task Scheduler

I am trying to configure a PowerShell script which will run in Windows Task Scheduler for multiple service accounts. I found a way to run the PowerShell script with a given user via the 2nd answer in this link Running PowerShell as another user, and launching a script.

For reference this is the PowerShell snippet:

$username = 'userA'
$password = 'passwordA'

$securePassword = ConvertTo-SecureString $password -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential $username, $securePassword
Start-Process C:\BatchPath\MyBatch.bat -Credential $credential

Now in Windows Task Scheduler I configure the job and in the execution I have it setup to run Powershell.exe with additional arguments '-ExecutionPolicy Bypass C:\ScriptPath\Script.ps1'. If the user for this task scheduler entry is configured as userA then it works. If I configure the task scheduler entry on userB then it fails. Both userA and userB are administrators on the machine.

In the second scenario I would expect that the script file would be started by userB but then the PowerShell Start-Process would force the batch file to be run as userA. From watching Task Manager I don't see the job within the batch file started.

This example is a bit superficial but in the final form of the PowerShell script it would be running different batch files with different service accounts.

Copyright Notice:Content Author:「Jeff Fol」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/46676391/powershell-script-running-as-different-user-in-windows-task-scheduler

More about “PowerShell script running as different user in Windows Task Scheduler” related questions

PowerShell script running as different user in Windows Task Scheduler

I am trying to configure a PowerShell script which will run in Windows Task Scheduler for multiple service accounts. I found a way to run the PowerShell script with a given user via the 2nd answer...

Show Detail

Issue with windows task scheduler with running a powershell script in windows 2008 server

This question would seem a very common question but i have done every possible thing for the solution but nothing works for me. I m trying to run a powershell script via batch powershell.exe c:\

Show Detail

Running Windows Powershell script always from task scheduler

This might be a very basic question. I have FileWatcher script in windows powershell which I want to run always so that it keeps watching a particular location for files. when I run it from Windows

Show Detail

Issue Running PowerShell on Windows Task Scheduler

I'm trying to run a PowerShell command via Task Scheduler but keep getting the below result when the task tries to run PowerShell on my Windows Server 2008 R2 environment. action "powershe...

Show Detail

PowerShell Script not running when set in the task scheduler

I'm attempting to create a task via powershell to delete some files older then 6 hours, if I execute the script from powershell there are no issues, if I try to execute from task scheduler nothing

Show Detail

Powershell and task scheduler

I am having a strange issue with task scheduler on my Win 7 machine not properly running a Powershell script which is supposed to email me a PDF file daily. If I run the script manually via ISE......

Show Detail

Windows Task Scheduler not starting program from Powershell script

I'm very new to powershell and server management. I've created a simple powershell script that stops and starts a program. This script works without issues when I run it manually but when Windows...

Show Detail

Windows Task Scheduler not running task despite trigger

please keep in mind throughout this that I am quite new to windows automation and powerShell so go easy on me please. My goal was to create an automatic USB cloner in order to copy files if the dri...

Show Detail

Logfile not being generated while running a powershell script with windows task scheduler

I have created the following powershell script: stop-service ''3456'' start-sleep -s 60 stop-service "2354" start-sleep -s 60 Restart-computer QY34 -Force send -mailmessag

Show Detail

Windows Task Scheduler not returning $LASTEXITCODE from a Powershell script

I have a script that just calls Exit 1. But, when this script is run via Windows Task Scheduler, I get either a return code of 0, or 2147942401, but I never get the expected 1. Details below. Script

Show Detail