VMware Cloud Community
Tigerstolly
Enthusiast
Enthusiast
Jump to solution

Why isn't this declare variable working ?

I'm trying to but the build of a host host (read in from $dest) into a variable called $desthostbuild.

It outputs to screen correctly returning this

Build : 143128

but $desthostbuild remains empty.

I'm hoping to use this as part of a script that will move a vm to a host and then upgrade the tools if the build number of the host is higher than the build number of the tools in the guest.

What am i doing wrong ?

function Get-HostBuild {

get-vmhost $dest | % { $server = $_ | get-view; $server.Config.Product | select Build}

}

$desthostbuild = (Get-HostBuild | fl *)

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

I think it will be easier if you create a similar function for getting the Tools version for a guest.

Since both functions return a string you can just as well use the -eq operator

Something like this for example

function Get-HostBuild {
	param ([string]$hostname)
	(Get-VMHost $hostname | Get-View).Config.Product.Build
}

function Get-ToolsVersion {
	param ([string]$vmname)
	(get-vm $vmname | get-view).config.tools.toolsVersion
}

$dest = <host-name>
$a = get-hostbuild $dest
$vm = <VM-name>
$b = get-toolsversion $vm
if ($a -eq $b) {Write-Host "Tools version OK"}
else {Write-Host "Tools need upgrade"}


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

View solution in original post

0 Kudos
5 Replies
LucD
Leadership
Leadership
Jump to solution

The Select cmdlet returns a PSCustomObject not just the string with the build number.

You can use the Get-Member cmdlet to verify this.

This is a possible alternative.

Use a parameter in the function instead of relying on a variable you defined in the main function.

The build number is the value that the function will return.

function Get-HostBuild {
	param ([string]$hostname)
	(Get-VMHost $hostname | Get-View).Config.Product.Build
}

$dest = <host-name>
get-hostbuild $dest


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

0 Kudos
Tigerstolly
Enthusiast
Enthusiast
Jump to solution

hmm interesting i'll give this a go, thanks again for your prompt reply !

0 Kudos
Tigerstolly
Enthusiast
Enthusiast
Jump to solution

I'm probably doing something silly to please bear with me Smiley Wink

OK so i can get the build version with that, and i can get the tools version with this

get-vm $vm | % { get-view $_.ID } | select Name, @{ Name="ToolsVersion"; Expression={$_.config.tools.toolsVersion}} . Although this doesn't return just the number currently, but i'm sure i can figure that bit out.

If i can get them both in a variable for example $a for the tools version and $b for the host version its easy for me to compare them like this

$d = $a.CompareTo($b) which will handily return a -1 if $a is less than $b. I'm sure i can then do something to either run the tools version, or not.

Or are you saying this is entirely the wrong approach ? It just seems the logical way of doing it !

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I think it will be easier if you create a similar function for getting the Tools version for a guest.

Since both functions return a string you can just as well use the -eq operator

Something like this for example

function Get-HostBuild {
	param ([string]$hostname)
	(Get-VMHost $hostname | Get-View).Config.Product.Build
}

function Get-ToolsVersion {
	param ([string]$vmname)
	(get-vm $vmname | get-view).config.tools.toolsVersion
}

$dest = <host-name>
$a = get-hostbuild $dest
$vm = <VM-name>
$b = get-toolsversion $vm
if ($a -eq $b) {Write-Host "Tools version OK"}
else {Write-Host "Tools need upgrade"}


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

0 Kudos
Tigerstolly
Enthusiast
Enthusiast
Jump to solution

that works like a charm, thanks for your help !

0 Kudos