clamorte
Contributor
Contributor

Newbie question about get-vm.

Jump to solution

I'm just getting started with the tool kit and am trying to put together a script for our VDI environment. I have a pretty basic question that so far is stumping me. I need to dump out a list of all guest on each of my ESX hosted attached to my VC server.

This sounds easy and I'm sure it is when I try to specify the target VIServer with the Get-VM command it keeps balking at me saying the server name is incorrect. I ran Get-VMHost just to ensure I was typing the ESX server name correctly but to no avail. If I do a Get-ESX instead of a Get-VC the Get-VM works for the specific server I did the Get-ESX to but I was hoping I could just do a Get-VM with my VC credentials and dump the results into a variable for each ESX host.

Any help would be greatly appreciated! Smiley Happy

0 Kudos
1 Solution

Accepted Solutions
hugopeeters
Hot Shot
Hot Shot

I made a mistake with the previous example. Here's a better one:

Connect-VIServer "YourVC.domain.local"
Get-VMHost "ESXServer.domain.local" | Get-VM

To loop through all ESX servers:

Connect-VIServer "YourVC.domain.local"
ForEach ($VMHost in Get-VMHost)
{
Write-Host "========="
Write-Host $VMHost.Name
Write-Host "========="
 $VMHost | Get-VM
}

Message was edited by: hugopeeters: man I must be getting tired

View solution in original post

0 Kudos
5 Replies
esarakaitis
Enthusiast
Enthusiast

sorry, but VERY hard to follow what the exact results your looking for are.... try this

$clusters = get-cluster

foreach ($cluster in $clusters)
{
    foreach ($vmhost in ($cluster | get-vmhost))
    {
        $vmhost | get-vm | Select-Object @{Name="Cluster"; Expression={$cluster.name}},`
            Name
    }
}

http://www.vmwarescripting.com

hugopeeters
Hot Shot
Hot Shot

Does this not work?

$VC = Connect-VIServer "yourVCserver.domain.local"
ForEach ($VMHost in Get-VMHost)
{
   Write-Host "==========="
   Write-Host $VMHost.Name
   Get-VM | Format-Table Name
}

clamorte
Contributor
Contributor

I'm really just looking to get a dump of all the guests running on a particular ESX server after connecting using "Get-VC". If I connect via "Get-ESX" to the host directly of course I'll get the list of guest just for that ESX server using "Get-VM". I just want to filter the results of "Get-VM" to return just the Guests on a specific ESX Host without having to specifically bind to that ESX server.

Esarakaits,

Thanks I'll give your suggestion a shot!

0 Kudos
hugopeeters
Hot Shot
Hot Shot

I made a mistake with the previous example. Here's a better one:

Connect-VIServer "YourVC.domain.local"
Get-VMHost "ESXServer.domain.local" | Get-VM

To loop through all ESX servers:

Connect-VIServer "YourVC.domain.local"
ForEach ($VMHost in Get-VMHost)
{
Write-Host "========="
Write-Host $VMHost.Name
Write-Host "========="
 $VMHost | Get-VM
}

Message was edited by: hugopeeters: man I must be getting tired

0 Kudos
clamorte
Contributor
Contributor

Huqopeeters

Spot-on this is exactly what I needed!! Works like a charm.

I'm both new to the VI-Toolkit and Powershell so forgive my ignorance.

Thank you all for the suggestions and feedback.

:smileygrin:

0 Kudos