VMware Cloud Community
AGDave
Enthusiast
Enthusiast
Jump to solution

Script error (Works on vCenter 4.x, errors on 5.x)

Hi Everyone,

LucD kindly provided me a script to produce a report on think provisioned virtual disks in my infrastructure. I used it extensively on multiple 4.x vCenter servers and it worked perfectly.

I've recently upgraded to vSphere 5, and re-ran the script but encountering issues. The script itself hasn't been modified, so I'm lead to believe that something has changed between these version numbers that's affecting the script behavior.

Here is the script:

$report = @()

$allvms = Get-VM

$allvms += Get-Template            

foreach ($vm in $allvms) {

  $vmview = $vm | Get-View 

  $diskTab = @{}

  $vmview.LayoutEx.Disk | %{

    $filekeys = @()

    $_.Chain | %{

      $fileKeys += $_.FileKey   

   }

    $size = $vmview.LayoutEx.File | where {$filekeys -contains $_.Key} |    

  Measure-Object -Property Size -Sum | Select -ExpandProperty Sum  

  $diskTab.Add($_.Key,$size)

  }

 

  foreach($dev in $vmview.Config.Hardware.Device){

    if($dev.Backing.ThinProvisioned -eq $true) {

        $row = "" | select VMNAME, HD, DATASTORE, VMSIZE_GB, VMUSED_GB, PERCENT       

  $row.VMNAME = $vmview.Config.Name       

  $row.HD = $dev.DeviceInfo.Label       

  $row.DATASTORE = $dev.Backing.FileName.Split(']')[0].Split('[')[1]

        $row.VMSIZE_GB = [math]::Round(($dev.CapacityInKB/1MB),2)

        $row.VMUSED_GB = [math]::Round(($diskTab[$dev.Key]/1GB),2)

        $row.PERCENT = [math]::Round(($row.VMUSED_GB/$row.VMSIZE_GB*100),2)

        $report += $row    }

  }

}

$report | Export-Csv "C:\Test.csv" -NoTypeInformation -UseCulture

And the errors: (I perform a connect-viserver prior to running the script). In both cases a report is produced

Get-View : Cannot validate argument on parameter 'VIObject'. The argument is null or empty. Supply an argument that is not null or empty and then try the command again.

At U:\Useful Scrips\VMware PowerCLI\ThinProvisionedVM's.ps1:6 char:27

+   $vmview = $vm | Get-View <<<<

    + CategoryInfo          : InvalidData: (:) [Get-View], ParameterBindingVal

   idationException

    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.DotNetInterop.GetVIView

Exception calling "Add" with "2" argument(s): "Key cannot be null.

Parameter name: key"

At U:\Useful Scrips\VMware PowerCLI\ThinProvisionedVM's.ps1:15 char:14

+     $diskTab.Add <<<< ($_.Key,$size)

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

    + FullyQualifiedErrorId : DotNetMethodException

Does anyone know what changes from 4.x to 5.x could result in the above behavior?

Thanks,


David

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

So the error is only appearing from some VMs and/or templates ?

Now is it possible to find out for which ones ?


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

View solution in original post

Reply
0 Kudos
8 Replies
LucD
Leadership
Leadership
Jump to solution

Afaik nothing changed in the Get-View cmdlet.

You can try to replace this line

  $vmview = $vm | Get-View

with

  $vmview = $vm.ExtensionData

and see if that makes a difference


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

AGDave
Enthusiast
Enthusiast
Jump to solution

Hi LucD,

I've changed that line but still get the same error. Nice thought though.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Which PowerCLI version are you using ? Do a

Get-PowerCLIVersion

Btw, just tested this in a vSphere 5 environment and the script seems to work there.


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

Reply
0 Kudos
AGDave
Enthusiast
Enthusiast
Jump to solution

PowerCLI Version

----------------

   VMware vSphere PowerCLI 5.1 Release 2 build 1012425

---------------

Snapin Versions

---------------

   VMWare AutoDeploy PowerCLI Component 5.1 build 768137

   VMWare ImageBuilder PowerCLI Component 5.1 build 768137

   VMware License PowerCLI Component 5.1 build 669840

   VMware VDS PowerCLI Component 5.1 build 1012428

   VMware vSphere PowerCLI Component 5.1 build 1012428

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

And you are sure there is something in $allvms ?


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

Reply
0 Kudos
AGDave
Enthusiast
Enthusiast
Jump to solution

Definitely. Executing the script does actually produce a report listing virtual disks, which is making the current error messages slightly perplexing.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

So the error is only appearing from some VMs and/or templates ?

Now is it possible to find out for which ones ?


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

Reply
0 Kudos
AGDave
Enthusiast
Enthusiast
Jump to solution

I put in a write-output for the VM name to catch the VM producing the error. It appears to be a VM which had no virtual disks attached, which explains the nature of the message. So in that way the script is running as expected. Thanks for your help Smiley Happy

Reply
0 Kudos