首页 > 解决方案 > 在具有凭据的本地计算机上使用 powershell Start-Process 时出现奇怪的行为

问题描述

我在 Windows 调度程序中有任务。它在 Domain\User1 下运行 powershell 脚本 ScriptScheduler.ps1。此脚本使用凭据 Domain\User2 运行另一个 GenerateAndApplyACLforMultipleProjects.ps1。当我在 Domain\User1 下以交互方式运行它时,一切正常。但是按照计划,我看到进程开始了,但什么也没发生。未创建第二个脚本的转录文件。我唯一得到的,应用程序日志中的事件 26,关于 powershell 启动 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
**********************  

标签: powershellcredentialsstart-process

解决方案


这是 ...

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

... 是运行计划任务的错误方式。除非 $credentials 在您运行时已经在内存中,否则 $credentials 是空的。

通过 ST 运行 PowerShell 命令和脚本是一个有据可查且经常使用的用例。

您可以使用 ST GUI 进行设置并确保选择正确的身份来使用,或使用 PowerShell 创建 ST。[视频]

文档---

新计划任务 - docs.microsoft.com

设置计划任务 - docs.microsoft.com


推荐阅读