如何在远程VM的并行度上执行一组Powershell命令

时间:2019-12-19 07:01:08

标签: windows powershell virtual-machine powershell-remoting powershell-workflow

我们使用的是Powershell 5,而远程计算机是Windows 2016服务器

$computers = @("VMserverA","VMserverB")
     $Session=  New-PSWorkflowSession -Name MyWorkflow -ComputerName $computers -Credential $cred -ThrottleLimit 150
workflow MyWorkflow
{
  foreach -parallel -throttlelimit 10 ($computer in $computers)
  {
    "Starting working on $computer"
        if(Test-Connection -ComputerName $computer){
        $res= Invoke-Command -Session $Session -ScriptBlock {
        Get-Host
        $env:computername
        }
        $res

        }
    "Finished working on $computer"
   # Remove-PSSession -Session $Session
  }
}
MyWorkflow

但是它的抛出错误为

At line:22 char:15
+         $res= Invoke-Command -Session $Session -ScriptBlock {
+               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Cannot call the 'Invoke-Command' command. Other commands from this module have
been packaged as workflow activities, but this command was specifically
excluded. This is likely because the command requires an interactive Windows
PowerShell session, or has behavior not suited for workflows. To run this
command anyway, place it within an inline-script (InlineScript {
Invoke-Command }) where it will be invoked in isolation.
    + CategoryInfo          : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : CommandActivityExcluded
MyWorkflow : The term 'MyWorkflow' is not recognized as the name of a cmdlet,
function, script file, or operable program. Check the spelling of the name, or
if a path was included, verify that the path is correct and try again.
At line:41 char:1
+ MyWorkflow
+ ~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (MyWorkflow:String) [], CommandN
   otFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

,但我们希望一次(并行)在每个远程服务器上复制项目。主机和远程服务器是Windows Server 2016,PowerShell版本是5。

我们编写了用于执行并行命令的powershell脚本

workflow DoSomeWork {
parallel{
Get-Process -PSComputerName "serverA"
    Get-Process -PSComputerName "serverB"
    "parallel block"
}
  $sets= "serverA","serverB"
  "parallel end"
  ForEach -parallel -throttlelimit 10 ($item in $sets)
    {
sequence{
 "foreach block begin for $item"
    Get-Process -PSComputerName $item
  "foreach block end for $item"
    }
  }
}
DoSomeWork

0 个答案:

没有答案