8 Replies Latest reply on Sep 25, 2020 10:27 PM by vmk2014

    Connecting multiple vCenter with encrypted credential

    vmk2014 Expert

      Hi All,

       

      I want to run the script for multiple vcenter with saved credential C:\credential.ps1 file. Looking for help to modify the script. Right now, i have to connect each vcenter and run the script.

       

      will the below command will work ? Any pointers will be much appreciated.

       

      $Creds = Get-Credential -Message "Enter username and password" -ErrorAction SilentlyContinue

      $vCenters = (Get-Content "C:\VC.txt")  

       

       

       

       

      $report = foreach ($vCenter in $vCenters) {

       

       

          Connect-VIServer $vCenter -Protocol https -Credential $Creds | Out-Null

       

       

      foreach($cluster in Get-Cluster){

          $esx = $cluster | Get-VMHost

        

         

          $cluster | Select        @{N="DCname";E={(Get-Datacenter -Cluster $cluster).Name}},

              @{N="Clustername";E={$cluster.Name}},

              @{N="Total Physical Memory (GB)";E={($esx | Measure-Object -Property MemoryTotalGB -Sum).Sum}},

              @{N="Configured Memory GB";E={($esx | Measure-Object -Property MemoryUsageGB -Sum).Sum}},

              @{N="Available Memory (GB)";E={($esx | Measure-Object -InputObject {$_.MemoryTotalGB - $_.MemoryUsageGB} -Sum).Sum}},

              @{N="Num of CPUs)";E={($esx | Measure-Object -Property NumCpu -Sum).Sum}},

              @{N="Total CPU (Mhz)";E={($esx | Measure-Object -Property CpuTotalMhz -Sum).Sum}},

              @{N="Configured CPU (Mhz)";E={($esx | Measure-Object -Property CpuUsageMhz -Sum).Sum}},

              @{N="Nuber of VMs";E={($esx | Get-VM).count}},

              @{N="Number of ESXi Hosts";E={$esx.count}}

       

       

       

       

              $endpoint = "https://api.powerbi.com/beta/a1f1e214-7ded-45b6-81a1-9e8ae3459641/datasets/6ab225bd-c265-4649-b971-a3a438d04b1f/rows?key=dL9orzw1YMWColkVEJRMjoosEIOC5ESr%2BkNovk4fJYMYAPNWE%2FweU1Aiu1YjGwFkozKKhuqCuDtbpZztuGJAJg%3D%3D"

      $payload = @{

      "Available Memory (GB)" =($esx | Measure-Object -InputObject {$_.MemoryTotalGB - $_.MemoryUsageGB} -Sum).Sum

      "Configured Memory GB" =($esx | Measure-Object -Property MemoryUsageGB -Sum).Sum

      "Total Physical Memory (GB)" =($esx | Measure-Object -Property MemoryTotalGB -Sum).Sum

      "Clustername" =$cluster.Name

      "Num of CPUs" =($esx | Measure-Object -Property NumCpu -Sum).Sum

      "Total CPU (Mhz)" =($esx | Measure-Object -Property CpuTotalMhz -Sum).Sum

      "Configured CPU (Mhz)" =($esx | Measure-Object -Property CpuUsageMhz -Sum).Sum

      "Datacenter" =(Get-Datacenter -Cluster $cluster).Name

      "Number of VMs" =($esx | Get-VM).count

      "Number of ESXi Hosts" =$esx.count

      }

      Invoke-RestMethod -Method Post -Uri "$endpoint" -Body (ConvertTo-Json @($payload))

       

       

              }

       

       

      Thanks

      V

        • 1. Re: Connecting multiple vCenter with encrypted credential
          LucD Guru
          vExpertCommunity WarriorsUser Moderators

          No, that will not work.

          The Credential parameter expects an object of type PSCredential.

           

          You could do

           

          $qCreds = Get-Credential -Message "Enter username and password" -ErrorAction SilentlyContinue

          $sCred = @{

              TypeName = 'PSCredential'

              ArgumentList = $qCreds.UserName, (ConvertTo-SecureString -String ($qCreds.GetNetworkCredential()).Password -AsPlainText -Force)

          }

          $Creds = New-Object @sCRed


          $vCenters = (Get-Content "C:\VC.txt")


          $report = foreach ($vCenter in $vCenters) {

              Connect-VIServer $vCenter -Protocol https -Credential $Creds | Out-Null

          Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
          • 2. Re: Connecting multiple vCenter with encrypted credential
            vmk2014 Expert

            Thank you, LucD.

             

            What if we store the credential using

             

             

            New-VICredentialStoreItem -Host vc -User admin -Password 'xyz'

             

            Do i need to change the script ?  I'm planning to run the script through Windows Tasks scheduler, so that it will run in the background no manual intervention required.  kindly help me.

             

             

            Thanks

            V

            • 3. Re: Connecting multiple vCenter with encrypted credential
              LucD Guru
              vExpertUser ModeratorsCommunity Warriors

              In that case, provided you created a VICredentialStoreItem for each vCenter, you should able to connect with just the Server parameter, no CRedential parameter needed.


              But note, the VICredentialStoreItem will need to be created by the same user under as the one under which you run the scheduled task.
              And that all has to be done on the same computer.

              Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
              • 4. Re: Connecting multiple vCenter with encrypted credential
                vmk2014 Expert

                Ok, i just need to add below command line, since i removing credential cmdlet

                 

                $vCenters = (Get-Content "C:\VC.txt")

                 

                $report = foreach ($vCenter in $vCenters) {

                    Connect-VIServer $vCenter -Protocol

                 

                }

                 

                Thanks

                V

                • 5. Re: Connecting multiple vCenter with encrypted credential
                  LucD Guru
                  Community WarriorsvExpertUser Moderators

                  Correct, but use -Protocol https or leave it out altogether.

                  Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                  • 6. Re: Connecting multiple vCenter with encrypted credential
                    vmk2014 Expert

                    LucD,

                     

                    Yes, i tried using the same, but it throws an error. Even after throwing an error i receive an output. Can it be fixed ?

                     

                    Connect-VIServer : Cannot validate argument on parameter 'Server'. The argument is null or empty. Provide an

                    argument that is not null or empty, and then try the command again.

                    At D:\abc\Mulitple-vCenter-BI.ps1:7 char:22

                    +     Connect-VIServer $vCenter -Protocol https -Credential $Creds | Ou ...

                    +                      ~~~~~~~~

                        + CategoryInfo          : InvalidData: (:) [Connect-VIServer], ParameterBindingValidationException

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

                       IServer

                     

                     


                    $vCenters = (Get-Content "C:\VC.txt")

                     

                    $report = foreach ($vCenter in $vCenters) {

                        Connect-VIServer $vCenter -Protocol https

                     

                     

                    }

                     

                     

                    Thanks

                    V

                    • 7. Re: Connecting multiple vCenter with encrypted credential
                      LucD Guru
                      vExpertCommunity WarriorsUser Moderators

                      Looks like you might have an empty line in your .txt file.

                      Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz