VMware Cloud Community
tdubb123
Expert
Expert

install-vmhost patch

Would this work with install-vmhost patch?

$vmhost = get-vmhost -name (get-content esx.txt)

foreach ($esx in $vmhost)

{

set-vmhost -vmhost $esx -state maintenance

Install-VMHostPatch -VMHost $esx -HostPath /vmfs/volumes/datastore/ESXi600-201905001/metadata.zip

}

would the host automatically reboot after the install-vmhostpatch and set itself back into connected state or will it stay in maintenance mode?

Do I need a wait loop?

0 Kudos
28 Replies
LucD
Leadership
Leadership

Did those jobs end?
Can you do a Receive-Job on them?


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

0 Kudos
tdubb123
Expert
Expert

I am using following script

$code = {

   param(

   [string]$EsxName,

   [string]$Server,

   [string]$SessionId

   )

   Connect-VIServer -Server $Server -Session $SessionId

   $esx = Get-VMHost -Name $EsxName

   #Set-VMHost -VMHost $esx -State Maintenance -Evacuate -Confirm:$false

   Install-VMHostPatch -VMHost $esx -HostPath '/vmfs/volumes/xxxxxxx/metadata.zip'

   Restart-VMHost -VMHost $esx -Confirm:$false

  

   Set-VMHost -VMHost $esx -State Connected -Confirm:$false | out-null

}

$jobs = Get-Content -Path esxhosts.txt |

ForEach-Object -Process {

   Start-Job -Name EsxPatch -ScriptBlock $code -ArgumentList $_, $global:DefaultVIServer.name, $global:DefaultVIServer.SessionId

}

get-job | wait-job

get-job | Receive-Job -Job $jobs -Wait

but the host only stays in maintenance mode and script never terminates. its stuck after rebooting

0 Kudos
LucD
Leadership
Leadership

Did you try building in a loop after the Restart-VMHost to wait till the ESXi is reachable again?


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

0 Kudos
tdubb123
Expert
Expert

Hi

Still cant get this to work. The jobs started but nothing is going on

$code = {

   param(

  [string]$EsxName,

  [string]$Server,

  [string]$SessionId

  )

   Connect-VIServer -Server $server -Session $SessionId

   $esx = get-vmhost -name $esxname

   $baseline = Get-Baseline -server $server -name *Critical*

   $baseline | Attach-Baseline -entity $esx -Confirm:$false

   Set-VMHost -VMHost $esx -State Maintenance -Evacuate -Confirm:$false

  Scan-inventory -entity $esx

  sleep 30

   $baseline | Remediate-Inventory -entity $esx -Confirm:$false

   Restart-VMHost -VMHost $esx -Confirm:$false

   Set-VMHost -VMHost $esx -State Connected -Confirm:$false | out-null

}

$computers = get-content "c:\scripts\esxhosts.txt"

foreach ($esxname in $computers){

start-job -name esxpatch -scriptblock $code -argumentlist $esxname, $global:DefaultVIServer.name, $global:DefaultVIServer.SessionId

}

get-job | wait-job

get-job | receive-job

0 Kudos
LucD
Leadership
Leadership

Can you add a Start-Transcript and Stop-Transcript to the job?
And add the Verbose switch on the PowerCLI cmdlets in the job.


That might give some clues.


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

0 Kudos
tdubb123
Expert
Expert

this is from the capture

**********************

Windows PowerShell transcript start

Start time: 20190718085832

Username: xxxxxx

RunAs User: xxxxxx

Configuration Name:

Machine: xxxxxxx (Microsoft Windows NT 10.0.16299.0)

Host Application: C:\windows\system32\WindowsPowerShell\v1.0\PowerShell_ISE.exe

Process ID: 10828

PSVersion: 5.1.16299.1146

PSEdition: Desktop

PSCompatibleVersions: 1.0, 2.0, 3.0, 4.0, 5.0, 5.1.16299.1146

BuildVersion: 10.0.16299.1146

CLRVersion: 4.0.30319.42000

WSManStackVersion: 3.0

PSRemotingProtocolVersion: 2.3

SerializationVersion: 1.1.0.1

**********************

Transcript started, output file is c:\scripts\cap.txt

Id     Name            PSJobTypeName   State         HasMoreData     Location             Command

--     ----            -------------   -----         -----------     --------             -------

33     esxpatch        BackgroundJob   Running       True            localhost            ...

35     esxpatch        BackgroundJob   Running       True            localhost  

0 Kudos
LucD
Leadership
Leadership

That looks as if you added the Start-Transcript to the script that starts the jobs.

I meant to add it to the background jibs, inside the code block.


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

0 Kudos
tdubb123
Expert
Expert

Thanks

looks like its working now. but host is still in maintenance mode

i dont see anything in the transcript

----------------------------------------------------------------------------------------------

$code = {

    param(

    [string]$EsxName,

    [string]$Server,

    [string]$SessionId

    )

    Set-PowerCLIConfiguration -DisplayDeprecationWarnings $false -Confirm:$false | Out-Null

    import-module VMware.VimAutomation.Core

    import-module VMware.VumAutomation

    Start-Transcript -Path "c:\scripts\cap.txt" -Confirm:$false

    Connect-VIServer -Server $server -Session $SessionId

    $esx = get-vmhost -name $esxname

    $baseline = Get-Baseline -server $server -name *Critical*

    $baseline | Attach-Baseline -entity $esxname -Confirm:$false

    #Set-VMHost -VMHost $esx -State Maintenance -Evacuate -Confirm:$false

    Scan-inventory -entity $esxname

    sleep 30

    $baseline | Remediate-Inventory -entity $esxname -Confirm:$false

    Restart-VMHost -VMHost $esxname -confirm:$false

    Set-VMHost -VMHost $esxname -State Connected -Confirm:$false | out-null

    Stop-Transcript

    }

$computers = get-content "c:\scripts\esxhosts.txt"

foreach ($esxname in $computers)

{

start-job -name esxpatch -scriptblock $code -argumentlist $esxname, $global:DefaultVIServer.name, $global:DefaultVIServer.SessionId

}

get-job | wait-job

get-job | receive-job

0 Kudos
LucD
Leadership
Leadership

One more thing to try:

  • do the Restart-VMhost with the RunAsync option
  • in a loop, wait till the host is actually unreachable
  • now  start a new loop where you wait till the host is back
  • then do the Set-VMHost to get out of maintenance mode

Something along these lines

# Wait for reboot to start

while(Test-Connection -ComputerName $esxName -Quiet -Count 1){

  sleep 2

}


# Wait till node is back

while(-not (Test-Connection -ComputerName $esxName -Quiet -Count 1)){

  sleep 2

}


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

0 Kudos