jamie20
Enthusiast
Enthusiast

Exclude vms in Get-View

Jump to solution

Hi guys,

I customized a VM Inventory script for my requirement by coping some of the LucD scripts :smileymischief:. Its working fine. But now my doubt is I need to exclude the vms named "replica". But I dont  know where to use where-object cmdlet.

Below is my script:

$vms =  Get-View -ViewType VirtualMachine -Property Name,runtime.powerState,Guest.hostname,Guest.net,Config.Hardware.numCPU,Summary.Storage.Committed

,Summary.Storage.UnCommitted,Config.Hardware.MemoryMB,Runtime.Host,Guest.GuestFullName,Parent,ResourcePool

,Config.Hardware.Device,Config.version,guest.toolsversionstatus

foreach($vm in $vms)

{

    $t = Get-View $vm.ResourcePool -Property Name,Parent

    $datacenter = $t.Name

    $vm.Config.Hardware.Device | where {$_.GetType().Name -eq "VirtualDisk"} |

    Select  @{N="VMName";E={$vm.Name}},

 

    @{N="Hostname";E={$vm.guest.hostname}},

    @{N='IP Address';E={[string]::Join(',',($vm.Guest.Net | %{$_.IpAddress | where{$_.Split('.').Count -eq 4} | %{$_}}))}},

    @{N='OS';E={$vm.Guest.GuestFullName}},

    @{N='Host';E={$script:esx = Get-View -Id $vm.Runtime.Host; $script:esx.name}},

    @{N='Status';E={$vm.runtime.powerState}},

    @{N='CPU';E={$vm.config.Hardware.NumCpu}},

    @{N='RAM';E={$vm.Config.Hardware.MemoryMB| %{[math]::Round($_/1kb,2)}}},

    @{N="Disk capacity GB";E={$_.CapacityInKB| %{[math]::Round($_/1MB,2)}}}, 

    @{N="Disk datastore";E={$_.Backing.Filename.Split(']')[0].TrimStart('[')}},

    @{N="ThinProvisioned";E={$_.Backing.ThinProvisioned}},

   @{N="Disk Provision GB";E={$vm.Summary.Storage.UnCommitted| %{[math]::Round($_/1GB,2)}}},

    @{N="Disk Used GB";E={$vm.Summary.Storage.Committed| %{[math]::Round($_/1GB,2)}}},

   @{N="HW Version";E={$vm.Config.version}},

   @{N="Tools Status";E={$vm.guest.toolsversionstatus}}

}

##################End of script################

Any help?

1 Solution

Accepted Solutions
LucD
Leadership
Leadership

No need to have a Where-clause, the Get-View cmdlet has the Filter parameter.

In the RegEx you can use negative look around.

$sView = @{

    ViewType = 'VirtualMachine'

    Property = 'Name','runtime.powerState','Guest.hostname','Guest.net','Config.Hardware.numCPU',

          'Summary.Storage.Committed','Summary.Storage.UnCommitted','Config.Hardware.MemoryMB',

          'Runtime.Host','Guest.GuestFullName','Parent','ResourcePool',

          'Config.Hardware.Device','Config.version','guest.toolsversionstatus'

    Filter = @{

        Name = '^((?!replica).)*$'

    }

}

$vms = Get-View @sView


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

View solution in original post

18 Replies
LucD
Leadership
Leadership

No need to have a Where-clause, the Get-View cmdlet has the Filter parameter.

In the RegEx you can use negative look around.

$sView = @{

    ViewType = 'VirtualMachine'

    Property = 'Name','runtime.powerState','Guest.hostname','Guest.net','Config.Hardware.numCPU',

          'Summary.Storage.Committed','Summary.Storage.UnCommitted','Config.Hardware.MemoryMB',

          'Runtime.Host','Guest.GuestFullName','Parent','ResourcePool',

          'Config.Hardware.Device','Config.version','guest.toolsversionstatus'

    Filter = @{

        Name = '^((?!replica).)*$'

    }

}

$vms = Get-View @sView


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

View solution in original post

jamie20
Enthusiast
Enthusiast

Hi LucD,

Thanks for replying.

I tried the above script, but am getting empty result. :smileyconfused:

0 Kudos
LucD
Leadership
Leadership

There is nothing in $vms?

I just tested again, and it it returns all VMs except the ones that have 'replica' in their Displayname


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

0 Kudos
LucD
Leadership
Leadership

Just noticed I had still a Select Name after the call to Get-View.

That was for my testing and shouldn't have been there.

I corrected the code


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

jamie20
Enthusiast
Enthusiast

Thanks LucD :smileygrin:

Its working....Am having another doubt....

Instead of using Expression ={$_.Backing.ThinProvisioned}.....How to use 'StorageFormat' so that I can get thin or thick as output.

0 Kudos
LucD
Leadership
Leadership

The Expression part in a calculated property can be lines of code.

