I have a script that I've run a few times before using Citrix snapins. It runs just fine in the console, generally taking around 20 minutes. When I try to run it as a scheduled task, it never completes.
The script is as follows:
$time1 = get-date Add-PSSnapIn Citrix.Common.Commands Add-PSSnapin Citrix.XenApp.Commands $_csv = @() $_csv_header = """Date"",""Sessions"",""IdleSessions"",""ForcedDCSEssions""" $_csv += $_csv_header $count = 0 $idlesessions = get-xasession -Full | where {$_.browsername -eq "IDM Search" -and $_.state -ne "Listening" -and $_.state -ne "Disconnected" -and $_.SessionName -ne "Console" -and ($_.currenttime - $_.Lastinputtime).minutes -gt 15} $sessions = get-xasession -Full | where {$_.browsername -eq "IDM Search" -and $_.state -ne "Listening" -and $_.state -ne "Disconnected" -and $_.SessionName -ne "Console"} if ($sessions.count -gt 400) { foreach($idlesession in $idlesessions){ stop-xasession -SessionId $idlesession.sessionid -ServerName $idlesession.servername $count++ } } $sessioncount = $sessions.count $idlecount = $idlesessions.count $date = get-date $_csv += """$date"",""$sessioncount"",""$idlecount"",""$count""" $time2 = get-date $time3 = $time2 - $time1 $duration = $time3.TotalMinutes $_csv += """$duration""" $date = get-date -format 'dd.MM.yyyy.hh.mm' $_csv | out-file c:\powershell\logs\ImageDC$date.csv -Encoding ASCII
The Task Scheduler settings are as follows:
General tab: It runs with local and domain admin privs whether the user is logged in or not
Triggers tab: It begins at 7:55 AM every morning, repeating every 30 minutes for 12 hours (enabled is checked)
Actions tab: Start Program = %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe Add arguments = –noninteractive -nologo -noprofile -noexit C:\powershell\ScheduledIDMDisconnect.ps1 start in = c:\powershell
Conditions tab: Only two things checked are "Start the task only if the computer is on AC power" and "Stop if the computer switches to battery power" (those are the defaults, IIRC)
Settings tab: "Allow task to be run on demand" is checked. "Stop the task if it runs longer than (4 hours)" is checked. "If the running task does not end when requested, force it to stop" is checked. "If the task is already running, then the following rule applies: (Run a new instance in parallel)" is at the bottom.
Can anyone offer any insight? Thanks!
zarberg@gmail.com