Strange behaviour, when using powershell Start-Process on local computer with credentials

0

I have task in windows scheduler. It runs powershell script ScriptScheduler.ps1 under Domain\User1. This script run another one GenerateAndApplyACLforMultipleProjects.ps1 with credential Domain\User2. When i am run it interactively under Domain\User1, everything ok. But with schedule i am see process started and nothing happend. Transcribe file for second script not created. The only thing i am get,event 26 in the application log, error about powershell start 0xc0000142.

GenerateAndApplyACLforMultipleProjects.ps1 start code:

Start-Process -FilePath C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ArgumentList -WindowStyle Hidden -NonInteractive  -ExecutionPolicy Bypass –Noprofile  -file "C:\DATA\ProjectServices\SetProjectPermissions\SCRIPTS\GenerateAndApplyACLforMultipleProjects.ps1" -Verb RunAs -Credential $Credentials -PassThru
Transcript ScriptScheduler.ps1

**********************
Windows PowerShell transcript start
Start time: 20200422212501
Username: Domain\User1
RunAs User: Domain\User1
Machine: TEST-PC (Microsoft Windows NT 6.1.7601 Service Pack 1)
Host Application: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -Noninteractive -ExecutionPolicy Bypass –Noprofile -file C:\DATA\Projects\ScriptScheduer\SCRIPTS\ScriptScheduler.ps1
Process ID: 5980
PSVersion: 5.1.14409.1018
PSEdition: Desktop
PSCompatibleVersions: 1.0, 2.0, 3.0, 4.0, 5.0, 5.1.14409.1018
BuildVersion: 10.0.14409.1018
CLRVersion: 4.0.30319.42000
WSManStackVersion: 3.0
PSRemotingProtocolVersion: 2.3
SerializationVersion: 1.1.0.1
**********************
Transcript started, output file is C:\DATA\Projects\ScriptScheduler\LOGS\Transcript.log
Logging initialized.
Script scheduler started.
Run script @{Name=GenerateAndApplyACLforMultipleProjects; Path=C:\DATA\ProjectServices\SetProjectPermissions\SCRIPTS\GenerateAndApplyACLforMultipleProjects.ps1; UserName=Domain\User2; Credentials=System.Management.Automation.PSCredential; LogFile=C:\DATA\Projects\ScriptScheduler\LOGS\Daily.log; Evaluate=True; WorkDir=C:\DATA\ProjectServices\SetProjectPermissions\SCRIPTS} with evaluate.

GenerateAndApplyACLforMultipleProjects

Handles NPM(K) PM(K) WS(K) CPU(s)   Id SI ProcessName
------- ------ ----- ----- ------   -- -- -----------
             0     0     0        5592


Processing results.
Seconds run: [2.446404], task count: [1], task name: [GenerateAndApplyACLforMultipleProjects] errors count: [0].
Script scheduler exited.
PS>$global:?
True
**********************
Windows PowerShell transcript end
End time: 20200422212503
**********************  
powershell
credentials
start-process
asked on Stack Overflow Apr 22, 2020 by Alex

1 Answer

0

This is ...

Start-Process -FilePath C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ArgumentList -WindowStyle Hidden -NonInteractive  -ExecutionPolicy Bypass –Noprofile  -file "C:\DATA\ProjectServices\SetProjectPermissions\SCRIPTS\GenerateAndApplyACLforMultipleProjects.ps1" -Verb RunAs -Credential $Credentials -PassThru

... is the wrong way to run a scheduled task. Unless $credentials are already in memory when you run this, $credentials are empty.

Running PowerShell commands and scripts via ST is a well documented and regularly used use case.

You can use the ST GUI to set this up and ensure you select the right identity to use, or use PowerShell to create the ST. [Video]

Docs ---

New-Scheduled Task - docs.microsoft.com

Set-Scheduled Task - docs.microsoft.com

answered on Stack Overflow Apr 22, 2020 by postanote

User contributions licensed under CC BY-SA 3.0