Vel_VMware
Enthusiast
Enthusiast

PowerCLI script to fetch the required details.

Jump to solution

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')
1 Solution

Accepted Solutions
LucD
Leadership
Leadership

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

View solution in original post

0 Kudos
34 Replies
LucD
Leadership
Leadership

What do you already have and where did you get stuck?


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

0 Kudos
Vel_VMware
Enthusiast
Enthusiast

Hi LuCD

I was searching the script to get all these details, unfortunately couldn't get, That's why I have posted here.

0 Kudos
Vel_VMware
Enthusiast
Enthusiast

Have you already created the script to fetch these details?

If so can you share me the link please.

0 Kudos
Vel_VMware
Enthusiast
Enthusiast

Have you already created the script to fetch these details?

If so can you share me the link please.

0 Kudos
Vel_VMware
Enthusiast
Enthusiast

Hi,

Can you some one help me with script please.

Thanks in advance.

0 Kudos
jpsider
Expert
Expert

This project should get you most of the way there. Sadly, what you are asking is specific to you, so you will need to finish/polish anything that could be provided. https://github.com/arielsanchezmora/vDocumentation

0 Kudos
Vel_VMware
Enthusiast
Enthusiast

Hi LuCD,

I have seen your script in one of your thread, can you just help me adding two more blocks.

1) Whether any VM's disks has enabled with multiwriter. (Just want to mark whether Yes/No in multi-writer column in CSV output).

2) Want to add validation column "Status" in output CSV, If any VM exist with snapshot or FT or multiwriter or hardware version lessthan 7, it has to say "Not         Compliance"

$report = @()

foreach($vm in Get-View -ViewType Virtualmachine){

    $vms = "" | Select-Object VMName, Hostname, IPAddress, OS, Boottime, VMState, TotalCPU, CPUAffinity,

         CPUHotAdd, CPUShare, CPUlimit, OverallCpuUsage, CPUreservation, TotalMemory, MemoryShare, MemoryUsage,

         MemoryHotAdd, MemoryLimit, MemoryReservation, Swapped, Ballooned, Compressed, TotalNics, ToolsStatus,

          ToolsVersion, HardwareVersion, TimeSync, CBT, Portgroup, VMHost, ProvisionedSpaceGB, UsedSpaceGB, Datastore,

          Notes, FaultTolerance, SnapshotName, SnapshotDate, SnapshotSizeGB, Owner, NB_last_backup

        

    $vms.VMName = $vm.Name

    $vms.Hostname = $vm.guest.hostname

    $vms.IPAddress = $vm.guest.ipAddress

    $vms.OS = $vm.Config.GuestFullName

    $vms.Boottime = $vm.Runtime.BootTime

    $vms.VMState = $vm.summary.runtime.powerState

    $vms.TotalCPU = $vm.summary.config.numcpu

    $vms.CPUAffinity = $vm.Config.CpuAffinity

    $vms.CPUHotAdd = $vm.Config.CpuHotAddEnabled

    $vms.CPUShare = $vm.Config.CpuAllocation.Shares.Level

    $vms.TotalMemory = $vm.summary.config.memorysizemb

    $vms.MemoryHotAdd = $vm.Config.MemoryHotAddEnabled

    $vms.MemoryShare = $vm.Config.MemoryAllocation.Shares.Level

    $vms.TotalNics = $vm.summary.config.numEthernetCards

    $vms.OverallCpuUsage = $vm.summary.quickStats.OverallCpuUsage

    $vms.MemoryUsage = $vm.summary.quickStats.guestMemoryUsage

    $vms.ToolsStatus = $vm.guest.toolsstatus

    $vms.ToolsVersion = $vm.config.tools.toolsversion

    $vms.TimeSync = $vm.Config.Tools.SyncTimeWithHost

    $vms.HardwareVersion = $vm.config.Version

    $vms.MemoryLimit = $vm.resourceconfig.memoryallocation.limit

    $vms.MemoryReservation = $vm.resourceconfig.memoryallocation.reservation

    $vms.CPUreservation = $vm.resourceconfig.cpuallocation.reservation

    $vms.CPUlimit = $vm.resourceconfig.cpuallocation.limit

    $vms.CBT = $vm.Config.ChangeTrackingEnabled

    $vms.Swapped = $vm.Summary.QuickStats.SwappedMemory

    $vms.Ballooned = $vm.Summary.QuickStats.BalloonedMemory

    $vms.Compressed = $vm.Summary.QuickStats.CompressedMemory

    $vms.Portgroup = Get-View -Id $vm.Network -Property Name | select -ExpandProperty Name

    $vms.VMHost = Get-View -Id $vm.Runtime.Host -property Name | select -ExpandProperty Name

    $vms.ProvisionedSpaceGB = [math]::Round(($vm.Summary.Storage.Committed + $vm.Summary.Storage.UnCommitted)/1GB,2)

    $vms.UsedSpaceGB = [math]::Round($vm.Summary.Storage.Committed/1GB,2)

    $vms.Datastore = $vm.Config.DatastoreUrl[0].Name

    $vms.Notes = $vm.Config.Annotation

    $vms.FaultTolerance = $vm.Runtime.FaultToleranceState

    $vms.SnapshotName = &{$script:snaps = Get-Snapshot -VM $vm.Name; $script:snaps.Name -join ','}

    $vms.SnapshotDate = $script:snaps.Created -join ','

    $vms.SnapshotSizeGB = $script:snaps.SizeGB -join ','

    $vms.Owner = (Get-TagAssignment -Category Owner -Entity $vm.Name).Tag.Name

    $vms.NB_last_backup =  Get-VM -Name $vm.Name | select -ExpandProperty Customfields | where{$_.Key -eq 'Last Backup'} | select -ExpandProperty Value

    $Report += $vms

}

