powershell - 在具有凭据的本地计算机上使用 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
**********************
解决方案
这是 ...
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 GUI 进行设置并确保选择正确的身份来使用,或使用 PowerShell 创建 ST。[视频]
文档---
推荐阅读
- php - 通过将标题列名称与另一个表中的行值匹配来获取值
- c# - 从 Outlook VSTO 中的 JunkFolder 修改“阻止的电子邮件发件人”列表
- powershell - 为什么 For-Each 循环在 PowerShell 中不起作用?
- python - 如何在 tensorflow.keras 模型指标中使用 sklearn AUC?
- nagios - 是否可以将服务的执行限制在特定的时间范围内?
- python - 如何从一些 python 脚本中调用函数,这些函数的名称可以在另一个 python 脚本中更改?
- python - 可以直接将 xpath 复制并粘贴到漂亮的汤解析器中,还是必须对其进行修改?
- php - Issue in using multiple databases in laravel 5.2
- javascript - 如何使用leaflet.js(附截图)实现以下地图功能?
- bash - 如何在 Linux 中循环访问变量值?