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.
There are a few ways to start a interactive PowerShell command prompt but probably the easiest one is using PsExec.
Using PsExec
PSTools.zip
and just copy PsExec
into your executable pathPowerShell
as Administrator (accept the User AccountControl
prompt).\PsExec -i -s -d PowerShell
PowerShell
command window will open:WhoAmI
to confirm the current account) From here you can troubleshoot the specific script:
If it appears that the script runs actually fine in the SYSTEM window, then I would check for any errors in the Task Scheduler
:
Task Scheduler Local
- Task Scheduler Library
in the left paneDisplay All Task History
in the right pane Enabled
)history
tab