VMware Cloud Community
mgraveney
Contributor
Contributor

how come this doesn't work?

I'm a bit confused, all I want to do is do an if statement only if vmware tools are installed, so i thought i'd check and see if Guest.ToolsStatus is set to toolsNotInstalled, however switching the get-vm between TestVM_01 which hasn't got tools and another VM with the tools it aways says "Do This".

I've been looking at this for too long and am probably missing something obvious, can anyone help out here?

$vm = get-vm TestVM_01
$vminfo = $vm | Get-View

Write-host $vminfo.Guest.ToolsStatus

$test = $vminfo.Guest.ToolsStatus | Out-String

Write-host $test

if ($test -notcontains "toolsNotInstalled"){
    write-host "Do This"
}
else{
    write-host "Do That"
}

Reply
0 Kudos
2 Replies
bulletprooffool
Champion
Champion

$vminfo  =  get-vm TestVM_01 | get-view

if ($vminfo.Guest.ToolsStatus -contains "toolsNotInstalled")

     {write-host "yes"}

One day I will virtualise myself . . .
Reply
0 Kudos
LucD
Leadership
Leadership

The reason why you have the problem is because of the Out-String cmdlet.

It takes the content of the toolsStatus property, which is an enumeration of the type VirtualMachineToolsStatus, and converts it to a string.

But it will also add a <CR><LF> to the end. It is an Out- cmdlet after all.

Due to these 2 extra characters the comparison will not work.

Just remove the Out-String cmdlet and your script will be ok.


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

Reply
0 Kudos