3

I wrote a PS1 script that will count how many processes (in this case MS Project) are running on a Citrix farm.

The code it's:

$variable=(query farm /process | where { $_.Contains("WINPROJ") }  | measure ).count
echo $variable

If I execute the PS1 from powershell, it works flawlessly but If I invoke it from cmd (for schedule it) using powershell.exe -file "C:\foo\countProcessProject.ps1" or powershell.exe -c "C:\foo\countProcessProject.ps1" the script will stop until I press a button multiple times, outputting the variable.

I've compared environment variables on CMD and PS execution and are almost the same.

Greets,

4 Answers4

1

You might be able to force it to return using the -NonInteractive argument with powershell:

PowerShell.exe -File "C:\foo\countProcessProject.ps1" -NonInteractive

While I have no insightful explanation as to why this would be necessary, here is how I would work around this issue:

Without PowerShell:

If you insist on using the archaeological gem of query farm, this can be done using cmd alone - no need to complicate a simple task:

query farm /process |find /i "winproj" /c

This will output the number "winproj" processes much in the same way as your current powershell invocation statement

Using just PowerShell:

If you are keen on moving on to a more PowerShell-centric solution, you can use the PS Snapins included in the offical XenApp PowerShell SDK from Citrix:

Add-PSSnapin Citrix.XenApp.Commands
$FarmProcs = Get-XAServer -OnlineOnly | Get-XASessionProcess
$MSProjProcs = $FarmProcs | Where {ProcessName -match "winproj"} | Measure-Object
return $MSProjProcs.Count
Mathias R. Jessen
  • 24,907
  • 4
  • 62
  • 95
  • It works, and I love the use of the powershell cmdlets (I didn't know about the XA PS SDK... now on bookmarks for future scripts). – vfondevilla Jan 21 '14 at 08:42
  • The XenApp cmdlets are not super versatile, but they work and they ease the pain of wrapping commandline management. Glad to hear it works :) – Mathias R. Jessen Jan 21 '14 at 08:53
0

I'll in first place try to put this :

PowerShell.exe -file "C:\foo\countProcessProject.ps1"

In a batch file and then create a Schedule Task that runs this batch which will run your Powershell script.

Assuming you've not already tried this.

NooJ
  • 194
  • 1
  • 2
  • 13
  • I tried again calling the script via bat file & task scheduler, same result. Until I press any keyboard button it keeps freezed – vfondevilla Jan 17 '14 at 15:31
0

Does it work when you use the start /b command to spawn powershell inside the cmd console.

start /b /wait Powershell.exe -file "C:\foo\countProcessProject.ps1"

Can you confirm you are not clicking anywhere on the screen during the script enabling the click pause as described here.

Knuckle-Dragger
  • 366
  • 1
  • 2
  • 13
0

I'm still learning PowerScript and don't have the points to comment above, but have been looking at this and have some questions. Hope you will indulge me.

First I can create a simple test file and run it from cmd like this:

$v = (dir | Where-Object { $_.name.Contains("test") } | measure).count
echo $v

I run this in cmd with:

powershell -file test.ps1

It takes a moment but then returns 1 because my file is test.ps1


What is the 'query farm /process' ? Is this an external command? And what does it return? I think the pipe following it is expecting an array, is it not? I'm wondering what happens to errors when you run from cmd. I do know that a 2> command line suffix null will cleanly get rid of any error.

Elliptical view
  • 684
  • 6
  • 9
  • So I had a little time to think about this, and if you get the chance please run the script I wrote above as a test. It will be interesting if it too has the same trouble, because it does not have that trouble here. – Elliptical view Jan 18 '14 at 02:52
  • query farm /process it's a Citrix command for list all XenApp's farm processes. The script you've wrote works without problems from CMD and also from task scheduler – vfondevilla Jan 20 '14 at 10:53
  • humm? Then there appears to be something strange about query farm /process, like it's stopping and asking for standard input or something, or a batch file, or checking some env variable, or something strange. ?? – Elliptical view Jan 20 '14 at 13:37
  • It seems, but in a Powershell windows the .PS1 runs without problem. I'll try on another approach – vfondevilla Jan 20 '14 at 14:33