How do I use here-string for ScriptBlock variable?

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of GS GS 1 year, 7 months ago.

  • Author
    Posts
  • #28868
    Profile photo of GS
    GS
    Participant

    Can not figure out why I'm getting error below on line 16

    + GetComputerName "localhost"
    + ~
    Missing '=' operator after key in hash literal.

    workflow GetComputerName
    {
    param ([String[]] $computerNames)
    $scriptblock = @{
    $result= [pscustomobject]@{"MachineName" = $env:ComputerName}
    Get-Date
    }

    foreach -parallel ($computername in $computerNames)
    {
    $workflow:out += inlinescript {Invoke-Command -ComputerName $computername -ScriptBlock ([scriptblock]::Create($using:scriptBlock))}
    }

    }

    GetComputerName "localhost"

  • #28870
    Profile photo of Tim Curwick
    Tim Curwick
    Participant

    GS,

    It's complaining because
    $scriptblock = @{
    should be
    $scriptblock = {

    But try something like this this instead.

    workflow GetComputerName
        {
        Param ( [String[]] $ComputerNames )
    
        ForEach -Parallel ($ComputerName in $ComputerNames)
            {
            InlineScript
                {
                [pscustomobject]@{ "MachineName" = $env:ComputerName }
    
                } -PSComputerName $ComputerName
            }
        }
    
    GetComputerName "localhost"
    

    You should also add a -ThrottleLimit 100 (or whatever) to your ForEach -Parallel. Otherwise, if you accidentally grab 5000 server names from AD and try to launch a separate thread for each, you are going to crash your computer.

  • #28873
    Profile photo of GS
    GS
    Participant

    Thanks, so InlineScript with -PsComputerName is acting like Invoke-Command equivalent in workflow?

You must be logged in to reply to this topic.