See below:
$vhost = "10.16.14.25"
function get-vmcount
{
$script:myvmcount = get-vmhost $vhost | Select @{N=“NumVM“;E={($_ | Get-VM).Count}}
#write-host "Total VM's on host $vhost = $myvmcount"
$myvmcount
}
get-vmcount
write-host "VM= $myvmcount"
all I get in the output is:
VM= @{NumVM=3}
Why?
It’s exactly what you’re asking for. What output are you expecting to get?
Not sure what benefit you are getting wrapping it in a function, but to answer your question you are returning an object and referencing the entire object versus a property in the object:
$vhost = “10.16.14.25”
$myvmcount = Get-VMHost-Name $vhost |
Select @{N=“NumVM“;E={($_ | Get-VM).Count}}
“VM Count = {0}" -f $myvmcount.NumVM
#or
“VM Count = {0}" -f $myvmcount | Select -ExpandProperty NumVM
Only had a function so I can reuse in other parts of a huge script. And I understand your explanation, had an idea it was something like that.
Just to be able to print out “The total number of VM’s on host HOST” = 3 (which is the actual number of VM’s on the host). I can do that.
Thank you.
This way it is even simpler…
[pre]
$vhost = “10.16.14.25”
$myvmcount = (Get-VM -ComputerName $vhost).Count
[pscustomobject]@{ VMCount = $myvmcount }
[/pre]
Thank you.
Olaf
July 20, 2020, 8:57pm
6
If you’re looking for simpler I’d recommend this snippet:
$vhost = '10.16.14.25'
[pscustomobject]@{
VMCount = (Get-VM -ComputerName $vhost).Count
}
[quote quote=243842]If you’re looking for simpler I’d recommend this snippet:
$vhost = ‘10.16.14.25’
[pscustomobject]@{
VMCount = (Get-VM -ComputerName $vhost).Count
}
[/quote]
Olaf- I get the error "a parameter cannot be found that matches parameter name ‘ComputerName’. Verified code I entered to yours.
If you’re looking for simpler I’d recommend this snippet:
$vhost = ‘10.16.14.25’
[pscustomobject]@{
VMCount = (Get-VM -ComputerName $vhost).Count
}
Olaf- I get the error “a parameter cannot be found that matches parameter name ‘ComputerName’. Verified code I entered to yours.
Also I am using VMware for virtualization
Okay, if it isn’t Hyper-V then what Rob suggested is the ideal way, but still, it can be in this way as well…
[pre]
$vhost = “10.16.14.25”
[pscustomobject]@{ VMCount = (Get-VMHost -Name $vhost | Get-VM).Count }
[/pre]
Thank you.
[quote quote=243935]Okay, if it isn’t Hyper-V then what Rob suggested is the ideal way, but still, it can be in this way as well…
$vhost = “10.16.14.25” [pscustomobject]@{ VMCount = (Get-VMHost -Name $vhost | Get-VM).Count } Thank you.[/quote]
Well, that works, sorta. I would like to print a line like “Toatal VM’s =” xx. Which is the variable I use, VMCount? Anyway after I incorporated this line in my code the command does not output anything until I exit the script. Maybe out-host? I had this issue before but damned if I remember how I fixed it.
…Alan
Like this?
$vhost = “10.16.14.25”
$obj = [pscustomobject]@{ VMCount = (Get-VMHost $vhost | Get-VM).Count}
Write-Host "Total VM's = $($obj.VMCount)"
$obj
What is the point of wrapping this into an object? It can simply be a variable:
$vhost = “10.16.14.25”
$VMCount = (Get-VMHost $vhost | Get-VM).Count
Write-Host "Total VM's = $VMCount"
Agreed if it’s just to write to the screen.