问题描述
我正在尝试将 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
.
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 }
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.
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
- DownloadPsTools
- 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 脚本 - 作为用户帐户运行正常,但不能作为系统运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!