VMware Cloud Community
breakstuf50
Enthusiast
Enthusiast
Jump to solution

Additional information to pull out

Hi Guys,

Need help pulling out more info from this script, the model of the esx hosts and filter powerstate of vm = on. This is what i have currently.

Connect-VIServer xxxxx

$date = Get-Date -format M-d-yyyy

Import-Csv C:\test\vmlisttest.csv -UseCulture | %{

    Get-VM -Name $_.Name |

    Select Name,

        @{N='Datacenter';E={Get-Datacenter -VM $_ | Select -ExpandProperty Name}},

        @{N='Cluster';E={Get-Cluster -VM $_ | Select -ExpandProperty Name}},

        Host,NumCpu, MemoryGB,

        ProvisionedSpaceGB,Powerstate

        @{N='Path';E={

            $current = Get-View $_.ExtensionData.Parent

            $path = $_.Name

            do {

                $parent = $current

                if($parent.Name -ne "vm"){$path =  $parent.Name + "\" + $path}

                $current = Get-View $current.Parent

            } while ($current.Parent -ne $null)

            [string]::Join('\',($path.Split('\')[0..($path.Split('\').Count-2)]))        

        }},

        FolderId

} | Export-Csv “C:\test\test-vcenter-test-vmpath-$date.csv” -NoTypeInformation -UseCulture

sample output

         

NameDatacenterClusterModelHostNumCpuMemoryGBProvisionedSpaceGBPathFolderId
testvmUSserverBL460cesxdev-02.mak.esportz.com1221.00084689US\Server\Folder-group-v4603

Cheers,

1 Solution

Accepted Solutions
Craig_Baltzer
Expert
Expert
Jump to solution

get-vm | where-object {$_.PowerState -eq "PoweredOn"} | Select Name, @{N='Model'; E={ (Get-VMHost -Name $_.Host).Model }}

has the basics of what you want to do I think.

So in your code add the where-object {$_.PowerState -eq "PoweredOn"} after  Get-VM -Name $_.Name | to work on only the powered on VMs.

Then add @{N='Model'; E={ (Get-VMHost -Name $_.Host).Model }}, after @{N='Cluster';E={Get-Cluster -VM $_ | Select -ExpandProperty Name}},

View solution in original post

Reply
0 Kudos
5 Replies
Craig_Baltzer
Expert
Expert
Jump to solution

get-vm | where-object {$_.PowerState -eq "PoweredOn"} | Select Name, @{N='Model'; E={ (Get-VMHost -Name $_.Host).Model }}

has the basics of what you want to do I think.

So in your code add the where-object {$_.PowerState -eq "PoweredOn"} after  Get-VM -Name $_.Name | to work on only the powered on VMs.

Then add @{N='Model'; E={ (Get-VMHost -Name $_.Host).Model }}, after @{N='Cluster';E={Get-Cluster -VM $_ | Select -ExpandProperty Name}},

Reply
0 Kudos
breakstuf50
Enthusiast
Enthusiast
Jump to solution

Hi Craig,

I tried to run this one liner and it works. was able to pull out the VM and the host model associated with it.

get-vm | where-object {$_.PowerState -eq "PoweredOn"} | Select Name, @{N='Model'; E={ (Get-VMHost -Name $_.Host).Model }}


when I incorporate the script you gave me the below is the output I get a bunch of datas.

PowerStateVersionDescriptionNotesGuestNumCpuMemoryMBMemoryGBHardDisksNetworkAdaptersUsbDevicesCDDrivesFloppyDrivesHostHostIdVMHostIdVMHostVAppFolderIdFolderResourcePoolIdResourcePoolPersistentIdUsedSpaceGBProvisionedSpaceGBDatastoreIdListHARestartPriorityHAIsolationResponseDrsAutomationLevelVMSwapfilePolicyVMResourceConfigurationGuestIdNameCustomFieldsExtensionDataIdUidClient

This is how my script looks like now per your recommendation.

Connect-VIServer

$date = Get-Date -format M-d-yyyy

Import-Csv C:\test\vmlisttest.csv -UseCulture | %{

    Get-VM -Name $_.Name | where-object {$_.PowerState -eq "PoweredOn"}

    Select Name,

        @{N='Datacenter';E={Get-Datacenter -VM $_ | Select -ExpandProperty Name}},

        @{N='Model'; E={ (Get-VMHost -Name $_.Host).Model }},

        @{N='Cluster';E={Get-Cluster -VM $_ | Select -ExpandProperty Name}},

        Host,NumCpu, MemoryGB,

        ProvisionedSpaceGB,Powerstate,

        @{N='Path';E={

            $current = Get-View $_.ExtensionData.Parent

            $path = $_.Name

            do {

                $parent = $current

                if($parent.Name -ne "vm"){$path =  $parent.Name + "\" + $path}

                $current = Get-View $current.Parent

            } while ($current.Parent -ne $null)

            [string]::Join('\',($path.Split('\')[0..($path.Split('\').Count-2)]))          

        }},

        FolderId

} | Export-Csv “C:\test\test-vcenter-test-vmpath-$date.csv” -NoTypeInformation -UseCulture

Any Ideas?

Reply
0 Kudos
breakstuf50
Enthusiast
Enthusiast
Jump to solution

Looks like I get it to worked already. I removed the where-object {$_.PowerState -eq "PoweredOn"} and it worked. If you have ideas why its causing to get all datas instead of the one defined please let me know. Thanks again.

here's how the script look like now.

Connect-VIServer

$date = Get-Date -format M-d-yyyy

Import-Csv C:\test\vmlisttest.csv -UseCulture | %{

    Get-VM -Name $_.Name |

    Select Name,

        @{N='Datacenter';E={Get-Datacenter -VM $_ | Select -ExpandProperty Name}},

        @{N='Cluster';E={Get-Cluster -VM $_ | Select -ExpandProperty Name}},

        Host,NumCpu, MemoryGB,

        ProvisionedSpaceGB,Powerstate,

        @{N='Path';E={

            $current = Get-View $_.ExtensionData.Parent

            $path = $_.Name

            do {

                $parent = $current

                if($parent.Name -ne "vm"){$path =  $parent.Name + "\" + $path}

                $current = Get-View $current.Parent

            } while ($current.Parent -ne $null)

            [string]::Join('\',($path.Split('\')[0..($path.Split('\').Count-2)]))        

        }},

        FolderId,

  @{N='Manufacturer'; E={ (Get-VMHost -Name $_.Host).Manufacturer }},

  @{N='Model'; E={ (Get-VMHost -Name $_.Host).Model }},

  @{N='ProcessorType'; E={ (Get-VMHost -Name $_.Host).ProcessorType }}

} #| Export-Csv “C:\test\test-vcenter-test-vmpath-$date.csv” -NoTypeInformation -UseCulture

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

There doesn't seem to be a pipeline symbol ('|') at the end of the line with the Where-clause


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

breakstuf50
Enthusiast
Enthusiast
Jump to solution

o wow thanks LucD!

Reply
0 Kudos