1 2 3 Previous Next 34 Replies Latest reply on Mar 2, 2020 3:13 AM by Vel_VMware

    PowerCLI script to fetch the required details.

    Vel_VMware Enthusiast

      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. Re: PowerCLI script to fetch the required details.
          LucD Guru
          User ModeratorsvExpertCommunity Warriors

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

          Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
          • 2. Re: PowerCLI script to fetch the required details.
            Vel_VMware Enthusiast

            Hi LuCD

             

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

            • 3. Re: PowerCLI script to fetch the required details.
              Vel_VMware Enthusiast

              Have you already created the script to fetch these details?

              If so can you share me the link please.

              • 4. Re: PowerCLI script to fetch the required details.
                Vel_VMware Enthusiast

                Have you already created the script to fetch these details?

                If so can you share me the link please.

                • 5. Re: PowerCLI script to fetch the required details.
                  Vel_VMware Enthusiast

                  Hi,

                   

                  Can you some one help me with script please.

                   

                  Thanks in advance.

                  • 6. Re: PowerCLI script to fetch the required details.
                    jpsider Hot Shot
                    vExpert

                    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

                    • 7. Re: PowerCLI script to fetch the required details.
                      Vel_VMware 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

                      • 8. Re: PowerCLI script to fetch the required details.
                        LucD Guru
                        Community WarriorsUser ModeratorsvExpert

                        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: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                        • 9. Re: PowerCLI script to fetch the required details.
                          Vel_VMware 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

                          • 10. Re: PowerCLI script to fetch the required details.
                            LucD Guru
                            vExpertCommunity WarriorsUser Moderators

                            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: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                            • 11. Re: PowerCLI script to fetch the required details.
                              Vel_VMware 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

                              • 12. Re: PowerCLI script to fetch the required details.
                                LucD Guru
                                User ModeratorsvExpertCommunity Warriors

                                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: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                                • 13. Re: PowerCLI script to fetch the required details.
                                  Vel_VMware 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..

                                  • 14. Re: PowerCLI script to fetch the required details.
                                    LucD Guru
                                    Community WarriorsUser ModeratorsvExpert

                                    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: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                                    1 2 3 Previous Next