Running same powershell script multiple asynchronous times with separate runspace
Posted
by
teqnomad
on Stack Overflow
See other posts from Stack Overflow
or by teqnomad
Published on 2011-01-14T00:49:17Z
Indexed on
2011/01/14
0:53 UTC
Read the original article
Hit count: 363
Hi,
I have a powershell script which is called by a batch script which is called by Trap Receiver (which also passes environment variables) (running on windows 2008).
The traps are flushed out at times in sets of 2-4 trap events, and the batch script will echo the trap details for each message to a logfile, but the powershell script on the next line of the batch script will only appear to process the first trap message (the powershell script writes to the same logfile).
My interpetation is that the defaultrunspace is common to all iterations of the script running and this is why the others appear to be ignored.
I've tried adding "-sta" when I invoke the powershell script using "powershell.exe -command", but this didn't help.
I've researched and found a method using C# but I don't know this language, and busy enough learning powershell, so hoping to find a more direct solution especially as interleaving a "wrapper" between batch and powershell will involve passing the environment variables.
http://www.codeproject.com/KB/threads/AsyncPowerShell.aspx
I've hunted through stackoverflow, and again the only question of similar vein was using C#.
Any suggestions welcome.
Some script background:
The powershell script is actually a modification of a great script found at gregorystrike website - cant post the link as I'm limited to one link but its the one for Lefthand arrays. Lots of mods so it can do multiple targets from one .ini file, taking in the environment variables, and options to run portions of the script interactively with winform. But you can see the gist of the original script.
The batch script is pretty basic. The keys things are I'm trying to filter out trap noise using the :~ operator, and I tried -sta option to see if this would compartmentalise the powershell script.
set debug=off
set CMD_LINE_ARGS="%*"
set LHIPAddress="%2"
set VARBIND8="%8"
shift
shift
shift
shift
shift
shift
shift
set CHASSIS="%9"
echo %DATE% %TIME% "Trap Received: %LHIPAddress% %CHASSIS% %VARBIND8%" >> C:\Logs\trap_out.txt
set ACTION="%VARBIND8:~39,18%"
echo %DATE% %TIME% "Action substring is %ACTION%" 2>&1 >> C:\Logs\trap_out.txt
if %ACTION%=="Remote Copy Volume" (
echo Prepostlefthand_env_v2.9 >> C:\Logs\trap_out.txt
c:\Windows\System32\WindowsPowerShell\v1.0\PowerShell.exe -sta -executionpolicy unrestricted -command " & 'C:\Scripts\prepostlefthand_env_v2.9.ps1' Backupsettings.ini ALL" 2>&1 >> C:\Logs\trap_out.txt
) ELSE (
echo %DATE% %TIME% Action substring is %ACTION% so exiting" 2>&1 >> C:\Logs\trap.out.txt
)
exit
© Stack Overflow or respective owner