Quantcast
Channel: VMware Communities: Message List
Viewing all articles
Browse latest Browse all 258290

Re: Execute SSH command to multiple ESXi Hosts in Parallel

$
0
0

Thanks Luc,

 

I got what needed to work but I decided I wanted to loop through network bridges since there might be more than one bridge on a single host, im using a hash table to map the bridge to a host but the code just doesn't work, here is an example.

 

Remove-Variable * -Force -ErrorAction SilentlyContinue

$esxCreds = Get-Credential

$Bridges = ('vmbrg01', 'vmbrg02')

$code = {

    param(

        [string]$Bridge,

        [System.Management.Automation.PSCredential]$esxCreds,

        [string]$BridgeNodeMappings

      

    )

    $BridgeNodeMappings = @{

        "vmbrg01" = "edge-10", "ocdn1-r1a-n01.local"

        "vmbrg02" = "edge-13", "ocdn1-r1a-n03.local"

    }

 

    $edgeID = $BridgeNodeMappings.$Bridge[0]

    $EsxHost = $BridgeNodeMappings.$Bridge[1]

 

 

    $ssh = New-SSHSession -ComputerName $EsxHost -Credential $esxCreds -AcceptKey -KeepAliveInterval 5 -Force

 

    $hostname = (Invoke-SSHCommand $ssh -Command 'hostname').Output

    $HostDate = (Invoke-SSHCommand $ssh -Command 'date').Output

    $PSDate = Get-Date

 

    $prop = [ordered]@{

        Server    = $EsxHost

        Hostname  = $hostname

        Hostdate  = $hostdate

        PSDate    = $PSDate

        DummyData = "Hello-$EsxHost"

        BrigeID   = $edgeID

    }

  

    New-Object -Type PSObject -Property $prop

    Sleep -Seconds 2

 

    $ssh | Remove-SSHSession | Out-Null

}

 

$jobs = @()

 

 

ForEach ($Bridge in $Bridges) {

    $jobs += Start-Job -ScriptBlock $code -Name "SSH Job" -ArgumentList $Bridge, $esxCreds, $BridgeNodeMappings

}

 

Wait-Job -Job $jobs

Receive-Job -Job $jobs


Viewing all articles
Browse latest Browse all 258290

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>