Using Invoke-Command -ScriptBlock on a function with arguments

后端 未结 3 991
遥遥无期
遥遥无期 2020-11-29 22:08

I\'m writing a PowerShell script that will execute commands on a remote host using Invoke-Command and its -ScriptBlock parameter. For example,

         


        
3条回答
  •  -上瘾入骨i
    2020-11-29 22:13

    This also works:

    function foo
    {
        param([string]$hosts, [string]$commands)
        $scriptblock = $executioncontext.invokecommand.NewScriptBlock($commands)
        $hosts.split(",") |% { Invoke-Command -Credential $cred -ComputerName $_.trim() -Scriptblock $scriptblock }
    }
    

提交回复
热议问题