$report

0 Kudos
LucD
Leadership
Leadership

Try like this

$report = @()

foreach($vm in Get-View -ViewType Virtualmachine){

    $vms = "" | Select-Object VMName, Hostname, IPAddress, OS, Boottime, VMState, TotalCPU, CPUAffinity,

         CPUHotAdd, CPUShare, CPUlimit, OverallCpuUsage, CPUreservation, TotalMemory, MemoryShare, MemoryUsage,

         MemoryHotAdd, MemoryLimit, MemoryReservation, Swapped, Ballooned, Compressed, TotalNics, ToolsStatus,

          ToolsVersion, HardwareVersion, TimeSync, CBT, Portgroup, VMHost, ProvisionedSpaceGB, UsedSpaceGB, Datastore,

          Notes, FaultTolerance, SnapshotName, SnapshotDate, SnapshotSizeGB, Owner, NB_last_backup,

          MultiWriter,Status

      

    $vms.VMName = $vm.Name

    $vms.Hostname = $vm.guest.hostname

    $vms.IPAddress = $vm.guest.ipAddress

    $vms.OS = $vm.Config.GuestFullName

    $vms.Boottime = $vm.Runtime.BootTime

    $vms.VMState = $vm.summary.runtime.powerState

    $vms.TotalCPU = $vm.summary.config.numcpu

    $vms.CPUAffinity = $vm.Config.CpuAffinity

    $vms.CPUHotAdd = $vm.Config.CpuHotAddEnabled

    $vms.CPUShare = $vm.Config.CpuAllocation.Shares.Level

    $vms.TotalMemory = $vm.summary.config.memorysizemb

    $vms.MemoryHotAdd = $vm.Config.MemoryHotAddEnabled

    $vms.MemoryShare = $vm.Config.MemoryAllocation.Shares.Level

    $vms.TotalNics = $vm.summary.config.numEthernetCards

    $vms.OverallCpuUsage = $vm.summary.quickStats.OverallCpuUsage

    $vms.MemoryUsage = $vm.summary.quickStats.guestMemoryUsage

    $vms.ToolsStatus = $vm.guest.toolsstatus

    $vms.ToolsVersion = $vm.config.tools.toolsversion

    $vms.TimeSync = $vm.Config.Tools.SyncTimeWithHost

    $vms.HardwareVersion = $vm.config.Version

    $vms.MemoryLimit = $vm.resourceconfig.memoryallocation.limit

    $vms.MemoryReservation = $vm.resourceconfig.memoryallocation.reservation

    $vms.CPUreservation = $vm.resourceconfig.cpuallocation.reservation

    $vms.CPUlimit = $vm.resourceconfig.cpuallocation.limit

    $vms.CBT = $vm.Config.ChangeTrackingEnabled

    $vms.Swapped = $vm.Summary.QuickStats.SwappedMemory

    $vms.Ballooned = $vm.Summary.QuickStats.BalloonedMemory

    $vms.Compressed = $vm.Summary.QuickStats.CompressedMemory

    $vms.Portgroup = Get-View -Id $vm.Network -Property Name | select -ExpandProperty Name

    $vms.VMHost = Get-View -Id $vm.Runtime.Host -property Name | select -ExpandProperty Name

    $vms.ProvisionedSpaceGB = [math]::Round(($vm.Summary.Storage.Committed + $vm.Summary.Storage.UnCommitted)/1GB,2)

    $vms.UsedSpaceGB = [math]::Round($vm.Summary.Storage.Committed/1GB,2)

    $vms.Datastore = $vm.Config.DatastoreUrl[0].Name

    $vms.Notes = $vm.Config.Annotation

    $vms.FaultTolerance = $vm.Runtime.FaultToleranceState

    $vms.SnapshotName = &{$script:snaps = Get-Snapshot -VM $vm.Name; $script:snaps.Name -join ','}

    $vms.SnapshotDate = $script:snaps.Created -join ','

    $vms.SnapshotSizeGB = $script:snaps.SizeGB -join ','

    $vms.Owner = (Get-TagAssignment -Category Owner -Entity $vm.Name).Tag.Name

    $vms.NB_last_backup =  Get-VM -Name $vm.Name | select -ExpandProperty Customfields | where{$_.Key -eq 'Last Backup'} | select -ExpandProperty Value

    $vms.MultiWriter = if($vm.Config.Hardware.Device | where{$_ -is [VMware.Vim.VirtualDisk] -and $_.Backing.Sharing -eq 'sharingMultiWriter'}){'Yes'}else{'No'}

    $vms.Status = if($vms.SnapshotName -ne '' -or $vms.FaultTolerance -ne 'NotConfigured' -or $vms.MultiWriter -eq 'Yes' -or [int]($vms.HardwareVersion.Split('-')[1]) -lt 7){'Not compliant'}else{'Compliant'}

    $Report += $vms

}

