VMware Cloud Community
BrianDGeorge
Enthusiast
Enthusiast
Jump to solution

PowerCLI IF/ELSEIF VUM

All,  I have created several POWERCLI scripts and am trying to get "fancy" by eliminating hardware specific scripts.  I have created one that has individual working components but when I try to add in a clause that states HP vs Cisco as manufacturer, it dies.  Attached is my script which returns no errors.  What am I doing wrong?

#Adds Baseline to Host, Scans Host, and the Remediates

disconnect-viserver * -confirm:$false

Connect-VIServer "$ESXIHOST" -User root -Password XXXXXXXX -WarningAction SilentlyContinue

$Manu = Get-VMHostHardware -VMHost "$ESXIHOST"| Select-object Manufacturer

if($Manu -eq 'HP') {

disconnect-viserver * -confirm:$false

$myServer = Connect-VIServer -Server XXX-vcenter1 -User administrator@vsphere.local -Password XXXXXXX

$baseline = Get-Baseline -Name 'HP Critical and Non-Critical'

Attach-Baseline -baseline $baseline -entity "$ESXIHOST"

Get-Inventory -Name "$ESXIHOST" | Scan-Inventory -UpdateType HostPatch -confirm:$false

Remediate-Inventory -baseline $baseline -entity "$ESXIHOST" -ClusterDisableHighAvailability:$true -confirm:$false}

elseif($Manu -eq 'Cisco Systems Inc') {

disconnect-viserver * -confirm:$false

$myServer = Connect-VIServer -Server XXX-vcenter1 -User administrator@vsphere.local -Password XXXXXXXX

$baseline = Get-Baseline -Name 'Cisco Critical and Non-Critical'

Attach-Baseline -baseline $baseline -entity "$ESXIHOST"

Get-Inventory -Name "$ESXIHOST" | Scan-Inventory -UpdateType HostPatch -confirm:$false

Remediate-Inventory -baseline $baseline -entity "$ESXIHOST" -ClusterDisableHighAvailability:$true -confirm:$false}

0 Kudos
1 Solution

Accepted Solutions
MKguy
Virtuoso
Virtuoso
Jump to solution

Your $Manu variable is not a string, but an object that contains a string property, so your conditional logic fails. You need to reference the string object:

PS D:\> $Manu

Manufacturer

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

HP

PS D:\> $Manu | gm

   TypeName: Selected.VMware.VimAutomation.ViCore.Impl.V1.Host.VMHostHardwareImpl

Name         MemberType   Definition

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

Equals       Method       bool Equals(System.Object obj)

GetHashCode  Method       int GetHashCode()

GetType      Method       type GetType()

ToString     Method       string ToString()

Manufacturer NoteProperty System.String Manufacturer=HP

PS D:\> $Manu.Manufacturer

HP

PS D:\> $Manu.Manufacturer | gm

   TypeName: System.String

You have 2 options:

1. Expand the property in the Select like this to get the string only varibale:

$Manu = Get-VMHostHardware -VMHost "$ESXIHOST"| Select-object -Expand Manufacturer


2. Reference the string property of the object in your conditional statements like this:

if($Manu.Manufacturer -eq 'HP') {

elseif($Manu.Manufacturer -eq 'Cisco Systems Inc') {

-- http://alpacapowered.wordpress.com

View solution in original post

0 Kudos
1 Reply
MKguy
Virtuoso
Virtuoso
Jump to solution

Your $Manu variable is not a string, but an object that contains a string property, so your conditional logic fails. You need to reference the string object:

PS D:\> $Manu

Manufacturer

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

HP

PS D:\> $Manu | gm

   TypeName: Selected.VMware.VimAutomation.ViCore.Impl.V1.Host.VMHostHardwareImpl

Name         MemberType   Definition

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

Equals       Method       bool Equals(System.Object obj)

GetHashCode  Method       int GetHashCode()

GetType      Method       type GetType()

ToString     Method       string ToString()

Manufacturer NoteProperty System.String Manufacturer=HP

PS D:\> $Manu.Manufacturer

HP

PS D:\> $Manu.Manufacturer | gm

   TypeName: System.String

You have 2 options:

1. Expand the property in the Select like this to get the string only varibale:

$Manu = Get-VMHostHardware -VMHost "$ESXIHOST"| Select-object -Expand Manufacturer


2. Reference the string property of the object in your conditional statements like this:

if($Manu.Manufacturer -eq 'HP') {

elseif($Manu.Manufacturer -eq 'Cisco Systems Inc') {

-- http://alpacapowered.wordpress.com
0 Kudos