I'm using this simplified PowerShell v1 script to collect some server statistics. The script is called form an external monitoring server (Zabbix). The collection of these statistics can take longer than 30 seconds (Zabbix timeout), so I am starting a second instance of the same script to do the gathering of data. Using this technique, the first instance of the script finishes within the Zabbix timeout, and the data gathering still takes place in the background:
Param([string] $action)
$path = "\\SERVER\share\"
$file = $path + "test.txt"
switch ($action) {
'collect' {
Write-Host "collecting data, this will take a while..."
# pause the script to simulate processing time:
Start-Sleep -s 10
# for testing, create an empty testfile:
New-Item -ItemType File $file | Out-Null
break
}
'execute' {
$script = $path + "ForkedExecute.ps1 collect"
$vars = "-ExecutionPolicy ByPass"
$out = $path + "stdout.txt"
$err = $path + "stderr.txt"
# without the -Redirect[...] options, the testfile is not created:
# Start-Process $pshome\powershell.exe -ArgumentList "& `"\$script`" $vars"
# the testfile is successfully created:
Start-Process $pshome\powershell.exe -ArgumentList "& `"\$script`" $vars" -RedirectStandardOutput $out -RedirectStandardError $err
break
}
default {
Write-Host "error - no action defined"
}
}
I now face the problem that without the -Redirect[...]
options, the second instance of the script is started, but somehow fails to create the testfile. With the -Redirect[...]
options in place, the testfile is created, but the first instance of the script has to wait for the second instance to be finished and will timeout eventually.
How can I fix this script, so that the 'collect' part is successfully run and the script does not timeout?