Sureshadmin
Contributor
Contributor

Need Powershell script for VM inventory

Jump to solution

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

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

0 Kudos
85 Replies
RvdNieuwendijk
Leadership
Leadership

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
0 Kudos
Sureshadmin
Contributor
Contributor

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?

0 Kudos
RvdNieuwendijk
Leadership
Leadership

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

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

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
0 Kudos
Sureshadmin
Contributor
Contributor

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.

0 Kudos
RvdNieuwendijk
Leadership
Leadership

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
0 Kudos
Sureshadmin
Contributor
Contributor

Thank you. I will check Alan's script.

0 Kudos
Sureshadmin
Contributor
Contributor

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?

0 Kudos
RvdNieuwendijk
Leadership
Leadership

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
0 Kudos
Sureshadmin
Contributor
Contributor

Thank you. Will wait for next release.

0 Kudos
justinsmith
Enthusiast
Enthusiast

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

0 Kudos
LucD
Leadership
Leadership

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

0 Kudos
justinsmith
Enthusiast
Enthusiast

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

0 Kudos
LucD
Leadership
Leadership

No problem, I attached the complete script.

____________

Blog: LucD notes

Twitter: lucd22


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

0 Kudos
gurjitd
Contributor
Contributor

Hi Luc,

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

0 Kudos
LucD
Leadership
Leadership

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

0 Kudos
gurjitd
Contributor
Contributor

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

Thanks a lot

Gurjit Dhillon

0 Kudos
gurjitd
Contributor
Contributor

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

0 Kudos