计划任务 Powershell 脚本 - 作为用户帐户运行正常,但不能作为系统运行 [英] Scheduled Task Powershell Script - Runs OK as user account, but not as SYSTEM
问题描述
我正在尝试将 Powershell 脚本作为 SYSTEM
帐户作为计划任务运行,并且该任务运行正常,但脚本运行不正确 因为它没有加载某些 Citrix Powershell以 SYSTEM 身份运行时所需的管理单元,但以我的用户帐户运行时可以正常运行.
脚本会检查以加载 Citrix 管理单元,但当以 SYSTEM
身份运行时,这些管理单元似乎不起作用.
if ((Get-PSSnapin "Citrix.Common.Commands" -EA 静默继续) -eq $null) {试试 { Add-PSSnapin Citrix.* -ErrorAction Stop }catch { write-error "Error Citrix.* Powershell snapin";返回 }
有什么特别的我需要做才能正确加载这些管理单元吗?我正在调用这样的脚本,如果重要的话:powershell.exe -executionpolicy bypass -文件 C:\path\to\script.ps1
.
从运行 (Get-PSSnapin -registered).count 为 SYSTEM
和我的用户帐户,我可以看到管理单元已正确加载,但仍然无法弄清楚为什么脚本的行为不同.
操作系统是 Server 2016,版本 1607,这是脚本:
(输入WhoAmI
确认当前账号)
从这里您可以对特定脚本进行故障排除:
- 运行特定脚本时是否有任何错误?
- 它是否在特定命令下挂起或崩溃?
- 在用户帐户下运行它有什么区别吗?
如果脚本在 SYSTEM 窗口中运行良好,那么我将检查 Task Scheduler
中的任何错误:
- 在左侧窗格中选择
Task Scheduler Local
-Task Scheduler Library
- 在中间顶部窗格中选择您的任务
- (确保您在右侧窗格中有
Display All Task History
Enabled
) - 在中间底部窗格中检查
history
标签中的事件
I'm attempting to run a Powershell script as the SYSTEM
account as a scheduled task, and the task runs OK, but the script runs incorrectly because it does not load some Citrix Powershell snap-ins that are needed when run as SYSTEM, but runs OK as my user account.
The script does a check to load the Citrix snap-ins, but those do not appear to work when run as SYSTEM
.
if ((Get-PSSnapin "Citrix.Common.Commands" -EA silentlycontinue) -eq $null) {
try { Add-PSSnapin Citrix.* -ErrorAction Stop }
catch { write-error "Error Citrix.* Powershell snapin"; Return }
Is there anything special I need to do to get those Snap-ins loaded correctly? I'm calling the script like this, if it matters: powershell.exe -executionpolicy bypass -file C:\path\to\script.ps1
.
EDIT: From running (Get-PSSnapin -registered).count as both SYSTEM
and my user account, I can see that the snap-ins are loaded correctly, but still can't figure out why the script behaves differently.
OS is Server 2016, version 1607, this is the script: https://gist.github.com/4oo4/85cec464e123d7f2793745e662d6e7ab
This isn't the answer why your specific script doesn't work under the SYSTEM account but explains how you might troubleshoot your (or any other) PowerShell Script under the SYSTEM account.
Give a man a fish, and you feed him for a day. Teach a man to fish, and you feed him for a lifetime.
The whole thing around this, is that you can actually open a interactive PowerShell command prompt under the SYSTEM account were you probably not aware of.
Run PowerShell as SYSTEM
There are a few ways to start a interactive PowerShell command prompt but probably the easiest one is using PsExec.
Using PsExec
- Download PsTools
- Extract
PSTools.zip
and just copyPsExec
into your executable path - Run
PowerShell
as Administrator (accept theUser AccountControl
prompt) - In the PowerShell administrator window, give the command:
.\PsExec -i -s -d PowerShell
- A new
PowerShell
command window will open:
(TypeWhoAmI
to confirm the current account)
From here you can troubleshoot the specific script:
- Are there any errors when running the specific script?
- Does it hang or crash at a specific command?
- Are there any differences with running it under a user account?
If it appears that the script runs actually fine in the SYSTEM window, then I would check for any errors in the Task Scheduler
:
- Select
Task Scheduler Local
-Task Scheduler Library
in the left pane - Select your task in the middle top pane
- (Make sure you have
Display All Task History
in the right paneEnabled
) - In the middle bottom pane check the events in the
history
tab
这篇关于计划任务 Powershell 脚本 - 作为用户帐户运行正常,但不能作为系统运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!