Update Local Variable from Remote Variable

I got stuck with this problem, I have tried many approaches I could not find any solutions.
I want to update a local variable from within the invoke-command.

example below:
I was looking to update the local $skip variable

$skip = '0'
$install = {
if($getversion.Version -eq $null) {
    Write-Host -ForegroundColor Yellow "No agent found, installing the latest agent"
       }
            Elseif($getversion.Version -eq "0.3.9.328") {
                Write-Host -ForegroundColor Yellow "Uninstalling old agent"
            }
                Elseif ($getversion.Version -eq "0.5.3004"){
                Write-Host -ForegroundColor Green "Nagios is using the latest version..skipped"
                $skip = '1'
                Break
                }
}

Invoke-Command -ComputerName WINSERVER -scriptblock $install

The two $skip variables are in different scopes. The correct way to do this would be to have your script block return a value and have the script process the returned value:

$install = {
    If ($null -eq $getversion.Version -eq) {
        Write-Host -ForegroundColor Yellow 'No agent found, installing the latest agent'
    }
    Elseif ($getversion.Version -eq '0.3.9.328') {
        Write-Host -ForegroundColor Yellow 'Uninstalling old agent'
    }
    Elseif ($getversion.Version -eq '0.5.3004') {
        Write-Host -ForegroundColor Green 'Nagios is using the latest version..skipped'
        $skip = '1'
        $skip
    }
}

$skip = Invoke-Command -ComputerName WINSERVER -scriptblock $install
3 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.