VMware Cloud Community
vmk2014
Expert
Expert
Jump to solution

Connecting multiple vCenter with encrypted credential

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

$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

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

Reply
0 Kudos
8 Replies
LucD
Leadership
Leadership
Jump to solution

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: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
vmk2014
Expert
Expert
Jump to solution

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

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
vmk2014
Expert
Expert
Jump to solution

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

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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


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

Reply
0 Kudos
vmk2014
Expert
Expert
Jump to solution

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

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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


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

Reply
0 Kudos
vmk2014
Expert
Expert
Jump to solution

Thank you. LucD.

Reply
0 Kudos