1 2 3 Previous Next 34 Replies Latest reply on Mar 2, 2020 3:13 AM by Vel_VMware Go to original post
      • 15. Re: PowerCLI script to fetch the required details.
        Vel_VMware 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.

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

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

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

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

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

                 

                 

                Thanks in advance.

                • 20. Re: PowerCLI script to fetch the required details.
                  LucD Guru
                  Community WarriorsvExpertUser Moderators

                  The VM's Name should be

                   

                  $vms.VMName = $vm.Name

                   

                  What other columns have the wrong values according to you?

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

                    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.

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

                      Getting below error..

                       

                      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

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

                        That is not the code I provided.

                         

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

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

                          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.

                           

                           

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

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

                            • 26. Re: PowerCLI script to fetch the required details.
                              LucD Guru
                              vExpertUser ModeratorsCommunity Warriors

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

                                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

                                • 28. Re: PowerCLI script to fetch the required details.
                                  LucD Guru
                                  User ModeratorsCommunity WarriorsvExpert

                                  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 automatic email to be sent. Can you please help .  to see what I mean.

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

                                    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..