VMware Cloud Community
Gsanty
Contributor
Contributor

Compare Variable Value and get result as True or False

I'm trying to compare the $banner string value with esxi advancesetting "Annotations.WelcomeMessage" value.

If "Annotations.WelcomeMessage" value is equal to $banner value. Expecting a result as True else False.

But below code always gives me as false as output eventhough $banner value is set on ESXI. Please help what i'm missing


$Banner = "This computer system, its network and data contained therein is the property of ..... Access to this computer and network is restricted to persons and programs authorised by the Group only.
Access by others is prohibited and unauthorised, and is wrongful under law. Do not proceed if you are not authorised. Any unauthorised access will be prosecuted to the fullest extent of the law."

$report = @()
ForEach ($ESXHost in $vmhost ){

         $ESXiBannermsg = get-vmhost $esxhost | Get-AdvancedSetting -Name "Annotations.WelcomeMessage"
             
          if($ESXiBannermsg.value -eq '$banner'){          
                $ESXiBanner = "True"
                }
          
          else{
                $ESXiBanner = "False"
                }
          
          $obj = “” | select “Host Name”,Syslogdir,Sysloghost,ESXitimeout,Banner
          $obj.“Host Name” = $ESXHost.Name
          $obj.Banner = $ESXiBanner

$report += $obj
} $report

0 Kudos
3 Replies
LucD
Leadership
Leadership

This is most probably due to the fact that in Windows lines are separated with <CR><LF>, while in Linux they are with <LF>.
You should be able to see this with the Format-Hex cmdlet.

$Banner = "This computer system, its network and data contained therein is the property of ..... Access to this computer and network is restricted to persons and programs authorised by the Group only.

Access by others is prohibited and unauthorised, and is wrongful under law. Do not proceed if you are not authorised. Any unauthorised access will be prosecuted to the fullest extent of the law."


$value = (Get-VMHost -Name MyEsx | Get-AdvancedSetting -Name "Annotations.WelcomeMessage").Value


$Banner | Format-Hex

$value | Format-Hex

Due to this the comparison will never return $true

$Banner -eq $value

You can take this into account in your comparison, and replace the <CR><LF>  with a <LF> for the comparison.

($Banner -replace "`r`n", "`n") -eq $value


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

0 Kudos
Dimon_SPB
Contributor
Contributor

replace '$banner' to "$banner"

and may by use

if($ESXiBannermsg.value -like "$banner")

0 Kudos
Gsanty
Contributor
Contributor

LucD,
Thanks. It worked.
0 Kudos