$report


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

Vel_VMware
Enthusiast
Enthusiast

Hi LuCD,

Added your blocks and modified the script for taking input file. But, getting some errors and posted here as well.

Script file:

$PreCheckReport = @()

$Data = Import-CSV -Path 'T:\Script\Checks\Servers.csv' -delimiter “;”

$Server = Read-Host  -Prompt 'Enter vCenter Server'

$Cre = Get-Credential -Message "!!! Greetings $Name !!!"

try{

    Connect-VIServer -server $server -credential $Cre -ErrorAction Stop

}

catch {

    throw "Connection to $Server failed!"

}

$Data | foreach {

$Servers = $_.VMName

$vm = Get-VM -Name $Servers

foreach($vm in Get-View -ViewType Virtualmachine){

   $vms = "" | Select-Object VMName, Datacenter, vCenter, Hostname, IPAddress, OS, VMState, TotalCPU, CPUAffinity, TotalMemory, TotalNics, ToolsStatus,

           ToolsVersion, HardwareVersion, CBT, Portgroup, VMHost, ProvisionedSpaceGB, UsedSpaceGB, Datastore,

           Notes, FaultTolerance, SnapshotName, SnapshotDate, NB_Backup, Tags, MultiWriter, HCXMigrationCompliance

    $vms.VMName = $vm

    $vms.Datacenter = Get-Datacenter $vm

    $vms.vCenter = $Server

    $vms.Hostname = $vm.guest.hostname

    $vms.IPAddress = $vm.guest.ipAddress

    $vms.TotalCPU = $vm.summary.config.numcpu

    $vms.CPUAffinity = $vm.Config.CpuAffinity

    $vms.TotalMemory = $vm.summary.config.memorysizemb

    $vms.TotalNics = $vm.summary.config.numEthernetCards

    $vms.Portgroup = Get-View -Id $vm.Network -Property Name | select -ExpandProperty Name

    $vms.CBT = $vm.Config.ChangeTrackingEnabled

    $vms.FaultTolerance = $vm.Runtime.FaultToleranceState

    $vms.SnapshotName = &{$script:snaps = Get-Snapshot -VM $vm.Name; $script:snaps.Name -join ','}

    $vms.SnapshotDate = $script:snaps.Created -join ','

    $vms.NB_Backup =  Get-VM -Name $vm.Name | select -ExpandProperty Customfields | where{$_.Key -eq 'NB_LAST_BACKUP'} | select -ExpandProperty Value

    $vms.MultiWriter = if($vm.Config.Hardware.Device | where{$_ -is [VMware.Vim.VirtualDisk] -and $_.Backing.Sharing -eq 'sharingMultiWriter'}){'Yes'}else{'No'}

    $vms.HCXMigrationCompliance = if($vms.SnapshotName -ne '' -or $vms.FaultTolerance -ne 'NotConfigured' -or $vms.MultiWriter -eq 'Yes' -or [int] ($vms.HardwareVersion.Split('-')[1]) -lt 7){'Not compliant'}else{'Compliant'} 

    $Date = Get-date

    $PreCheckReportReport += $vms

}

}

$PreCheckReport | Export-CSV -Path 'T:\Script\Checks\PreCheckReport_$Date.csv'

Errors:

Get-Datacenter : 25-02-2020 14:39:36    Get-Datacenter        Datacenter with name 'VMware.Vim.VirtualMachine' was not

found using the specified filter(s).

At T:\Script\Checks\PreCheckScript.ps1:30 char:23

+     $vms.Datacenter = Get-Datacenter $vm

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

    + CategoryInfo          : ObjectNotFound: (:) [Get-Datacenter], VimException

    + FullyQualifiedErrorId : Core_OutputHelper_WriteNotFoundError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetDat

   acenter

You cannot call a method on a null-valued expression.

At T:\Script\Checks\PreCheckScript.ps1:60 char:38

+     $vms.HCXMigrationCompliance = if($vms.SnapshotName -ne '' -or $vms.FaultTole ...

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

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

    + FullyQualifiedErrorId : InvokeMethodOnNull

Get-Datacenter : 25-02-2020 14:39:40    Get-Datacenter        Datacenter with name 'VMware.Vim.VirtualMachine' was not

found using the specified filter(s).

At T:\Script\Checks\PreCheckScript.ps1:30 char:23

+     $vms.Datacenter = Get-Datacenter $vm

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

    + CategoryInfo          : ObjectNotFound: (:) [Get-Datacenter], VimException

    + FullyQualifiedErrorId : Core_OutputHelper_WriteNotFoundError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetDat

   acenter

You cannot call a method on a null-valued expression.

At T:\Script\Checks\PreCheckScript.ps1:60 char:38

+     $vms.HCXMigrationCompliance = if($vms.SnapshotName -ne '' -or $vms.FaultTole ...

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

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

    + FullyQualifiedErrorId : InvokeMethodOnNull

Method invocation failed because [System.Management.Automation.PSObject] does not contain a method named 'op_Addition'.

At T:\Script\Checks\PreCheckScript.ps1:64 char:5

+     $PreCheckReportReport += $vms

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

    + CategoryInfo          : InvalidOperation: (op_Addition:String) [], RuntimeException

    + FullyQualifiedErrorId : MethodNotFound

Get-Datacenter : 25-02-2020 14:39:44    Get-Datacenter        Datacenter with name 'VMware.Vim.VirtualMachine' was not

found using the specified filter(s).

At T:\Script\Checks\PreCheckScript.ps1:30 char:23

+     $vms.Datacenter = Get-Datacenter $vm

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

    + CategoryInfo          : ObjectNotFound: (:) [Get-Datacenter], VimException

    + FullyQualifiedErrorId : Core_OutputHelper_WriteNotFoundError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetDat

   acenter

0 Kudos
LucD
Leadership
Leadership

You also added the Datacenter property.

That should be done like this

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


You also have a typo in

$PreCheckReportReport += $vms

that should be

$PreCheckReport += $vms


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

0 Kudos
Vel_VMware
Enthusiast
Enthusiast

Hi LuCD,

Atlast getting only one error now as below. Also, will it fetch all NIC and portgroup details if VM is having multiple network?

Get-View : Cannot validate argument on parameter 'Id'. The argument is null or empty. Provide an argument that is not

null or empty, and then try the command again.

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

+     $vms.Portgroup = Get-View -Id $vm.Network -Property Name | select -ExpandPro ...

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

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

    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.DotNetInte

   rop.GetVIView

Get-View : Cannot validate argument on parameter 'Id'. The argument is null or empty. Provide an argument that is not

null or empty, and then try the command again.

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

+     $vms.Portgroup = Get-View -Id $vm.Network -Property Name | select -ExpandPro ...

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

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

    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.DotNetInte

   rop.GetVIView

Get-View : Cannot validate argument on parameter 'Id'. The argument is null or empty. Provide an argument that is not

null or empty, and then try the command again.

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

+     $vms.Portgroup = Get-View -Id $vm.Network -Property Name | select -ExpandPro ...

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

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

    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.DotNetInte

   rop.GetVIView

0 Kudos
LucD
Leadership
Leadership

Do you have VMs with a vNIC that is not connected to a Portgroup?

Replace the line with

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


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

0 Kudos
Vel_VMware
Enthusiast
Enthusiast

Hi LuCD,

I think script works. But I am not getting any response or any errors as well, even script execution is not getting end also

I have just tested with 3 VM's, I wonder, for these 3 VM's will it take this much time to collect the data? I am not sure..

0 Kudos
LucD
Leadership
Leadership

No, this should be relatively quick.

When I test run the script it seems to work (except for the Custom Attributes (I do not have).

Perhaps attach your current version of the script (as a file).


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

0 Kudos
Vel_VMware
Enthusiast
Enthusiast

Hi LuCD,

I have attached the script file. Can you add one more thing, to fetch network portgroup details on vNic wise if VM is having multiple network.

0 Kudos
Vel_VMware
Enthusiast
Enthusiast

I am unable to attach the document, I don't know why.

$PreCheckReport = @()

$Data = Import-CSV -Path 'T:\Script\Checks\Servers.csv' -delimiter “;”

$Server = Read-Host  -Prompt 'Enter vCenter Server'

$Cre = Get-Credential -Message "!!! Greetings $Name !!!"

try{

    Connect-VIServer -server $server -credential $Cre -ErrorAction Stop

}

catch {

    throw "Connection to $Server failed!"

}

$Data | foreach {

$Servers = $_.VMName

foreach($vm in Get-View -ViewType Virtualmachine){

   $vms = "" | Select-Object VMName, Datacenter, vCenter, Hostname, IPAddress, OS, VMState, TotalCPU, CPUAffinity, TotalMemory, TotalNics, ToolsStatus,

           ToolsVersion, HardwareVersion, CBT, Portgroup, VMHost, ProvisionedSpaceGB, UsedSpaceGB, Datastore,

           Notes, FaultTolerance, SnapshotName, SnapshotDate, NB_Backup, Tags, MultiWriter, Status

    $vm = Get-VM -Name $Servers

    $vms.VMName = $vm

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

    $vms.vCenter = $Server

    $vms.Hostname = $vm.guest.hostname

    $vms.IPAddress = $vm.guest.ipAddress

    $vms.TotalCPU = $vm.summary.config.numcpu

    $vms.CPUAffinity = $vm.Config.CpuAffinity

    $vms.TotalMemory = $vm.summary.config.memorysizemb

    $vms.TotalNics = $vm.summary.config.numEthernetCards

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

    $vms.CBT = $vm.Config.ChangeTrackingEnabled

    $vms.FaultTolerance = $vm.Runtime.FaultToleranceState

    $vms.SnapshotName = &{$script:snaps = Get-Snapshot -VM $vm.Name; $script:snaps.Name -join ','}

    $vms.SnapshotDate = $script:snaps.Created -join ','

    $vms.NB_Backup =  Get-VM -Name $vm.Name | select -ExpandProperty Customfields | where{$_.Key -eq 'NB_LAST_BACKUP'} | select -ExpandProperty Value

    $vms.MultiWriter = if($vm.Config.Hardware.Device | where{$_ -is [VMware.Vim.VirtualDisk] -and $_.Backing.Sharing -eq 'sharingMultiWriter'}){'Yes'}else{'No'}

    $vms.Status = if($vms.SnapshotName -ne '' -or $vms.FaultTolerance -ne 'NotConfigured' -or $vms.MultiWriter -eq 'Yes' -or [int]($vms.HardwareVersion.Split('-')[1]) -lt 7){'Not compliant'}else{'Compliant'}

  

    $Date = Get-date

    $PreCheckReport += $vms

}

}

$PreCheckReport | Export-CSV -Path 'T:\Script\Checks\PreCheckReport_$Date.csv'

Disconnect-VIServer $Server -Confirm:$false

0 Kudos
LucD
Leadership
Leadership

You can't remove properties (HardwareVersion) that are used later on (Status).

The Portgroup property was there, but you removed it in this latest version.

In any case, the below works for me.

$PreCheckReport = @()

$Data = Import-Csv -Path 'T:\Script\Checks\Servers.csv' -delimiter “;

$Server = Read-Host  -Prompt 'Enter vCenter Server'

$Cre = Get-Credential -Message "!!! Greetings $Name !!!"

try {

    Connect-VIServer -server $server -credential $Cre -ErrorAction Stop

}

catch {

    throw "Connection to $Server failed!"

}

$filter = $Data.VMName -join '|'

foreach ($vm in Get-View -ViewType Virtualmachine -Filter @{'Name' = $filter }) {

    $vms = "" | Select-Object VMName, Datacenter, vCenter, Hostname, IPAddress, OS, VMState, TotalCPU, CPUAffinity, TotalMemory, TotalNics, ToolsStatus,

    ToolsVersion, HardwareVersion, CBT, Portgroup, VMHost, ProvisionedSpaceGB, UsedSpaceGB, Datastore,

    Notes, FaultTolerance, SnapshotName, SnapshotDate, NB_Backup, Tags, MultiWriter, Status

    $vms.VMName = $vm

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

    $vms.vCenter = $Server

    $vms.Hostname = $vm.guest.hostname

    $vms.IPAddress = $vm.guest.ipAddress

    $vms.TotalCPU = $vm.summary.config.numcpu

    $vms.CPUAffinity = $vm.Config.CpuAffinity

    $vms.TotalMemory = $vm.summary.config.memorysizemb

    $vms.TotalNics = $vm.summary.config.numEthernetCards

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

    $vms.CBT = $vm.Config.ChangeTrackingEnabled

    $vms.HardwareVersion = $vm.config.Version

    $vms.FaultTolerance = $vm.Runtime.FaultToleranceState

    $vms.SnapshotName = & { $script:snaps = Get-Snapshot -VM $vm.Name; $script:snaps.Name -join ',' }

    $vms.SnapshotDate = $script:snaps.Created -join ','

    $vms.NB_Backup = Get-VM -Name $vm.Name | Select-Object -ExpandProperty Customfields | Where-Object { $_.Key -eq 'NB_LAST_BACKUP' } | Select-Object -ExpandProperty Value

    $vms.MultiWriter = if ($vm.Config.Hardware.Device | Where-Object { $_ -is [VMware.Vim.VirtualDisk] -and $_.Backing.Sharing -eq 'sharingMultiWriter' }) { 'Yes' }else { 'No' }

    $vms.Status = if ($vms.SnapshotName -ne '' -or $vms.FaultTolerance -ne 'NotConfigured' -or $vms.MultiWriter -eq 'Yes' -or [int]($vms.HardwareVersion.Split('-')[1]) -lt 7) { 'Not compliant' }else { 'Compliant' }

    $PreCheckReport += $vms

}

$PreCheckReport | Export-Csv -Path 'T:\Script\Checks\PreCheckReport_$Date.csv'

Disconnect-VIServer $Server -Confirm:$false


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

Vel_VMware
Enthusiast
Enthusiast

Great..! let me check this and will update you.

0 Kudos
Vel_VMware
Enthusiast
Enthusiast

By the it works and got output.

Not getting values on the below fields and field VMName getting  "VMware.Vim.VirtualMachine" for all VM's.

I have changed dummy values in the output which ever values I got.

pastedImage_3.png

Thanks in advance.

0 Kudos