VMware Cloud Community
Sureshadmin
Contributor
Contributor
Jump to solution

Need Powershell script for VM inventory

Hi,

Need a powershell script to collect the below given info from virtual center.

vmname | ESX Name | memory(GB) | vCPU count | vNIC Count | IP address(all) | vmdk(s) size(GB) | Total vmdk size(GB) | Datastore name | Tools version | tools update | shares | reservations(MB) | limit(MB) | snapshot count | Guest OS

vmdk(s) size(GB) ---> Need all vmdk size of the vm seperated by "". EX: for a vm of 3 vmdk's of size 20,20,10GB each the output would be 2020+10

Datastore Name ---> Name of the datastore where VMX is located.

Tools update ---> Need status like old or updated

1 Solution

Accepted Solutions
RvdNieuwendijk
Leadership
Leadership
Jump to solution

Attached you will find the combined VM information script version 3. If you want only certain fields like in the VMX and VMDK script you can use the Select-Object cmdlet to select only this fields. The next oneliner will use the Get-Information-v3.ps1 script to generate the output of the VMX and VMDK script:

./Get-VMInformation-v3.ps1 | Select-Object -property VMname,VMXName,VMDKName,VmdkSizeGB,TotalVmdkSizeGB,DatastoreName

Robert

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition

View solution in original post

Reply
0 Kudos
85 Replies
RvdNieuwendijk
Leadership
Leadership
Jump to solution

The attached script will give the VM inventory information you need. I didn't know if you wanted CPU or memory shares, so I included them both. I have to attach the script becasue it contains square brackets and the forum software has problems with them.

Robert

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Reply
0 Kudos
Sureshadmin
Contributor
Contributor
Jump to solution

Perfect Script. Thank you so much, Robert.

Just missed a couple of field's in my question. Can you please add the fields given in bold,

vmname | ESX Name | Cluster name | memory(GB) | vCPU count | vNIC Count | IP address(all) | vmdk(s) size(GB) | Total vmdk size(GB) | Datastore name | Tools version | tools update | CPUshares | CPU Limit(MHz) | CPU reservation(MHz) | Mem Shares | reservations(MB) | limit(MB) | snapshot count | Guest OS

Also need a VMX and VMDK script in below format,

VMname | VMX name | vmdk(s) name | vmdk(s) size | total vmdk | Datastore(s) Name

Expected output(example): abc | abc.vmx | abc.vmdk, abc_1.vmdk | 10+10 | 20 | ds1, ds2

Datastores Name : In case if vm files are in multiple datastores, then need names of multiple datastores.

Can you please help me in this script also?

Reply
0 Kudos
RvdNieuwendijk
Leadership
Leadership
Jump to solution

First I have attached the new version of the Get-VMinformation script with Cluster name, CPU Limit(MHz) and CPU reservation(MHz) included.

I'm still working on the VMX and VMDK script.

Robert

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
RvdNieuwendijk
Leadership
Leadership
Jump to solution

Attached you will find the VMX and VMDK script.

I will make a new script that will combine the information in both scripts because there is al lot of overlap.

Robert

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
RvdNieuwendijk
Leadership
Leadership
Jump to solution

Attached you will find the combined VM information script version 3. If you want only certain fields like in the VMX and VMDK script you can use the Select-Object cmdlet to select only this fields. The next oneliner will use the Get-Information-v3.ps1 script to generate the output of the VMX and VMDK script:

./Get-VMInformation-v3.ps1 | Select-Object -property VMname,VMXName,VMDKName,VmdkSizeGB,TotalVmdkSizeGB,DatastoreName

Robert

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Reply
0 Kudos
Sureshadmin
Contributor
Contributor
Jump to solution

WoW...Awesome. Thank u so much for your quick response.

If possible please add vmCreatedByUser and vmCreatedDate fields in the script. This information is there in the VC database and i don't know whether this can be collected via a powershell script. If this is difficult please leave it, not so mandatory for me.

Given correct and helpful answers.

Reply
0 Kudos
RvdNieuwendijk
Leadership
Leadership
Jump to solution

Look for the vmCreatedByUser and vmCreatedDate fields at a wonderfull script made by Alan Renouf: Who created that VM ?. The problem is that this information is not in the database. Alan's script retrieves the information from the eventlogs and stores them in the annotations of the VM's. He also gives an example how to retrieve the information.

I have extended my script with the vmCreatedByUser and vmCreatedDate fields. Remember that you have to run Alan Renouf's script first, to be able to use my latest version.

Robert

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Reply
0 Kudos
Sureshadmin
Contributor
Contributor
Jump to solution

Thank you. I will check Alan's script.

Reply
0 Kudos
Sureshadmin
Contributor
Contributor
Jump to solution

hi Robert,

This script works fine when connected directly to ESX server. When i connect to virtual center and run this script i get this error given below,

Get-VMResourceConfiguration : Object reference not set to an instance of an obj

ect.

At C:\ps\vmconfig.ps1:5 char:65

+ $VMResourceConfiguration = $VM | Get-VMResourceConfiguration <<<<

Any ideas?

Reply
0 Kudos
RvdNieuwendijk
Leadership
Leadership
Jump to solution

There seems to be a problem with the Get-VMResourceConfiguration cmdlet. See thread . You problably run into this problem. Hopefully it will be solved in the next release. Maybe next month.

Robert

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Reply
0 Kudos
Sureshadmin
Contributor
Contributor
Jump to solution

Thank you. Will wait for next release.

Reply
0 Kudos
justinsmith
Enthusiast
Enthusiast
Jump to solution

Is there a way to output this to a text/word document?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Creating a Word document is a bit more complex, but creating a text file is quite easy.

Taking the script you only have to make some minimal changes

$allLines = @()                                            # Line added
Get-VM | `
  ForEach-Object {
...
    $Report.GuestOS = $VM.Guest.OSFullName
    $allLines += $Report                                   # Line changed
  }
$allLines | Out-File "C:\VMreport.txt"                     # Line added

____________

Blog: LucD notes

Twitter: lucd22


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

Reply
0 Kudos
justinsmith
Enthusiast
Enthusiast
Jump to solution

Im very new when it comes to scripting in powershell... where would I go about adding these new lines and where do I add onto the line that was changed?

Edit: Nevermind, I think I figured it out

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

No problem, I attached the complete script.

____________

Blog: LucD notes

Twitter: lucd22


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

Reply
0 Kudos
gurjitd
Contributor
Contributor
Jump to solution

Hi Luc,

Is it possible to get the inventory only for the mention Vm's which is there in txt file from VC server ?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Provided the vm names are in a text file with this layout

vmname1
vmname2
vmname3
...

then you can use the attached version of the script.

____________

Blog: LucD notes

Twitter: lucd22


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

Reply
0 Kudos
gurjitd
Contributor
Contributor
Jump to solution

WOW, too fast reply. I will check your script.

Thanks a lot

Gurjit Dhillon

Reply
0 Kudos
gurjitd
Contributor
Contributor
Jump to solution

Hi Luc,

I am getting below error, any clue ?

Unable to index into an object of type VMware.VimAutomation.Common.ReadOnlyDictionary`2[[http://System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089|http://System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[http://System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]|http://System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]].

At :line:27 char:44

+ $Report.vmCreatedByUser = $VM.CustomFields

Regards

GD

Reply
0 Kudos