VMware Cloud Community
Vel_VMware
Enthusiast
Enthusiast
Jump to solution

PowerCLI script to fetch the required details.

Hi,

Can some one help me on the PowerCLI script to have below details, this is like I will give the servers list and it has to fetch the below details with respective to the VM's. Output in .CSV would be very fine.

  • vCenter
  • Cluster
  • Host
  • Powerstate- VM  power state should be ‘on’
  • OS according to the configuration file
  • DNS Name
  • Network #1 (Primary Network)
  • Network #2 (If available)
  • Network #3 (If available)
  • Network #4 (If available)
  • IP Address's
  • MAC Address
  • Tools version
  • HW version
  • Total number of disks
  • Provisioned MB
  • In Use MB
  • Multiwirter (If enabled return 'Yes', If not enabled return 'No')
  • Tags
  • Annotation
  • DRS Rule name (if VM is part of any DRS rule)
  • If direct - I/O path is enabled - (If there return 'Yes', If not there return 'No')
  • FT - (If there return 'Yes', If not there return 'No')
  • Snapshot - (If there return 'Yes', If not there return 'No') - Snapshot description.
  • Mounted ISO- (If there return 'Yes', If not there return 'No')
  • SCSI Bus- (If there return 'Yes', If not there return 'No')
  • Raw disk mapped (if any)- (If there return 'Yes', If not there return 'No')
34 Replies
LucD
Leadership
Leadership
Jump to solution

The VM's Name should be

$vms.VMName = $vm.Name

What other columns have the wrong values according to you?


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

0 Kudos
Vel_VMware
Enthusiast
Enthusiast
Jump to solution

Its not wrong value, I have just changed the dummy values to post it in the public forum.

Let me try with changing that as you suggested.

0 Kudos
Vel_VMware
Enthusiast
Enthusiast
Jump to solution

Getting below error..Smiley Sad

Get-Datacenter : Cannot bind parameter 'VM'. Cannot convert the "VMware.Vim.VirtualMachine" value of type

"VMware.Vim.VirtualMachine" to type "VMware.VimAutomation.ViCore.Types.V1.Inventory.VirtualMachine".

At T:\Script\Checks\PreCheckScript.ps1:35 char:43

+     $vms.Datacenter = (Get-Datacenter -VM $vm).Name

+                                           ~~~

    + CategoryInfo          : InvalidArgument: (:) [Get-Datacenter], ParameterBindingException

    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetDatacente

   r

Get-Datacenter : Cannot bind parameter 'VM'. Cannot convert the "VMware.Vim.VirtualMachine" value of type

"VMware.Vim.VirtualMachine" to type "VMware.VimAutomation.ViCore.Types.V1.Inventory.VirtualMachine".

At T:\Script\Checks\PreCheckScript.ps1:35 char:43

+     $vms.Datacenter = (Get-Datacenter -VM $vm).Name

+                                           ~~~

    + CategoryInfo          : InvalidArgument: (:) [Get-Datacenter], ParameterBindingException

    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetDatacente

   r

0 Kudos
LucD
Leadership
Leadership
Jump to solution

That is not the code I provided.

$vms.Datacenter = (Get-Datacenter -VM $vm.Name).Name


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

0 Kudos
Vel_VMware
Enthusiast
Enthusiast
Jump to solution

There was copy past mistake thats why missed to make in changes in script. Now, I have just ran through your codes only.

Apart from black area, I am not getting other values.

Also, Complaint and multiwriter values getting for 1 vm but not for others. Really I wonder, what is causing this.

pastedImage_0.png

0 Kudos
Vel_VMware
Enthusiast
Enthusiast
Jump to solution

I think, I am stressing you and very sorry for it.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

All those properties were in my original reply.
You removed them, so I assumed you didn't want them.

It's just a matter of copying the lines from my original reply into the latest version.


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

0 Kudos
Vel_VMware
Enthusiast
Enthusiast
Jump to solution

You are really great...! All works fine and need one last and final changes..

When I am having  VM's with multiple networks , am getting values like below. Can you help me to add block to get all network details in NIC wise if VM is having multiple network.

Also, please add one more block to check if VM is part of any DRS rule

pastedImage_0.png

0 Kudos
LucD
Leadership
Leadership
Jump to solution

To get multiple portgroups change that line to

$vms.Portgroup = if ($vm.Network) { (Get-View -Id $vm.Network -Property Name | Select-Object -ExpandProperty Name) -join '|' }


Not sure what exactly you want to see for DRS rule.
There are many types of DRS rules: affinity, anti-affinity, VM-to-VMHost. To report this in one column is quite tricky.
Have a look at Re: Need a script to pull the DRS rules, Vms on the rule, and if the rule is disabled then need an ...  to see what I mean.


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

0 Kudos
Vel_VMware
Enthusiast
Enthusiast
Jump to solution

I understand. We cannot get DRS rule config in single column on VM basis.

So far ...so good script work, this would be enough for now.

Thanks a looooooooooot for yourinstant help.. I would say again.. You are really great scripting in PowerCLI..

0 Kudos
Vel_VMware
Enthusiast
Enthusiast
Jump to solution

Hi LuCD,

One small doubt, I was trying to get the ourput CSV with current date as below, but not getting value.

Is here anything I am missing in method of calling variable?

$Date = Get-Date

$Report | Export-CSV -Path 'T:\Report_$Date.csv'  -NoTypeInformation -UseCulture

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You have to use double quotes if you want variable substitution.

$Date = Get-Date

$Report | Export-CSV -Path "T:\Report_$Date.csv"  -NoTypeInformation -UseCulture


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

0 Kudos
Vel_VMware
Enthusiast
Enthusiast
Jump to solution

Still I am getting below error.

Export-CSV : The given path's format is not supported.

At T:\Script.ps1:99 char:21

+   $Report | Export-CSV -Path "T:\Report_$Date.csv"

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : OpenError: (:) [Export-Csv], NotSupportedException

    + FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.ExportCsvCommand

0 Kudos
LucD
Leadership
Leadership
Jump to solution

That most probably because the resulting filename contains some blanks or slashes or other invalid characters for a file path.

Try specifying what kind of text appears in the filename.

For example

$date = Get-Date -Format 'yyyyMMdd'

$Report | Export-CSV -Path "T:\Report_$Date.csv"


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

Vel_VMware
Enthusiast
Enthusiast
Jump to solution

Yes it works, now..

Great.. ! Thanks ..

0 Kudos