简体   繁体   English

在单独的运行空间中多次异步运行同一Powershell脚本

[英]Running same powershell script multiple asynchronous times with separate runspace

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). 我有一个Powershell脚本,该脚本由批处理脚本调用,该脚本由Trap Receiver调用(还传递环境变量)(在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). 有时会在2-4个陷阱事件的集合中清除陷阱,并且批处理脚本会将每条消息的陷阱详细信息回显到日志文件,但是批处理脚本下一行的powershell脚本只会显示为处理第一个陷阱消息(powershell脚本写入同一日志文件)。

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. 我尝试使用“ powershell.exe -command”调用powershell脚本时添加“ -sta”,但这无济于事。

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. 我已经研究并找到了一种使用C#的方法,但是我不了解这种语言,并且忙于学习powershell,因此希望找到一个更直接的解决方案,尤其是因为在批处理和powershell之间插入“包装”将涉及传递环境变量。

http://www.codeproject.com/KB/threads/AsyncPowerShell.aspx http://www.codeproject.com/KB/threads/AsyncPowerShell.aspx

I've hunted through stackoverflow, and again the only question of similar vein was using C#. 我已经通过stackoverflow进行了搜索,同样,唯一类似的问题是使用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. Powershell脚本实际上是对gregorystrike网站上的一个出色脚本的修改-无法发布该链接,因为我仅限于一个链接,但它只能用于Lefthand数组。 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. 大量的mod,因此它可以从一个.ini文件中执行多个目标,并接收环境变量以及用于与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. 关键的事情是我正在尝试使用:〜运算符过滤掉陷阱噪声,并且尝试了-sta选项以查看这是否会分隔Powershell脚本。

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

I have absolutely no clue what you're trying to do, and to be honest, I don't even understand the question. 我完全不知道您要做什么,老实说,我什至不明白这个问题。 However, if you're trying to run several instances of the same script asynchronously in different runspaces (for reasons only you know best, sorry) then I can point you to a script I wrote some time ago. 但是,如果您试图在不同的运行空间中异步运行同一脚本的多个实例(仅出于某种原因,很抱歉,对不起),那么我可以将您指向我之前编写的脚本。 This is for v2 ctp3 but works fine in v2 RTM (which is current): 这适用于v2 ctp3,但在v2 RTM(当前)中可以正常工作:

http://www.nivot.org/2009/01/22/CTP3TheRunspaceFactoryAndPowerShellAccelerators.aspx http://www.nivot.org/2009/01/22/CTP3TheRunspaceFactoryAndPowerShellAccelerators.aspx

Hope this helps, and good luck in whatever the hell it is you're doing :) 希望这会有所帮助,无论您在做什么,都祝您好运:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM