PowerShell:Start-Job -scriptblock多行脚本块? [英] PowerShell: Start-Job -scriptblock Multi line scriptblocks?

查看:120
本文介绍了PowerShell:Start-Job -scriptblock多行脚本块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望有人可以帮助我弄清楚这是否可行.我有20条ish行,可以在while循环中扫描日志文件.我需要将其与脚本的其余部分并行进行.日志扫描循环正在将条目传递到SQLite中,其他脚本需要对此信息采取行动-因此希望并行运行它们.

Hoping someone can help me to figure out if this is possible. I have a 20 ish lines that scan a log file in a while loop. I need this to happen in parallel with the rest of the script. The log scanning loop is passing the entries into SQLite and other scripts need to act on this information - hence wanting to run them in parallel.

如果我使用Job-Start命令,那么-SciptBlock函数似乎只会接受一条管道命令.我要传递的命令太多,因此我需要在脚本块中运行多行.

If i use the Job-Start command then it seems the -SciptBlock function will only accept one piped line of commands. I have too many commands to want to pipe so i need to run multiple lines in the scriptblock.

我尝试了几种方法,但是以下示例给出的错误最少.我也像第二个示例一样,在Invoke-Command -ScriptBlock -as作业中尝试了这两种方法-两种方式都不接受多行脚本块.

I tried several ways of doing it but the following examples give the least errors. I also tried it in an Invoke-Command -ScriptBlock -as job like in the second example - both ways will not accept a multiline scriptblock.

请问我在做什么错?

Start-Job -Name LogScan -ScriptBlock
{
  $EventOld = ConvertFrom-Json (Get-content ( Get-ChildItem  | Sort-Object -Property LastWriteTime | Select-Object -last 1 ) | Select-Object -last 1) 
  $RunLoop = 1
  while (RunLoop -ge 1)
    {
    Start-Sleep -Milliseconds 333
    $RunLoop = $RunLoop +1
    $EventNew = ConvertFrom-Json (Get-content ( Get-ChildItem  | Sort-Object -Property LastWriteTime | Select-Object -last 1 ) | Select-Object -last 1)
    if ($EventOld.timestamp -ne $EventNew.timestamp)
      {
         # lots of commands and here passing the array to SQLite
      }
    $EventOld = $EventNew
  }
}

错误如下:

Start-Job : Missing an argument for parameter 'ScriptBlock'. 
Specify a parameter of type 'System.Management.Automation.ScriptBlock' 
and try again. [..]

推荐答案

briantist 表示敬意,以帮助我到达那里:)

Kudos to briantist for helping me to get there :)

使用Start-JobInvoke-Command时,请注意-ScriptBlock参数的开头{与命令放在同一行.不要像使用ifwhile命令那样将其放在下面的行中.

When using Start-Job and Invoke-Command, be careful to put the opening { for the -ScriptBlock parameter on the same line as the command. Do not put it on the line below as you would with an if or while command.

这会导致进一步的问题,即没有发现您没有正确匹配打开{和关闭}括号,因为您习惯于匹配其缩进级别以将它们配对.

This leads to a the further issue of not spotting that you have not matched opening { and closing } brackets properly, as you are used to the convention of matching their indentation level to pair them up.

请注意,代码中对$Event.timestamp的引用.这些来自以下事实:一个JSON字段称为timestamp-它不是标准字符串或数组的方法或属性.

Please note that the references to $Event.timestamp in the code. These come from the fact that one of the JSON fields is called timestamp - it is not a method or property of a standard string or array.

这篇关于PowerShell:Start-Job -scriptblock多行脚本块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