One way could be

@{N="ThinProvisioned";E={

    if($_.Backing.ThinProvisioned){'Thin'}

    else{'Thick'}

}}


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

jamie20
Enthusiast
Enthusiast

Thats good LucD....Its working....

I want to add another line, to count no of disks....

I tried these...

@{N="TotalHDD"; E={($_ | Get-HardDisk).count }}    But this gives empty result.

@{N="TotalHDD"; E={($_ .CapacityInKB).count }}  And this gives wrong result.

0 Kudos
LucD
Leadership
Leadership

I don't know how your current script looks, but if you are still looping through $vm.Config.Hardware.Device then $_ does not represent the VM.


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

0 Kudos
jamie20
Enthusiast
Enthusiast

Yes LucD...am looping through $vm.Config.Hardware.Device

So how can i get the count now?

0 Kudos
LucD
Leadership
Leadership

Something like this should work

@{N="TotalHDD"; E={(Get-HardDisk -VM $vm).count }}


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

0 Kudos
jamie20
Enthusiast
Enthusiast

No LucD....Its not working....Giving empty output....:smileyplain:

0 Kudos
LucD
Leadership
Leadership

I will need to see your current version of the script


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

0 Kudos
jamie20
Enthusiast
Enthusiast

Hi LucD,

Here it is:

$sView = @{

    ViewType = 'VirtualMachine'

    Property = 'Name','runtime.powerState','Guest.hostname','Guest.net','Config.Hardware.numCPU','Summary.Storage.Committed','Summary.Storage.UnCommitted','Config.Hardware.MemoryMB','Runtime.Host','Guest.GuestFullName','Config.Hardware.Device','Config.version','guest.toolsversionstatus','Parent','ResourcePool'

    Filter = @{Name = '^((?!replica).)*$'}

}

$vms = Get-View @sView

foreach($vm in $vms)

{

    $t = Get-View $vm.ResourcePool -Property Name,Parent

    $datacenter = $t.Name

    $vm.Config.Hardware.Device | where {$_.GetType().Name -eq "VirtualDisk"} |

    Select  @{N="VMName";E={$vm.Name}},

    @{N="Hostname";E={$vm.guest.hostname}},

    @{N='IP Address';E={[string]::Join(',',($vm.Guest.Net | %{$_.IpAddress | where{$_.Split('.').Count -eq 4} | %{$_}}))}},

    @{N='OS';E={$vm.Guest.GuestFullName}},

    @{N='Host';E={$script:esx = Get-View -Id $vm.Runtime.Host; $script:esx.name}},

    @{N='Status';E={$vm.runtime.powerState}},

    @{N='CPU';E={$vm.config.Hardware.NumCpu}},

    @{N='RAM';E={$vm.Config.Hardware.MemoryMB| %{[math]::Round($_/1kb,2)}}},

    @{N="Disk capacity GB";E={$_.CapacityInKB| %{[math]::Round($_/1MB,2)}}},

    @{N="Disk datastore";E={$_.Backing.Filename.Split(']')[0].TrimStart('[')}},

    @{N="Disk Type";E={if($_.Backing.ThinProvisioned){'Thin'}

    else{'Thick'}}},

    @{N="TotalHDD"; E={(Get-HardDisk -VM $vm).count }},

   @{N="Disk Provision GB";E={$vm.Summary.Storage.UnCommitted| %{[math]::Round($_/1GB,2)}}},

    @{N="Disk Used GB";E={$vm.Summary.Storage.Committed| %{[math]::Round($_/1GB,2)}}},

   @{N="HW Version";E={$vm.Config.version}},

   @{N="Tools Status";E={$vm.guest.toolsversionstatus}}

}

0 Kudos
LucD
Leadership
Leadership

Ok, then use this

    @{N="TotalHDD"; E={($vm.Config.Hardware.Device | where{$_ -is [VMware.Vim.VirtualDisk]}).Count}},


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

jamie20
Enthusiast
Enthusiast

Good LucD....Its working....:smileyblush:

But depending on the number of disk in vm , I get that amount of rows as result.

For example: If VM1 has 2 disks...The output also has 2 rows for each disk for vm1.

Is it possible to get total capacity in a single row, rather than separate disks in multiple rows?

0 Kudos
LucD
Leadership
Leadership

Yes, that is possible.
But how would you then decide on the Thick/Thin property?

Different HD can have different formats, be located on different datastores ...

This thread seems to be going on forever.

We're now far away from the original 'exclude' question :smileygrin:

I don't mind, but it will make it harder for other users to find this information.


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

0 Kudos
jamie20
Enthusiast
Enthusiast

Eeeeeeee....:smileylaugh:

True LucD....I marked the correct answer....

My script is incomplete thats the problem...Clear the total disk query...We can finish with that....

0 Kudos
LucD
Leadership
Leadership

Feel free to start a new thread for that


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