de2rfg
Enthusiast
Enthusiast

Scan-Inventory -> Test-Compliance... Operation is not valid due to the current state of the object

Hi,

I'm working on a script to patch our single ESXi hosts. But the Scan-Inventory command gives me an error although it is executed  successfully and the host was put into maintenance mode.

...

Get-VMHost -Name $esxi | Set-VMHost -State Maintenance -Confirm:$false | Out-Null

Scan-Inventory -Verbose -Entity $esxi -UpdateType HostPatch

...

VERBOSE: 08.07.2016 09:36:42    Test-Compliance    Started execution

VERBOSE: Performing operation 'Scan Inventory' on target: 'xxxxxx'

VERBOSE: 08.07.2016 09:36:47    Test-Compliance    Finished execution

Scan-Inventory : 08.07.2016 09:36:47    Test-Compliance        Operation is not valid due to the current state of the object.  

At D:\VMware_Skripte\patch_host.ps1:51 char:1

+ Scan-Inventory -Verbose -Entity $esxi -UpdateType HostPatch

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [Test-Compliance], VimException

    + FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VumAutomation.Commands.ScanInventory

vCenter Tasks/Events

Scan entity xxxxx Completed xxxxx xxxxx 08/07/2016 09:28:55 08/07/2016 09:28:55 08/07/2016 09:29:10

Enter maintenance mode xxxxx Completed xxxxx xxxxx 08/07/2016 09:28:38 08/07/2016 09:28:38 08/07/2016 09:28:38

I don't see what the problem could be, any ideas?

Edit: Remediate also fails

Remediate-Inventory : 08.07.2016 10:16:39Update-Entity    Operation is not valid due to the current state of the object.   

At D:\VMware_Skripte\patch_host.ps1:57 char:1

+ Remediate-Inventory -Baseline $baselines -Entity $esxi -Confirm:$false

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

+ CategoryInfo      : NotSpecified: (:) [Update-Entity], VimException
+ FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VumAutomation.Commands.RemediateInventory
0 Kudos
8 Replies
LucD
Leadership
Leadership

Tried to replicate the issue, didn't succeed.

This error sometimes appears when your PowerShell session is in a funny state.

Did you try a stop/start of the PowerShell session?


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

0 Kudos
de2rfg
Enthusiast
Enthusiast

I've already rebooted the host, no difference.

0 Kudos
de2rfg
Enthusiast
Enthusiast

With ISE Test-Compliance command is working again. Now I'm back to my initial problem "Operation is not valid due to the current state of the object". Host was put in maintenance mode and the scan and remediate commands are executed but due to the error the scrip does not wait until the host was patched and rebooted,  the script immediately continues with the next code line and the host leaves maintenance mode and VM's a also restarted.

Scan-Inventory : 11.07.2016 13:15:13Test-Compliance   Operation is not valid due to the current state of the object.  

At D:\VMware_Skripte\patch_host.ps1:71 char:1

+ Scan-Inventory -Entity $esxi -UpdateType HostPatch

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

+ CategoryInfo     : NotSpecified: (:) [Test-Compliance], VimException
+ FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VumAutomation.Commands.ScanInventory

Remediate-Inventory : 11.07.2016 13:15:30Update-Entity   Operation is not valid due to the current state of the object.  

At D:\VMware_Skripte\patch_host.ps1:83 char:1

+ Remediate-Inventory -Baseline $baselines -Entity $esxi -Confirm:$false

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

+ CategoryInfo     : NotSpecified: (:) [Update-Entity], VimException
+ FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VumAutomation.Commands.RemediateInventory

# Enter Maintenance Mode

Write-Host "** Set Maintenance Mode **"

Get-VMHost -Name $esxi | Set-VMHost -State Maintenance -Confirm:$false | Out-Null

# Scan Host

Write-Host "** Scan Host **"

Scan-Inventory -Entity $esxi -UpdateType HostPatch

# Check for missing patches

Write-Host "** Missing patches before Update **"

(Get-Compliance -Entity $esxi  -Detailed).NotCompliantPatches | Select-Object Name,IDByVendor,Description,@{n='Product';e={$_.product | Select-Object -expandproperty Version}},ReleaseDate

# Get Baselines

Write-Host "** Get Baselines **"

$baselines = Get-Baseline -Entity $esxi -Inherit

# Remediate Host

Write-Host "Remediate Host **"

Remediate-Inventory -Baseline $baselines -Entity $esxi -Confirm:$false

# Leave Maintenence Mode

Write-Host "** Leave Maintenance Mode **"

Get-VMHost -Name $esxi | Set-VMHost -State Connected

0 Kudos
LucD
Leadership
Leadership

Did you already try adding a check in the code, which checks if the ESXi node is actually in the 'maintenance' state ?

Something like this for example

$esx = Get-VMHost -Name $esxi

Set-VMHost -VMHost $esx -State Maintenance -Confirm:$false | Out-Null

while($esx.State -ne [VMware.VimAutomation.ViCore.Types.V1.Host.VMHostState]::Maintenance){

    sleep 5

    $esx = Get-VMHost -Name $esxi

}

Scan-Inventory -Verbose -Entity $esx -UpdateType HostPatch


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

0 Kudos
de2rfg
Enthusiast
Enthusiast

I've added a sleep 30 before which didn't change the behaviour. But today after restarting ISE and running the script again, I get no error. Everything is working as expected. Seems to be one of the powercli problems that I saw in other threads before. This is a bit frustrating.

0 Kudos
de2rfg
Enthusiast
Enthusiast

After I've run the script a couple of times I get the error "Operation is not valid due to the current state of the object" for several commands. I'm not sure if this is related to the nested ESXi host I'm testing this with, After a remediate I go back to a snap without the current ESXi patches.

0 Kudos
LucD
Leadership
Leadership

I don't think the nested ESXi is involved, more suspect a PS/PowerCLI - vSphere issue.

I know there have been several mentions of similar issues, problem seems to be that it seems to happen intermittent.

Hard to track/pinpoint the issue for the Dev Team I guess.

Just to make sure, does a restart of your PS/PowerCLI session always get you back to a working environment?


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

0 Kudos
de2rfg
Enthusiast
Enthusiast

The problem did not happen again today. I'll let you know if a restart helps next time the problem occurs.

0 Kudos