VMware Cloud Community
malek12
Contributor
Contributor

VM Script data collection : need help combining scripts

I have a request to create a script that will give the following properties of a VM in one csv file:

 

Power State

DNS Name

GuestOS

vCPUs

MemoryGB

Provisoned Space GB

Used Space GB

Disk  Type ( is disk type thick or thin provisioned )

Datastore

Cluster

Resource Pool

Folder

Notes

Tags

IP’s

 

They need to be pulled from specific resource pools.  So for example all resource pools that are for the 01 departments, so that would be get-resourcepool -name depart01*

 

I have multiple scripts that gather this information, however I do not know how to combine them all into one script to produce one csv file.

 

I use a simple select script that pulls most of the vm info:

 

get-resourcepool -name depart01* | get vm | select Name, PowerState, Notes, NumCpu, MemoryGB, UsedSpaceGB, ProvisionedSpaceGB, Folder, ResourcePool

 

Another script I have collects the Hard disk info

Get-ResourcePool -name depart01 * | Get-VM |Get- Harddisk

Select Name,@{N="VM";E={$_.Parent.Name}},

    @{N="Datastore";E={$_.Filename.Split(']')[0].Trim('[')}},

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

    DiskType,Persistence,CapacityGB

 

For cluster and datastore I use the following script :

get-resourcepool -name depart01* | Get-VM | Select Name, @{N="Cluster";E={Get-Cluster -VM $_}}, `@{N="Datastore";E={Get-Datastore -VM $_}}

 

 

I export all of these into different CSV files but now the ask is to create one file.

I would also like to be able to change the names of the headings.  For example instead of the output just saying Name for the vm name column I would like for it to say  VM Name.  Or instead of the column saying UsedSpaceGB I would like for it to say “Used Disk Space GB”

 

Thanks,
Reply
0 Kudos
13 Replies
LucD
Leadership
Leadership

Basically you will have to add the desired properties to the Select cmdlet.

To have the option to change the header of a column you can use a calculated property.

A template to start from.
Just added your other properties in there.

$resourcePoolName = 'depart01*'


Get-ResourcePool -Name $resourcePoolName -PipelineVariable rp |

Get-VM |

Select @{N='VM Name';E={$_.Name}},

    @{N='Resource Pool';E={$rp.Name}},

    @{N='Used Disk Space GB';E={$_.UsedSpaceGB}} |

Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture


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

Reply
0 Kudos
malek12
Contributor
Contributor

Thanks for the guidance Luc D.  I am working my way through it.  Below is what I have so far and it is giving me the info that I need.

$resourcePoolName = 'depart01*'
Get-ResourcePool -Name $resourcePoolName -PipelineVariable rp |
Get-VM |
Select @{N='VM Name';E={$_.Name}},
@{N='Resource Pool';E={$rp.Name}},
@{N='Used Disk Space GB';E={$_.UsedSpaceGB}},
@{N='Provisioned Disk Space GB';E={$_.ProvisionedSpaceGB}},
@{N='Power State';E={$_.PowerState}},
@{N='VM Notes';E={$_.notes}},
@{N='Memory in GB';E={$_.MemoryGB}},
@{N='vCPUS';E={$_.NumCpu}},
@{N='Folder';E={$_.folder}},
@{N="Host Cluster";E={Get-Cluster -VM $_}},
@{N="Datastore";E={Get-Datastore -VM $_}},
@{N="DNSName"; E={$_.ExtensionData.Guest.Hostname}},
@{N='Guess OS';E={$_.Guest.OSFullName}}|
Export-Csv departtest10.csv -NoTypeInformation -UseCulture

 

I still need to add columns for IP, MAC, and Disk Type (thick or thin provisioned).  Should be able to do that in the morning.

Reply
0 Kudos
malek12
Contributor
Contributor

I was only able to figure out the IP address part to what I have above.  I am using @{N='IPs';E={$_.Guest.IPAddress -join '|'}},

 

How do I add MAC address, vm tags, and disk type (thick or thin provision in the same format as the rest of the script ?

Reply
0 Kudos
LucD
Leadership
Leadership

Do you have VMs that have more than 1 harddisk or vNIC?

Can those values be shown together?


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

Reply
0 Kudos
malek12
Contributor
Contributor

yes vm's can have multiple hard disk and can have multiple vnics

A quick looks shows vm's with as many a 9 hard disk, and as many as 7 nics, but only 3 of the nics are connected.

 

If possible I would like to have a column for MAC address, a column for vm tags, and a column for disk type (thin or thick provisioned)

Reply
0 Kudos
LucD
Leadership
Leadership

I understand that, but if a VM has 9 harddisk, there would be 9 columns in the output.
Or can I combine the type of those 9 harddisk in one property?

For example: HDType = "Thin|Thin|Thick|Thin"


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

Reply
0 Kudos
malek12
Contributor
Contributor

Ah ok.  Yes.  They can be combined into one property
Reply
0 Kudos
LucD
Leadership
Leadership

Try like this

$resourcePoolName = 'depart01*'

Get-ResourcePool -Name $resourcePoolName -PipelineVariable rp |

Get-VM |

Select @{N='VM Name';E={$_.Name}},

@{N='Resource Pool';E={$rp.Name}},

@{N='Used Disk Space GB';E={$_.UsedSpaceGB}},

@{N='Provisioned Disk Space GB';E={$_.ProvisionedSpaceGB}},

@{N='Power State';E={$_.PowerState}},

@{N='VM Notes';E={$_.notes}},

@{N='Memory in GB';E={$_.MemoryGB}},

@{N='vCPUS';E={$_.NumCpu}},

@{N='Folder';E={$_.folder}},

@{N="Host Cluster";E={Get-Cluster -VM $_}},

@{N="Datastore";E={Get-Datastore -VM $_}},

@{N="DNSName"; E={$_.ExtensionData.Guest.Hostname}},

@{N='Guess OS';E={$_.Guest.OSFullName}},

@{N='IPs';E={$_.Guest.IPAddress -join '|'}},

@{N='HD Type';E={(Get-HardDisk -VM $_).StorageFormat -join '|'}},

@{N='MacAddress';E={(Get-NetworkAdapter -VM $_).MacAddress -join '|'}}|

Export-Csv departtest10.csv -NoTypeInformation -UseCulture


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

Reply
0 Kudos
malek12
Contributor
Contributor

Thanks LucD.  Only thing missing is the vm tags.  I need those added as well because each VM will have tags attached to them.  For example a tag to say what department and then another tag that says what subdivision of the department. 
Reply
0 Kudos
LucD
Leadership
Leadership

What are the names of these tags and their category?


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

Reply
0 Kudos
malek12
Contributor
Contributor

Tag names and categories are owned by department heads and can change at a whim.  

We currently have a script that we run that just pulls all tags associated with any vm in a resource pool.

Get-ResourcePool -name depart* | Get-VM |
      Select Name,@{Name="Tags";Expression={(Get-TagAssignment -Entity $_).Tag.Name}} |
      Where {$_.Tags} | Export-Csv

Reply
0 Kudos
malek12
Contributor
Contributor

I used @{Name="Tags";Expression={(Get-TagAssignment -Entity $_).Tag.Name}} |Where {$_.Tags} |  and it looks like that gave me what I needed
Reply
0 Kudos
LucD
Leadership
Leadership

So is it working now?


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

Reply
0 Kudos