bansne
Enthusiast
Enthusiast

Disable system swap settings esxi

Jump to solution

Hi,

Is there a way to disable system swap setting on each esxi in vCenter Server.

i can do one by one using Esxi-->Configure-->System swap. But how to do for all in cluster.

pastedImage_1.png

i can only see option to find where it is active.

foreach($esx in Get-VMHost){

  $esxcli = Get-EsxCli -VMHost $esx

  $esxcli.sched.swap.system.get() | Select @{N='VMHost';E={$esx.Name}},HostLocalSwapActive

}

not much of use.

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership

If you can use the SSH service on each ESXi node and if you have the Posh-SSH module installed, you could do

$clusterName = 'cluster'

$user = 'root'

$pswd = 'VMware1!'

$cmd = 'services.sh restart'


$cred = New-Object -TypeName PSCredential -ArgumentList $user,(ConvertTo-SecureString -String $pswd -AsPlainText -Force)


Get-Cluster -Name $clusterName |

Get-VMHost |

ForEach-Object -Process {

    $esxcli = Get-EsxCli -VMHost $_ -V2

    $swap = @{

        datastoreenabled= $false

        hostcacheenabled = $false

        hostlocalswapenabled = $false

    }

    $esxcli.sched.swap.system.set.Invoke($swap) |

    Select @{N='VMHost';E={$esxcli.VMHost.Name}},

        @{N='Swap Change Success';E={$_}},

        @{N='Service Restart Exit Status';E={

            $oldState = Get-VMHostService -VMHost $esxcli.VMHost.Name | where{$_.Key -eq 'TSM-SSH'}

            if(-not $oldState.Running){

                Start-VMHostService -HostService $oldState -Confirm:$false

            }

      

      

            $session = New-SSHSession -ComputerName $esxcli.VMHost.Name -Credential $cred –AcceptKey -ConnectionTimeout 15

            $result = Invoke-SSHCommand -SSHSession $session -Command $cmd

            Remove-SSHSession -SSHSession $session | Out-Null

      

            if(-not $oldState.Running){

                Stop-VMHostService -HostService $oldState -Confirm:$false

            }

            $result.ExitStatus

        }}

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
7 Replies
LucD
Leadership
Leadership

Did you try like this?

$clusterName = 'cluster'

Get-Cluster -Name $clusterName |

Get-VMHost |

ForEach-Object -Process {

    $esxcli = Get-EsxCli -VMHost $_ -V2

    $swap = @{

        datastoreenabled= $false

        hostcacheenabled = $false

        hostlocalswapenabled = $false

    }

    $esxcli.sched.swap.system.set.Invoke($swap)

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

bansne
Enthusiast
Enthusiast

sure let me give this a try...

Regards

0 Kudos
bansne
Enthusiast
Enthusiast

Hi,

It worked , getting output like below , and setting is disabled on cluster Smiley Happy

true

true

true

true

true

true

true

true

true

is there way to give details like hostname and on which host its true and on which it was not applicable.

lastly is there single liner to restart services.sh on all host. after above is done.

0 Kudos
LucD
Leadership
Leadership

If you can use the SSH service on each ESXi node and if you have the Posh-SSH module installed, you could do

$clusterName = 'cluster'

$user = 'root'

$pswd = 'VMware1!'

$cmd = 'services.sh restart'


$cred = New-Object -TypeName PSCredential -ArgumentList $user,(ConvertTo-SecureString -String $pswd -AsPlainText -Force)


Get-Cluster -Name $clusterName |

Get-VMHost |

ForEach-Object -Process {

    $esxcli = Get-EsxCli -VMHost $_ -V2

    $swap = @{

        datastoreenabled= $false

        hostcacheenabled = $false

        hostlocalswapenabled = $false

    }

    $esxcli.sched.swap.system.set.Invoke($swap) |

    Select @{N='VMHost';E={$esxcli.VMHost.Name}},

        @{N='Swap Change Success';E={$_}},

        @{N='Service Restart Exit Status';E={

            $oldState = Get-VMHostService -VMHost $esxcli.VMHost.Name | where{$_.Key -eq 'TSM-SSH'}

            if(-not $oldState.Running){

                Start-VMHostService -HostService $oldState -Confirm:$false

            }

      

      

            $session = New-SSHSession -ComputerName $esxcli.VMHost.Name -Credential $cred –AcceptKey -ConnectionTimeout 15

            $result = Invoke-SSHCommand -SSHSession $session -Command $cmd

            Remove-SSHSession -SSHSession $session | Out-Null

      

            if(-not $oldState.Running){

                Stop-VMHostService -HostService $oldState -Confirm:$false

            }

            $result.ExitStatus

        }}

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
bansne
Enthusiast
Enthusiast

awsome , let me run this and get back to you Smiley Happy

Thanks alot

0 Kudos
bansne
Enthusiast
Enthusiast

thanks for all your help. Worked like charm.

Not sure after services.sh ideally it should disconnect and reconnect host but didn't do it this time. Wonder if it is actually restarting service.

0 Kudos
LucD
Leadership
Leadership

You could check in the /var/log/ jumpstart-stdout.log


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos