1

I'm using PowerShell and trying to update a list variable that is located outside of my invoke-command command and so far I haven't figure out how to get it to work. The "$USING:vCompliant_target_srvr" value in the body of the command has a red squiggly and the message "The assignment expression is not valid. The input to an assignment operator must be an object that is able to accept assignments..."

$target_servers = "Test1","Test2","Test3"
$vCompliant_target_srvr = ""

foreach($target_server in $target_servers){
$Response = Invoke-Command -ComputerName $target_server -ScriptBlock {
    $srvPSV = $PSVersionTable.PSVersion
    if(!$srvPSV -lt [Version]'5.1'){
        Return @{
            # neither of the below works
            vCompliant_target_srvr = vCompliant_target_srvr + ,$USING:target_server

            vCompliant_target_srvr += vCompliant_target_srvr
        }
    } else {
        write-host "This DOES NOT meet requirements" $USING:target_server
    }
}
$Response.GetEnumerator() | %{
    Set-Variable $_.Key -Value $_.Value
}
}

$vCompliant_target_srvr

I need to $vCompliant_target_srvr to be global because I want to use it and it's values after the invoke-command runs. I have googled, searched the MS PowerShell site, and searched stack overflow, but I've been unable to figure out how to make it work. Thanks!

M_66
  • 299
  • 5
  • 19
  • 1
    You cannot set variables in the caller's scope from a remotely executing script block. Your only option is to _output_ values and assign them to a variable. – mklement0 May 06 '20 at 21:57
  • I attempted to implement your solution, but I'm unable to add to the list for vCompliant_target_srvr. When this block of code runs, I should have a list of servers associated with vCompliant_target_srvr. Using the example in the other post overwrites the value and I end up with a list of a single server name. I've edited my code above with your recommendation. Can you provide additional recommendation? – M_66 May 06 '20 at 22:23
  • `[array] $vCompliant_target_srvr = Invoke-Command ...` should do, combined with `return $USING:target_server`. If you have further questions, please create a _new_ question post. – mklement0 May 06 '20 at 22:41

0 Answers0