PowerCLI

Expand all | Collapse all

Need Powershell script for VM inventory

gurjitd

gurjitdOct 21, 2010 01:44 PM

  • 1.  Need Powershell script for VM inventory

    Posted Apr 17, 2010 05:12 AM

    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



  • 2.  RE: Need Powershell script for VM inventory

    Posted Apr 17, 2010 09:56 AM

    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



  • 3.  RE: Need Powershell script for VM inventory

    Posted Apr 17, 2010 01:22 PM

    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?



  • 4.  RE: Need Powershell script for VM inventory

    Posted Apr 17, 2010 02:16 PM

    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



  • 5.  RE: Need Powershell script for VM inventory

    Posted Apr 17, 2010 02:49 PM

    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



  • 6.  RE: Need Powershell script for VM inventory
    Best Answer

    Posted Apr 17, 2010 03:02 PM

    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



  • 7.  RE: Need Powershell script for VM inventory

    Posted Apr 17, 2010 03:17 PM

    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.



  • 8.  RE: Need Powershell script for VM inventory

    Posted Apr 17, 2010 04:50 PM

    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



  • 9.  RE: Need Powershell script for VM inventory

    Posted Apr 18, 2010 08:56 AM

    Thank you. I will check Alan's script.



  • 10.  RE: Need Powershell script for VM inventory

    Posted Apr 21, 2010 07:16 PM

    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?



  • 11.  RE: Need Powershell script for VM inventory

    Posted Apr 21, 2010 09:22 PM

    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



  • 12.  RE: Need Powershell script for VM inventory

    Posted Apr 21, 2010 09:45 PM

    Thank you. Will wait for next release.



  • 13.  RE: Need Powershell script for VM inventory

    Posted Sep 08, 2017 09:18 PM

    Hi Robert,

    is it possible to add Custom Attributes to the list? I am able to add Notes by adding $Report.Note = $VM.Notes and also added "Notes" to the Select-Object line, but to no avail to get any Custom Attributes to be displayed. Thank you.



  • 14.  RE: Need Powershell script for VM inventory

    Posted 9 days ago

    I don't see any attachment :) Please share!




  • 15.  RE: Need Powershell script for VM inventory

    Posted Sep 08, 2010 11:36 PM

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



  • 16.  RE: Need Powershell script for VM inventory

    Posted Sep 08, 2010 11:46 PM

    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



  • 17.  RE: Need Powershell script for VM inventory

    Posted Sep 08, 2010 11:52 PM

    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



  • 18.  RE: Need Powershell script for VM inventory

    Posted Sep 08, 2010 11:59 PM

    No problem, I attached the complete script.

    ____________

    Blog: LucD notes

    Twitter: lucd22



  • 19.  RE: Need Powershell script for VM inventory

    Posted Oct 07, 2010 12:44 PM

    Hi Luc,

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



  • 20.  RE: Need Powershell script for VM inventory

    Posted Oct 07, 2010 12:52 PM

    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



  • 21.  RE: Need Powershell script for VM inventory

    Posted Oct 07, 2010 12:54 PM

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

    Thanks a lot

    Gurjit Dhillon



  • 22.  RE: Need Powershell script for VM inventory

    Posted Oct 07, 2010 02:00 PM

    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



  • 23.  RE: Need Powershell script for VM inventory

    Posted Oct 07, 2010 03:25 PM

    Do you have a custom field with the name "CreatedBy" in your vSphere environment ?

    ____________

    Blog: LucD notes

    Twitter: lucd22



  • 24.  RE: Need Powershell script for VM inventory

    Posted Oct 08, 2010 09:24 AM

    Hi Luc,

    It working after upgrading PowerGuito latest version.

    I have one question, Can I get this report in excel format or in comma seperated format ?

    Regards

    GD



  • 25.  RE: Need Powershell script for VM inventory

    Posted Oct 08, 2010 09:43 AM

    Sure, on the last line change this

    Out-File "C:\VMreport.txt"
    

    into this

    Export-Csv "C:\VMreport.csv" -NoTypeInformation
    

    ____________

    Blog: LucD notes

    Twitter: lucd22



  • 26.  RE: Need Powershell script for VM inventory

    Posted Oct 08, 2010 10:49 AM

    Hi Luc,

    Thanks a lot, its working. few changes I have seen after taking the report in csv format, script is not able to fetch the Ipaddress and VMdkname , which it was able to get earlier in txt format.

    Instead of ipaddress I am getting System.String[] , and for vmdkname I am getting System.Object[[]|http://] , and for vmdkname I am getting System.Object[]

    Any clue.



  • 27.  RE: Need Powershell script for VM inventory

    Posted Oct 08, 2010 11:17 AM

    That's because those properties are arrays.

    The Export-Csv cmdlet doesn't handle arrays very well :smileywink:

    With the Join function of the String object, you can join the array elements together

    See attached script.

    ____________

    Blog: LucD notes

    Twitter: lucd22



  • 28.  RE: Need Powershell script for VM inventory

    Posted Oct 08, 2010 12:00 PM

    Hi,

    Ok, I have tired the changes you have said, got below error.

    Cannot find an overload for "Join" and the argument count: "1".

    At :line:31 char:34

    + $Report.VMDKname = ::Join &lt;&lt;&lt;&lt; (','.$VMDKnames)

    GD



  • 29.  RE: Need Powershell script for VM inventory

    Posted Oct 08, 2010 12:16 PM

    HI Luc,

    VMDkname was not important for me, so I have remvoed it, and script started working, along with the Ipaddress details.

    Can I also get the Vlan this vm has ?

    Thanks a ton again.


    GD



  • 30.  RE: Need Powershell script for VM inventory

    Posted Oct 08, 2010 12:26 PM

    There was a typo, a dot instead of a comma.

    Attached the corrected version with the VLANids.

    ____________

    Blog: LucD notes

    Twitter: lucd22



  • 31.  RE: Need Powershell script for VM inventory

    Posted Oct 08, 2010 01:14 PM

    Hi Luc,

    As excepted its working, One more question, as I am proving a list to find the vm details, there may be some vm's which are not there in the environment, it is giving error for this vm's on the console, can I get this list of the vm's which didn;t found in a seperate file ?

    in memeory field I am not getting the data in GB, it is showing me , 3609375, where it should show me 3696 , I am doing the the calculation in excel fie, is it possible do get the data in GB.

    Thanks a ton again.

    GD



  • 32.  RE: Need Powershell script for VM inventory

    Posted Oct 08, 2010 02:04 PM

    The attached script creates a separate file with the guests that were not found.

    I'm not sure which field you mean for the GB unit.

    What is the name of the property ?

    ____________

    Blog: LucD notes

    Twitter: lucd22



  • 33.  RE: Need Powershell script for VM inventory

    Posted Oct 09, 2010 05:42 AM

    first of all, Thanks a lot for this script...

    The datastoreName is displaying "VMware.Vim.VirtualMachineConfigInfoDatastoreUrlPair[]" for all the VM's.

    Also it will good if we can add the port group column.

    Thanks !!!



  • 34.  RE: Need Powershell script for VM inventory

    Posted Oct 09, 2010 10:46 AM

    There you go.

    ____________

    Blog: LucD notes

    Twitter: lucd22



  • 35.  RE: Need Powershell script for VM inventory

    Posted Oct 11, 2010 10:30 AM

    hi!

    i cant get the script to return anything...

    here is what i do:

    open vi powershell

    i do a connect to the vcenter ip

    when i run the ./Get-VMInformation-v3.ps1

    the report.csv is empty

    what do i do wrong?



  • 36.  RE: Need Powershell script for VM inventory

    Posted Oct 11, 2010 10:47 AM

    The script Get-VMInformation-v3.ps1 writes the output to the console, not a .csv file.

    ____________

    Blog: LucD notes

    Twitter: lucd22



  • 37.  RE: Need Powershell script for VM inventory

    Posted Oct 11, 2010 10:52 AM

    im using the Get-VMInformation-v4-1-to-text-3-1-2.ps1

    Are there any steps i should do in order to get a report? should i use any switches after the script?

    should i do something other in the powershell before running the script?



  • 38.  RE: Need Powershell script for VM inventory

    Posted Oct 11, 2010 11:10 AM

    Hi Luc,

    After making the changes in this script Get-VMInformation-v4-1-to-text-3-1-2.ps1 to give the output of the not found vm's in some file, script is giving issue, it doesnt; stops, its just says scripting executing, andnever stops.

    when I have disabled the above features from the scrpt, it worked as per desire.

    Can you please look ?

    Regadrs

    GD



  • 39.  RE: Need Powershell script for VM inventory

    Posted Oct 11, 2010 11:25 AM

    There was indeed a bug in the script.

    I updated the attached script above.

    Please try again.

    ____________

    Blog: LucD notes

    Twitter: lucd22



  • 40.  RE: Need Powershell script for VM inventory

    Posted Oct 11, 2010 12:26 PM

    i just want to let you know that now it works!



  • 41.  RE: Need Powershell script for VM inventory

    Posted Oct 11, 2010 02:09 PM

    the script works like a charm!

    how can i add in to get also the stats for the "used space" and "provisioned space" ?



  • 42.  RE: Need Powershell script for VM inventory

    Posted Oct 20, 2010 10:57 AM

    Hi Luc,

    I am using Get-VMInformation-v4-1-to-text-3-1-3.ps1 version, it works very nicely, is it possible to get the other details for the nic card configure in the vm, along with the Ip address. Like Gateway,DNS, and whether the Nic is configure as static or dynamic for each Nic in the VM.

    Regards

    GD



  • 43.  RE: Need Powershell script for VM inventory

    Posted Oct 20, 2010 11:03 AM

    Did you mean Get-VMInformation-v4-1-to-text-3-1.ps1 ?

    ____________

    Blog: LucD notes

    Twitter: lucd22



  • 44.  RE: Need Powershell script for VM inventory

    Posted Oct 20, 2010 11:39 AM

    Hi Luc,

    Not sure if I have renamed it again, I have attached it for you.

    Regards

    GD



  • 45.  RE: Need Powershell script for VM inventory

    Posted Oct 21, 2010 01:44 PM

    Hi Luc,

    Any luck ?

    Regards

    GD



  • 46.  RE: Need Powershell script for VM inventory

    Posted Oct 21, 2010 08:50 PM

    That information (gateway) is not available in the Guest object afaik.

    You would have to use the Get-VMGuestNetworkInterface cmdlet.

    But that requires that you pass the credentials for an account on the ESX(i) server and an account in the guest with administrative authority.

    ____________

    Blog: LucD notes

    Twitter: lucd22



  • 47.  RE: Need Powershell script for VM inventory

    Posted Nov 03, 2010 12:30 AM

    Thanks, getting the error below, for both for vlan id as well as portgroup.

    these are the lines added:

    $Report.VLANid = ::Join(',',(Get-VirtualPortgroup -VM $vm | %{$_.VlanId}))

    $Report.Portgroup = ::Join(',',(Get-VirtualPortgroup -VM $vm | %{$_.Name}))

    error:

    Exception calling "Join" with "2" argument(s): "Value cannot be null.

    Parameter name: value"

    At :line:38 char:32

    + $Report.VLANid = ::Join <<<< (',',(Get-VirtualPortgroup -VM $vm | %{$_.VlanId}))

    what could be the problem?



  • 48.  RE: Need Powershell script for VM inventory

    Posted Nov 03, 2010 01:03 AM

    Could it be that the VM is not connected to any portgroups ?

    ____________

    Blog: LucD notes

    Twitter: lucd22



  • 49.  RE: Need Powershell script for VM inventory

    Posted Nov 03, 2010 07:00 AM

    yes, that is the case. is there a way to insert a blank when the portgroup/vlan id is not set?

    thanks for your help.



  • 50.  RE: Need Powershell script for VM inventory

    Posted Nov 03, 2010 06:18 PM

    Try the attached script.

    It captures a $null condition and replaces the $null by an empty string.

    That way the Join method won't have a problem.

    ____________

    Blog: LucD notes

    Twitter: lucd22



  • 51.  RE: Need Powershell script for VM inventory

    Posted Nov 04, 2010 05:54 PM

    Thanks.

    Value cannot be null.

    Parameter name: value

    At :line:41 char:34

    + $tmpVLANId = Get-VirtualPortgroup <<<< -VM $vm | %{$_.VlanId}

    not sure why. I did a write-output and the script does run and i can see VM's with blank vlanid. but, the script fails witht he above message. Doesnt show the name of the VM it failed. Also, i hover the mouse on $vm, it shows the VM name which does have a dvswitch and vlanid set. is it because of the dvswitch rather than a standard switch?



  • 52.  RE: Need Powershell script for VM inventory

    Posted Nov 04, 2010 07:23 PM

    Yes, the current Get-VirtualPortgroup cmdlet doesn't list connections to portgroups on a dvSwitch.

    You can use the scripts in my dvSwitch scripting – Part 8 – Get and Set network adapters post for that :smileyhappy:

    ____________

    Blog: LucD notes

    Twitter: lucd22



  • 53.  RE: Need Powershell script for VM inventory

    Posted Nov 15, 2010 08:37 PM

    I have attached a script that I have been working with for a while to create an inventory of the guests in my virtual environment.

    This script will gather info about the guest and about the host that the guest is on.

    This script has been tested with PowerShell 2.0 and PowerCLI.

    Please let me know if you have any questions or problems with it.



  • 54.  RE: Need Powershell script for VM inventory

    Posted Nov 17, 2010 02:39 PM

    omarr1124- Great script!! I am only running into one error and that is trying to get the Service console IP. I get the following error:

    Cannot index into a null array.

    At c:\Get-vmware-guest-inventory.ps1:72 char110

    + $row | Add-Member -Type NoteProperty -Name "Host Service Console" -Value

    $VMHost.config.Network.consolenic[<<<<< 0].spec.IP.IPAddress

    +CategoryInfo :InvalidOperation: (0:Int32) [}, RuntimeException

    +FullyQualifiedErrorID : NullArray

    When I comment out the 4 Lines where it calls for the service console and subnet it runs great, but I dont get the Service Console IP. For some reason it doesnt like the and in the script. Any advice would be great.

    Also thanks for the scripts. Im not a scripter and I appriciate all the hard work and time you guys put into making these.



  • 55.  RE: Need Powershell script for VM inventory

    Posted Nov 17, 2010 07:02 PM

    OK I found the problem. This script will work on ESX hosts but not ESXi hosts because the $VMHost.Config.Network.ConsoleVNic value in ESXi is $null.

    Any Suggestions on finding the Service console IPon ESXi using your script?



  • 56.  RE: Need Powershell script for VM inventory

    Posted Nov 17, 2010 07:57 PM

    The following code will get the Console IP address for ESX and ESXi servers.

    Replace the original line with this one.

    $row | Add-Member -Type NoteProperty -Name "Host Service Console" -Value (& {
    		if($VMHost.Config.Product.ProductLineId -eq "embeddedEsx"){
    			($VMHost.Config.Network.Vnic | where{$_.Portgroup -eq "Management Network"} | Select -First 1).Spec.IP.IPAddress
    		}
    		else{
    			($VMHost.Config.Network.ConsoleVnic | Select -First 1).Spec.IP.IPAddress
    		}
    	})
    

    Note that this reports only on the first console (should you have more than 1).

    ____________

    Blog: LucD notes

    Twitter: lucd22



  • 57.  RE: Need Powershell script for VM inventory

    Posted Dec 28, 2010 05:21 PM

    A little modification is required, There are some VM's in our inventory using RDM disks, can it be possible to include the path of the RDM for this VM in the report? and also replace -1 with the word "No Limit" if  CPU & Memory Limit value is unlimited (-1).



  • 58.  RE: Need Powershell script for VM inventory

    Posted Dec 30, 2010 12:32 PM

    The following version of the script should do just that.

    $allLines = @() # Line added
    $null | Out-File "C:\vmnotfound.txt"
    Get-Content -Path "C:\vmnames.txt" | %{     $vm = Get-VM -Name $_ -ErrorAction SilentlyContinue    if(!$vm){         $_ | Out-File "C:\vmnotfound.txt" -Append    }     else{         $VMview = $VM | Get-View
           
    $VMResourceConfiguration = $VM | Get-VMResourceConfiguration
           
    $VMHardDisks = $VM | Get-HardDisk
           
    $HardDisksSizesGB = @()         $Temp = $VMHardDisks | ForEach-Object { $HardDisksSizesGB += [Math]::Round($_.CapacityKB/1MB) }         $VmdkSizeGB = ""
           
    $Temp = $HardDisksSizesGB | ForEach-Object { $VmdkSizeGB += "$_+" }         $VmdkSizeGB = $VmdkSizeGB.TrimEnd("+")         $TotalHardDisksSizeGB = 0
           
    $Temp = $HardDisksSizesGB | ForEach-Object { $TotalHardDisksSizeGB += $_ }         $VMDKnames = @()         $Temp = $VMHardDisks | ForEach-Object { $VMDKnames += $_.Filename.Split("/")[1] }         $Snapshots = $VM | Get-Snapshot
            $Report = "" | Select-Object VMname,vmCreatedByUser,vmCreatedDate,ESXname,ClusterName,MemoryGB,vCPUcount,vNICcount,         IPaddresses,VMXname,VMDKname,VmdkSizeGB,TotalVmdkSizeGB,DatastoreName,ToolsVersion,ToolsUpdate,NumCpuShares,         CpuLimitMHZ,CpuReservationMHZ,NumMemShares,ReservationsMB,LimitMB,SnapshotCount,GuestOS,VLANid,Portgroup,         RDMPath         $Report.VMName = $VM.name         $Report.vmCreatedByUser = $VM.CustomFields["CreatedBy"]         $Report.vmCreatedDate = $VM.CustomFields["CreatedOn"]         $Report.ESXname = $VM.Host         $Report.ClusterName = ($VM | Get-Cluster).Name         $Report.MemoryGB = $VM.MemoryMB/1KB         $Report.vCPUcount = $VM.NumCpu         $Report.vNICcount = $VM.Guest.Nics.Count         $Report.IPaddresses = [string]::Join(',',$VM.Guest.IPAddress)         $Report.VMXname = $VMview.Config.Files.VmPathName.Split("/")[1]         $Report.VMDKname = [string]::Join(',',$VMDKnames)         $Report.VmdkSizeGB = $VmdkSizeGB        $Report.TotalVmdkSizeGB = $TotalHardDisksSizeGB        $Report.DatastoreName = [string]::Join(',',($VMview.Config.DatastoreUrl | %{$_.Name}))         $Report.ToolsVersion = $VMview.Config.Tools.ToolsVersion         $Report.ToolsUpdate = $VMview.Guest.ToolsStatus         $Report.NumCpuShares = $VMResourceConfiguration.NumCPUShares         $Report.CpuLimitMHZ = &{if($VMResourceConfiguration.CpuLimitMhz -eq -1){"No Limit"}else{$VMResourceConfiguration.CpuLimitMhz}}         $Report.CpuReservationMHZ = $VMResourceConfiguration.CpuReservationMhz         $Report.NumMemShares = $VMResourceConfiguration.NumMemShares         $Report.ReservationsMB = $VMResourceConfiguration.MemReservationMB         $Report.LimitMB = &{if($VMResourceConfiguration.MemLimitMB -eq -1){"No Limit"}else{$VMResourceConfiguration.MemLimitMB}}         $Report.SnapshotCount = (@($VM | Get-Snapshot)).Count         $Report.GuestOS = $VM.Guest.OSFullName         $Report.VLANid = [string]::Join(',',(Get-VirtualPortgroup -VM $vm | %{$_.VlanId}))         $Report.Portgroup = [string]::Join(',',(Get-VirtualPortgroup -VM $vm | %{$_.Name}))         $RDMPaths = $vm | Get-HardDisk | where {$_.DiskType -like "Raw*"}         $Report.RDMPath = &{if($RDMPaths){$RDMPaths | %{$_.ScsiCanonicalName}}else{"No RDM"} }         $allLines += $Report # Line changed     } } $allLines | Export-Csv "C:\VMreport.csv" -NoTypeInformation -UseCulture


  • 59.  RE: Need Powershell script for VM inventory

    Posted Jan 03, 2011 02:01 AM

    Thanks Luc, For VM's with RDM drives getting an error like "system.object[]" in the report. Also vNIC type should also be added to this script and some of the VM's have multiple vNICs.

    Thnaks in advance.



  • 60.  RE: Need Powershell script for VM inventory

    Posted Jan 06, 2011 05:33 AM

    Need the above changes urgently. Please some one help.



  • 61.  RE: Need Powershell script for VM inventory

    Posted Jan 06, 2011 07:01 AM

    Give this a try

    $allLines = @() # Line added
    $null | Out-File "C:\vmnotfound.txt"
    Get-Content -Path "C:\vmnames.txt" | %{     $vm = Get-VM -Name $_ -ErrorAction SilentlyContinue
       
    if(!$vm){         $_ | Out-File "C:\vmnotfound.txt" -Append }     else{         $VMResourceConfiguration = $VM | Get-VMResourceConfiguration
            $VMHardDisks = $VM | Get-HardDisk
            $HardDisksSizesGB = @()         $Temp = $VMHardDisks | ForEach-Object { $HardDisksSizesGB += [Math]::Round($_.CapacityKB/1MB) }         $VmdkSizeGB = ""
           
    $Temp = $HardDisksSizesGB | ForEach-Object { $VmdkSizeGB += "$_+" }         $VmdkSizeGB = $VmdkSizeGB.TrimEnd("+")         $TotalHardDisksSizeGB = 0
            $Temp = $HardDisksSizesGB | ForEach-Object { $TotalHardDisksSizeGB += $_ }         $VMDKnames = @()         $Temp = $VMHardDisks | ForEach-Object { $VMDKnames += $_.Filename.Split("/")[1] }         $Snapshots = $VM | Get-Snapshot
            $Report = "" | Select-Object VMname,vmCreatedByUser,vmCreatedDate,ESXname,ClusterName,MemoryGB,vCPUcount,vNICcount,vNICType,         IPaddresses,VMXname,VMDKname,VmdkSizeGB,TotalVmdkSizeGB,DatastoreName,ToolsVersion,ToolsUpdate,NumCpuShares,         CpuLimitMHZ,CpuReservationMHZ,NumMemShares,ReservationsMB,LimitMB,SnapshotCount,GuestOS,VLANid,Portgroup,         RDMPath         $Report.VMName = $VM.name         $Report.vmCreatedByUser = $VM.CustomFields["CreatedBy"]         $Report.vmCreatedDate = $VM.CustomFields["CreatedOn"]         $Report.ESXname = $VM.Host         $Report.ClusterName = ($VM | Get-Cluster).Name         $Report.MemoryGB = $VM.MemoryMB/1KB         $Report.vCPUcount = $VM.NumCpu         $Report.vNICcount = $VM.Guest.Nics.Count         $report.vNicType = [string]::Join(',',($vm.NetworkAdapters | %{$_.Type}))         $Report.IPaddresses = [string]::Join(',',$VM.Guest.IPAddress)         $Report.VMXname = $vm.Extensiondata.Config.Files.VmPathName.Split("/")[1]         $Report.VMDKname = [string]::Join(',',$VMDKnames)         $Report.VmdkSizeGB = $VmdkSizeGB
           
    $Report.TotalVmdkSizeGB = $TotalHardDisksSizeGB
            $Report.DatastoreName = [string]::Join(',',($vm.Extensiondata.Config.DatastoreUrl | %{$_.Name}))         $Report.ToolsVersion = $vm.Extensiondata.Config.Tools.ToolsVersion         $Report.ToolsUpdate = $vm.Extensiondata.Guest.ToolsStatus         $Report.NumCpuShares = $VMResourceConfiguration.NumCPUShares         $Report.CpuLimitMHZ = &{if($VMResourceConfiguration.CpuLimitMhz -eq -1){"No Limit"}else{$VMResourceConfiguration.CpuLimitMhz}}         $Report.CpuReservationMHZ = $VMResourceConfiguration.CpuReservationMhz         $Report.NumMemShares = $VMResourceConfiguration.NumMemShares         $Report.ReservationsMB = $VMResourceConfiguration.MemReservationMB         $Report.LimitMB = &{if($VMResourceConfiguration.MemLimitMB -eq -1){"No Limit"}else{$VMResourceConfiguration.MemLimitMB}}         $Report.SnapshotCount = (@($VM | Get-Snapshot)).Count         $Report.GuestOS = $VM.Guest.OSFullName         $Report.VLANid = [string]::Join(',',(Get-VirtualPortgroup -VM $vm | %{$_.VlanId}))         $Report.Portgroup = [string]::Join(',',(Get-VirtualPortgroup -VM $vm | %{$_.Name}))         $RDMPaths = $vm | Get-HardDisk | where {$_.DiskType -like "Raw*"}         $Report.RDMPath = &{if($RDMPaths){[string]::Join(',',($RDMPaths | %{$_.ScsiCanonicalName}))}else{"No RDM"} }         $allLines += $Report # Line changed     } } $allLines | Export-Csv "C:\VMreport.csv" -NoTypeInformation -UseCulture


  • 62.  RE: Need Powershell script for VM inventory

    Posted Feb 06, 2011 04:49 AM

    Hi LucD,

    Thanks for this script, it's really useful.


    I was wondering if it was possible to add something? I'm trying to figure it out in the inventory script below. I was wondering if you could help figure out a way to add a few columns in the script for drivepath, disk space and disk usage? I know it's possible based on this post:

    http://communities.vmware.com/thread/292032

    For example, (similar to the IP address and DatastoreURL), I was hoping to add a few fields where the number of drives are separated by commas for each VM?

    VMname,   drivepath,   diskspace,   diskusage,   disk%

    +++++++++++++++++++++++++++++++++++++++++

    SomeVM,  C:\,D:\,   50GB, 30GB,   40GB, 20GB,   80%,66.66%

    Another(linux),   /         ,    100GB,      75GB,    75%         

    So if you were reading the above, you could easily tell that the C: drive is 50GB in size, it's using 40GB and is 80% full.

    I know it's possible based on the thread above but if I could incorporate it in this script, that would be perfect.

    Keep up the great work!

    Mike



  • 63.  RE: Need Powershell script for VM inventory

    Posted Feb 07, 2011 06:49 AM

    Here you go, I hope that's what you were looking for.

    The new lines are added at the end and all the properties start with GuestDisk

    $allLines = @() # Line added  
    $null
    | Out-File "C:\vmnotfound.txt" Get-Content -Path "C:\vmnames.txt" | %{     $vm = Get-VM -Name $_ -ErrorAction SilentlyContinue    if(!$vm){         $_ | Out-File "C:\vmnotfound.txt" -Append }     else{         $VMResourceConfiguration = $VM | Get-VMResourceConfiguration
           
    $VMHardDisks = $VM | Get-HardDisk
           
    $HardDisksSizesGB = @()         $Temp = $VMHardDisks | ForEach-Object { $HardDisksSizesGB += [Math]::Round($_.CapacityKB/1MB) }         $VmdkSizeGB = ""
           
    $Temp = $HardDisksSizesGB | ForEach-Object { $VmdkSizeGB += "$_+" }         $VmdkSizeGB = $VmdkSizeGB.TrimEnd("+")         $TotalHardDisksSizeGB = 0
            $Temp = $HardDisksSizesGB | ForEach-Object { $TotalHardDisksSizeGB += $_ }         $VMDKnames = @()         $Temp = $VMHardDisks | ForEach-Object { $VMDKnames += $_.Filename.Split("/")[1] }         $Snapshots = $VM | Get-Snapshot
            $Report = "" | Select-Object VMname,vmCreatedByUser,vmCreatedDate,ESXname,ClusterName,MemoryGB,vCPUcount,vNICcount,vNICType,         IPaddresses,VMXname,VMDKname,VmdkSizeGB,TotalVmdkSizeGB,DatastoreName,ToolsVersion,ToolsUpdate,NumCpuShares,         CpuLimitMHZ,CpuReservationMHZ,NumMemShares,ReservationsMB,LimitMB,SnapshotCount,GuestOS,VLANid,Portgroup,         RDMPath,GuestDisks,GuestDiskspaceGB,GuestDiskspaceUsedGB,GuestDiskspaceUsed         $Report.VMName = $VM.name         $Report.vmCreatedByUser = $VM.CustomFields["CreatedBy"]         $Report.vmCreatedDate = $VM.CustomFields["CreatedOn"]         $Report.ESXname = $VM.Host         $Report.ClusterName = ($VM | Get-Cluster).Name         $Report.MemoryGB = $VM.MemoryMB/1KB         $Report.vCPUcount = $VM.NumCpu         $Report.vNICcount = $VM.Guest.Nics.Count         $report.vNicType = [string]::Join(',',($vm.NetworkAdapters | %{$_.Type}))         $Report.IPaddresses = [string]::Join(',',$VM.Guest.IPAddress)         $Report.VMXname = $vm.Extensiondata.Config.Files.VmPathName.Split("/")[1]         $Report.VMDKname = [string]::Join(',',$VMDKnames)         $Report.VmdkSizeGB = $VmdkSizeGB
            $Report.TotalVmdkSizeGB = $TotalHardDisksSizeGB
            $Report.DatastoreName = [string]::Join(',',($vm.Extensiondata.Config.DatastoreUrl | %{$_.Name}))         $Report.ToolsVersion = $vm.Extensiondata.Config.Tools.ToolsVersion         $Report.ToolsUpdate = $vm.Extensiondata.Guest.ToolsStatus         $Report.NumCpuShares = $VMResourceConfiguration.NumCPUShares         $Report.CpuLimitMHZ = &{if($VMResourceConfiguration.CpuLimitMhz -eq -1){"No Limit"}else{$VMResourceConfiguration.CpuLimitMhz}}         $Report.CpuReservationMHZ = $VMResourceConfiguration.CpuReservationMhz         $Report.NumMemShares = $VMResourceConfiguration.NumMemShares         $Report.ReservationsMB = $VMResourceConfiguration.MemReservationMB         $Report.LimitMB = &{if($VMResourceConfiguration.MemLimitMB -eq -1){"No Limit"}else{$VMResourceConfiguration.MemLimitMB}}         $Report.SnapshotCount = (@($VM | Get-Snapshot)).Count         $Report.GuestOS = $VM.Guest.OSFullName         $Report.VLANid = [string]::Join(',',(Get-VirtualPortgroup -VM $vm | %{$_.VlanId}))         $Report.Portgroup = [string]::Join(',',(Get-VirtualPortgroup -VM $vm | %{$_.Name}))         $RDMPaths = $vm | Get-HardDisk | where {$_.DiskType -like "Raw*"}         $Report.RDMPath = &{if($RDMPaths){[string]::Join(',',($RDMPaths | %{$_.ScsiCanonicalName}))}else{"No RDM"} }         $Report.GuestDisks = [string]::Join(',',($VM.Guest.Disks | %{$_.Path}))         $Report.GuestDiskspaceGB = [string]::Join(',',($VM.Guest.Disks | %{"{0:f0}" -f ($_.Capacity/1GB)}))         $Report.GuestDiskspaceUsedGB = [string]::Join(',',($VM.Guest.Disks | %{"{0:f0}" -f (($_.Capacity - $_.FreeSpace)/1GB)}))         $Report.GuestDiskspaceUsed = [string]::Join(',',($VM.Guest.Disks | %{"{0:p0}" -f (($_.Capacity - $_.FreeSpace)/$_.Capacity)}))         $allLines += $Report # Line changed                 } } $allLines | Export-Csv "C:\VMreport.csv" -NoTypeInformation -UseCulture


  • 64.  RE: Need Powershell script for VM inventory

    Posted Feb 07, 2011 03:14 PM

    Great! That's it. Thanks LucD!

    One quick comment. If Tools are not running there will be errors like this:

    Attempted to divide by zero.
    At C:\scripts\PowerShell\vaughan_esx_inventory.ps1:99 char:120
    +         $Report.GuestDiskspaceUsed = [string]::Join(',',($VM.Guest.Disks | %{
    "{0:p0}" -f (($_.Capacity - $_.FreeSpace)/ <<<< $_.Capacity)}))
        + CategoryInfo          : NotSpecified: (:) [], RuntimeException
        + FullyQualifiedErrorId : RuntimeException
    Exception calling "Join" with "2" argument(s): "Value cannot be null.
    Parameter name: value"
    At C:\scripts\PowerShell\vaughan_esx_inventory.ps1:99 char:52
    +         $Report.GuestDiskspaceUsed = [string]::Join <<<< (',',($VM.Guest.Disk
    s | %{"{0:p0}" -f (($_.Capacity - $_.FreeSpace)/$_.Capacity)}))
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : DotNetMethodException

    It's not really a big deal as I'm aware that VMware Tools should be installed on VMs so their supported. Is there a way we could add an if clause to this? Maybe if Toolsversion = 0, do not look for disk info?

    Again, not a big deal at all because the export still succeeds and just leaves those values as 0 for guest.disk.

    Thanks again!

    Mike



  • 65.  RE: Need Powershell script for VM inventory

    Posted Feb 07, 2011 07:00 PM

    Yes, you easily can add a test.

    Something like this for example

    $Report.GuestDiskspaceUsedGB = &{if($VM.Guest){[string]::Join(',',($VM.Guest.Disks | %{"{0:f0}" -f (($_.Capacity - $_.FreeSpace)/1GB)}))}}

    When $VM.Guest is not $null, the expression will be calculated. Otherwise the property will be left empty.

    You will have to do something similar for the other property that involves a division.



  • 66.  RE: Need Powershell script for VM inventory

    Posted Feb 28, 2011 08:45 PM

    Great! Thanks again, LucD.

    I have one more question regarding this excellent script. Say I wanted to run the script in a loop, i can do something like this (which I found elsewhere on this forum):

    $viservers = "192.168.0.100", "192.168.0.200", "192.168.0.230", "192.168.0.240"

    foreach ($singleViserver in $viservers)

    {
    Connect-VIServer $singleViserver -user $username -password $password
    ###script goes here###
    Disconnect-viserver
    }

    $VMObject | Export-csv "$filelocation" -NoTypeInformation

    The for loop works (it connects and disconnects to each of the 4 hosts). But when the $VMObject is exported to the CSV file, it only exports the last one (the 192.168.0.240 host).

    Is there a way I can modify the loop so that it will export the data from all four hosts into one CSV file? That would be ideal.

    Thanks again for your help.


    Mike



  • 67.  RE: Need Powershell script for VM inventory

    Broadcom Employee
    Posted Feb 28, 2011 08:58 PM

    That's probably because the script overwrites $VMObject everytime it goes trough the loop.

    PowerCLI supports multiple connections

    Just remove the complete loop and just provide the array of viservers to the connect-viserver cmdlet

    $viservers = "192.168.0.100", "192.168.0.200", "192.168.0.230", "192.168.0.240"

    Connect-VIServer $Viservers -user $username -password $password

    ###script goes here###

    Disconnect-viserver



  • 68.  RE: Need Powershell script for VM inventory

    Posted Mar 01, 2011 03:00 AM

    Hi Avlieshout,

    Thanks for the speedy reply. I think I jumped the gun in posting my question because I tried one more thing after i posted and it worked!

    Here's my solution:

    $viservers = "192.168.0.100", "192.168.0.200", "192.168.0.230", "192.168.0.240"

    foreach ($singleViserver in $viservers)

    {
    Connect-VIServer $singleViserver -user $username -password $password
    ###script goes here###
    $VMObject += $Report
    Disconnect-viserver -Confirm:$False
    $FullReport += $VMObject
    }

    $FullReport | Export-csv "$filelocation" -NoTypeInformation

    I simply added a new variable for FullReport and made it increment off VMObject. Then I exported the 'full report' instead of VMobject. Worked like a charm!

    Thanks again for your quick reply.

    Mike



  • 69.  RE: Need Powershell script for VM inventory

    Broadcom Employee
    Posted Mar 01, 2011 06:08 PM

    Glad to hear that you've got it working!

    You should definitely try my suggestion if you find the time. It will make your scripts a lot easier.



  • 70.  RE: Need Powershell script for VM inventory

    Posted Jul 28, 2011 10:59 PM

    Hi LucD

    Could you help in explaining what am I doing wrong here to replace the  input vmnames.txt with the Get-VM command but am getting the errors attached in the error.txt file.

    Unfortunately could not trace it to the exact reason why this would be happening. Any help is highly appreciated.



  • 71.  RE: Need Powershell script for VM inventory

    Posted Jul 29, 2011 06:08 AM

    The first lines should be

    $allLines = @() # Line added 
    Get-VM
    | ForEach-Object {     $VMview = Get-View $_
    ...

    The $VM variable you had in your version would only have a content at the end of the Foreach loop.

    The lines above get the guests and place them in the pipeline.

    The Foreach-Object will take the objects one-by-one, make the object accessible through the $_ variable and execute the Foreach-Object code block for each object.



  • 72.  RE: Need Powershell script for VM inventory

    Posted Aug 03, 2011 11:44 PM

    Hi,

    Could you add support for think provisioned disks with the same totaling as you've done for the current disks stats?

    ie: list each disk thin provisioned size and total?

    Thanks

    Ray



  • 73.  RE: Need Powershell script for VM inventory

    Posted Oct 11, 2010 11:11 AM

    That version uses a .txt file that contains the names of the VMs to report on.

    If you want to use the script for all your VMs, you have to change thfirst lines.

    This

    $allLines = @() # Line added
    $null | Out-File "C:\vmnotfound.txt"
    Get-Content -Path "C:\vmnames.txt" | %{
    	Get-VM -Name $_ -ErrorAction SilentlyContinue
    	if(!$vm){
    		$_ | Out-File "C:\vmnotfound.txt" -Append
    	}
    } | ForEach-Object {
    

    should be replaced by this

    $allLines = @() # Line added
    Get-VM | ForEach-Object {
    

    ____________

    Blog: LucD notes

    Twitter: lucd22



  • 74.  RE: Need Powershell script for VM inventory

    Posted Aug 09, 2011 05:21 PM

    Hi LucD,

         Need your help for your good script for VM inventory.Its shows error and i need IP address too

    [vSphere PowerCLI] C:\tmp> .\VM-inventory.ps1
    Missing closing '}' in statement block.
    At C:\tmp\VM-inventory.ps1:62 char:1
    +  <<<<
        + CategoryInfo          : ParserError: (CloseBraceToken:TokenId) [], ParseException
        + FullyQualifiedErrorId : MissingEndCurlyBrace

    Thanks

    Kumar



  • 75.  RE: Need Powershell script for VM inventory

    Posted Aug 09, 2011 05:29 PM

    There was a curly brace missing.

    Try the attached version.



  • 76.  RE: Need Powershell script for VM inventory

    Posted Aug 09, 2011 05:34 PM

    Kumar,

    it could be a simple copy paste error..

    I was planning ot upload this latest version anyways for others to use.. try downloading this new file and see if it runs without errors for you.

    Details:

    • Line 6 -- you need to define the VCserver you are connecting to
    • Line 12 is the current default filename for the output
      • You can use Line 11 if you want date/timestamp in your file name (comment out if using line 11)
      • Comment Line 12 if you plan to use Line 11
    • Line 34 - Is commented and is there as a debug option to provide the VM name to ensure the script is running in your environment
    • Line 40 - Do not enable this line as it will give errors.. this function has been split to lines 41-43
    • Line 60 - 69 --> These are for custom fields .. adjust them based on your environment and adjust line 30 accordingly and comment out and appened it to Line 29 else you will get errors


  • 77.  RE: Need Powershell script for VM inventory

    Posted Aug 09, 2011 05:58 PM

    Thanks for prompt response.Is it possible that script should prompt for my credential becuase while executing  its taking default credential

    for windows while VC having diffirent id and diffrent passwd with more previlge.

    Thanks

    Kumar



  • 78.  RE: Need Powershell script for VM inventory

    Posted Aug 19, 2011 12:08 AM

    I need some assistance with grabbing just the MAC address of a given VM's Network Adapter 1 using powershell and then redirecting that output to a CSV file. I don't think I can use the scripts that have been posted on this thread because they involve getting the entire VM. I tried running the following:

    Get-NetworkAdapter -VM <VM>

    But this command spits out all of the NICs (I have 4 NICs) and a bunch of information I do not want. All I need is the MAC address of the first nic and to throw that information into a CSV file.

    Thanks in advance for the help.

    CJ



  • 79.  RE: Need Powershell script for VM inventory

    Posted Oct 31, 2011 05:14 AM

    Hi LucD

    Needed some help. with the attached script I am getting some errors and the macaddress being collected in the output is getting prefixed as follows. any help is highly appreciated.

    Macaddress -->

    Line 92 of the script.

    output is:

    VMMac == @{MacAddress=00:50:56:93:46:10}

    errors i am getting

    Exception calling "Join" with "2" argument(s): "Value cannot be null.

    Parameter name: value"

    At U:\Scripts\VMInventory-v4.ps1:47 char:50

    +     $Report.vNicType = &{if($_.Guest){[string]::Join <<<< (',',($vm.NetworkAd

    apters | %{$_.Type}))}}

        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException

        + FullyQualifiedErrorId : DotNetMethodException

    Exception calling "Join" with "2" argument(s): "Value cannot be null.

    Parameter name: value"

    At U:\Scripts\VMInventory-v4.ps1:58 char:52

    +     $Report.GuestDisks = &{if($_.Guest){[string]::Join <<<< (',',($_.Guest.Di

    sks | %{$_.Path}))}}

        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException

        + FullyQualifiedErrorId : DotNetMethodException

    Attempted to divide by zero.

    At U:\Scripts\VMInventory-v4.ps1:62 char:130

    +     $Report.GuestDiskspaceUsed = &{if($_.Guest){[string]::Join(',',($_.Guest.

    Disks | %{"{0:p0}" -f (($_.Capacity - $_.FreeSpace)/ <<<< $_.Capacity)}))}}

        + CategoryInfo          : NotSpecified: (:) [], RuntimeException

        + FullyQualifiedErrorId : RuntimeException

    Exception calling "Join" with "2" argument(s): "Value cannot be null.

    Parameter name: value"

    At U:\Scripts\VMInventory-v4.ps1:62 char:63

    +     $Report.GuestDiskspaceUsed = &{if($_.Guest){[string]::Join <<<< (',',($_.

    Guest.Disks | %{"{0:p0}" -f (($_.Capacity - $_.FreeSpace)/$_.Capacity)}))}}

        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException

        + FullyQualifiedErrorId : DotNetMethodException

    Exception calling "Join" with "2" argument(s): "Value cannot be null.
    Parameter name: value"
    At U:\Scripts\VMInventory-v4.ps1:47 char:35
    +     $Report.vNicType = [string]::Join <<<< (',',($vm.NetworkAdapters | %{$_.T
    ype}))
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : DotNetMethodException


  • 80.  RE: Need Powershell script for VM inventory

    Posted Oct 31, 2011 01:41 PM

    For the Join error, you should change this line

    $Report.vNicType = &{if($_.Guest){[string]::Join(',',($vm.NetworkAdapters | %{$_.Type}))}}

    into this

    $Report.vNicType = &{if($_.Guest){[string]::Join(',',($_.NetworkAdapters | %{$_.Type}))}}

    For the zero-divide error, it looks as if you have some VMs that zero-capacity harddisks.

    Is that possible ?

    You can try changing this line

    $Report.GuestDiskspaceUsed = &{if($_.Guest){[string]::Join(',',($_.Guest.Disks | %{"{0:p0}" -f (($_.Capacity - $_.FreeSpace)/$_.Capacity)}))}}

    into this

    $Report.GuestDiskspaceUsed = &{if($_.Guest){[string]::Join(',',($_.Guest.Disks | where {$_.Capacity -ne 0} | %{"{0:p0}" -f (($_.Capacity - $_.FreeSpace)/$_.Capacity)}))}}


  • 81.  RE: Need Powershell script for VM inventory

    Posted Sep 13, 2012 06:33 PM

    How about above script to HTML? Thank you.



  • 82.  RE: Need Powershell script for VM inventory

    Posted Sep 13, 2012 07:50 PM

    Basically when you replace the Export-CSV by a ConvertTo-Html | Set-Content, you should have a HTML file.

    But the fancy formatting will require playing with style sheets and using the Body and Head parameters.

    There are numerous examples in this community.

    Alan's vCheck is a good example how far you can go in producing HTML reports.

    Anything specific you're after ?



  • 83.  RE: Need Powershell script for VM inventory

    Posted Sep 13, 2012 08:28 PM

    Nothing specific, just wanted it to be in html and send to email.

    Thanks LucD



  • 84.  RE: Need Powershell script for VM inventory

    Posted Sep 19, 2012 06:32 PM

    Hi Luc,

    There are almost 3500+ VMs in our environment and its taking more than 10hours to complete this script. Is there any tweaks that can be made to have it completed in less amount of time. Also this is used on a daily basis.



  • 85.  RE: Need Powershell script for VM inventory

    Posted Sep 19, 2012 06:44 PM

    There are a couple of tricks to make scripts faster in bigger environments.

    Do you mean the script from this thread ? And if yes, which variation (there are quite a few).



  • 86.  RE: Need Powershell script for VM inventory

    Posted Sep 20, 2012 02:23 AM

    Yes... the script from your comment in #59

    $allLines = @() # Line added
    $null | Out-File "C:\vmnotfound.txt"
    Get-Content -Path "C:\vmnames.txt" | %{     $vm = Get-VM -Name $_ -ErrorAction SilentlyContinue
        if(!$vm){         $_ | Out-File "C:\vmnotfound.txt" -Append }     else{         $VMResourceConfiguration = $VM | Get-VMResourceConfiguration
            $VMHardDisks = $VM | Get-HardDisk
            $HardDisksSizesGB = @()         $Temp = $VMHardDisks | ForEach-Object { $HardDisksSizesGB += [Math]::Round($_.CapacityKB/1MB) }         $VmdkSizeGB = ""
            $Temp = $HardDisksSizesGB | ForEach-Object { $VmdkSizeGB += "$_+" }         $VmdkSizeGB = $VmdkSizeGB.TrimEnd("+")         $TotalHardDisksSizeGB = 0
            $Temp = $HardDisksSizesGB | ForEach-Object { $TotalHardDisksSizeGB += $_ }         $VMDKnames = @()         $Temp = $VMHardDisks | ForEach-Object { $VMDKnames += $_.Filename.Split("/")[1] }         $Snapshots = $VM | Get-Snapshot
            $Report = "" | Select-Object VMname,vmCreatedByUser,vmCreatedDate,ESXname,ClusterName,MemoryGB,vCPUcount,vNICcount,vNICType,         IPaddresses,VMXname,VMDKname,VmdkSizeGB,TotalVmdkSizeGB,DatastoreName,ToolsVersion,ToolsUpdate,NumCpuShares,         CpuLimitMHZ,CpuReservationMHZ,NumMemShares,ReservationsMB,LimitMB,SnapshotCount,GuestOS,VLANid,Portgroup,         RDMPath         $Report.VMName = $VM.name         $Report.vmCreatedByUser = $VM.CustomFields["CreatedBy"]         $Report.vmCreatedDate = $VM.CustomFields["CreatedOn"]         $Report.ESXname = $VM.Host         $Report.ClusterName = ($VM | Get-Cluster).Name         $Report.MemoryGB = $VM.MemoryMB/1KB         $Report.vCPUcount = $VM.NumCpu         $Report.vNICcount = $VM.Guest.Nics.Count         $report.vNicType = [string]::Join(',',($vm.NetworkAdapters | %{$_.Type}))         $Report.IPaddresses = [string]::Join(',',$VM.Guest.IPAddress)         $Report.VMXname = $vm.Extensiondata.Config.Files.VmPathName.Split("/")[1]         $Report.VMDKname = [string]::Join(',',$VMDKnames)         $Report.VmdkSizeGB = $VmdkSizeGB
            $Report.TotalVmdkSizeGB = $TotalHardDisksSizeGB
            $Report.DatastoreName = [string]::Join(',',($vm.Extensiondata.Config.DatastoreUrl | %{$_.Name}))         $Report.ToolsVersion = $vm.Extensiondata.Config.Tools.ToolsVersion         $Report.ToolsUpdate = $vm.Extensiondata.Guest.ToolsStatus         $Report.NumCpuShares = $VMResourceConfiguration.NumCPUShares         $Report.CpuLimitMHZ = &{if($VMResourceConfiguration.CpuLimitMhz -eq -1){"No Limit"}else{$VMResourceConfiguration.CpuLimitMhz}}         $Report.CpuReservationMHZ = $VMResourceConfiguration.CpuReservationMhz         $Report.NumMemShares = $VMResourceConfiguration.NumMemShares         $Report.ReservationsMB = $VMResourceConfiguration.MemReservationMB         $Report.LimitMB = &{if($VMResourceConfiguration.MemLimitMB -eq -1){"No Limit"}else{$VMResourceConfiguration.MemLimitMB}}         $Report.SnapshotCount = (@($VM | Get-Snapshot)).Count         $Report.GuestOS = $VM.Guest.OSFullName         $Report.VLANid = [string]::Join(',',(Get-VirtualPortgroup -VM $vm | %{$_.VlanId}))         $Report.Portgroup = [string]::Join(',',(Get-VirtualPortgroup -VM $vm | %{$_.Name}))         $RDMPaths = $vm | Get-HardDisk | where {$_.DiskType -like "Raw*"}         $Report.RDMPath = &{if($RDMPaths){[string]::Join(',',($RDMPaths | %{$_.ScsiCanonicalName}))}else{"No RDM"} }         $allLines += $Report # Line changed     } } $allLines | Export-Csv "C:\VMreport.csv" -NoTypeInformation -UseCulture


  • 87.  RE: Need Powershell script for VM inventory

    Posted Sep 25, 2012 01:27 AM

    Hi Luc,

    Any updates ?