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

Add-PSSnapin Citrix.*if it already exist why do you care? it will just ignore them, also runing test on my machine with citrix servers:(Get-PSSnapin -Registered).Counthas the same count with regular admin or with system account user, so it should'nt make a diffferent