1

I have a list of VCenter servers. They are on different locations and of different customers. I have created a text file with all the vcenter servers and credentials like below..I have more than 20 Vcenter Servers. I need to collect information of VM, Datastores, etc.(for which I have scripts).

Connect-VIServer vcenter0001 -User vcenter0001\sysdep -Password "Passwowrd1"
Connect-VIServer vcenter0002 -User vcenter0002\sysdep -Password "Passwowrd2"

I want to connect to each VCenter server and execute my scripts. Please help me. Thanks in Advance.

Subhantech
  • 67
  • 1
  • 8

2 Answers2

1

There's a couple ways to accomplish this, first you need to make sure that your configuration is set to allow for multiple connections. This is done with the following:

Set-PowerCLIConfiguration -DefaultVIServerMode Multiple

Note: It may also be necessary to run the following to enforce the change against all session scopes:

Set-PowerCLIConfiguration -DefaultVIServerMode Multiple -Scope User
Set-PowerCLIConfiguration -DefaultVIServerMode Multiple -Scope Session

Afterwards, you can pass multiple vCenter server names in string format or array format to the Connect-VIServer cmdlet to the 'Server' parameter.

Example using strings:

Connect-VIServer -Server vcenter0001,vcenter0002,vcenter0003 -User sysdep -Password "Password"

Example using an array:

$vCenterNames = @('vcenter0001','vcenter0002','vcenter0003')
Connect-VIServer -Server $vCenterNames -User sysdep -Password "Password"

Lastly, since it looks like you may be using local accounts instead of a single domain account, you could look at integrating the VICredentialStore. This saves your credentials in an XML file that will be referenced automatically at time of authentication.

Example Usage:

New-VICredentialStoreItem -Host vcenter0001 -User vcenter0001\sysdep -Password "Password"
New-VICredentialStoreItem -Host vcenter0002 -User vcenter0002\sysdep -Password "Password"
New-VICredentialStoreItem -Host vcenter0003 -User vcenter0003\sysdep -Password "Password"
Connect-VIServer -Server vcenter0001,vcenter0002,vcenter0003
Kyle Ruddy
  • 1,886
  • 1
  • 7
  • 5
  • Thank you Kyle, In my case the usernames and passwords can also change.. each vcenter can have different user instead of sysdep and machine generated passwords. – Subhantech Jul 27 '17 at 12:48
1

Suppose you have a top secret csv file where you store the connection info (i.e. vi server fqdn, logon user name and passwords) that looked like this:

viserver, username, password
myfav.cust1.org, cust1usr, cust1pw
my2fav.cust2.net, cust2usr, cust2pw
myleastfav.cust3.com, cust3usr, cust3pw

and it was saved in: c:\mysecretdocs\custviservers.csv you could use import-csv and a foreach statement to do your inventory dirty work with a function that looked something like this:

function get-vminventory
{
    $viCntinfo = Import-Csv c:\mysecretdocs\custviservers.csv
    foreach ($vi in $viCntInfo)
    {
        $convi = connect-viserver -server $vi.viserver -username $vi.username -password $vi.password
        $vms = get-vm
        $vms | select name, MemoryGB, NumCpu,
            @{ n = "hostname"; e = { $_.guest.hostname } },
            @{ n = "ip"; e = { $_.guest.ipaddress -join ", " } },
            @{ n = "viserver"; e = { $convi.Name } }
        $discvi = disconnect-viserver -server * -force -confirm:$false
    }
}

You can run any of the PowerCLI inventory or custom commands there and select whatever output you want, that's just an example using Get-VM. Either dot source the function or just paste it into your shell. Then execute it and put the output in a csv like this:

get-vminventory | Export-Csv c:\mycustomerdata\vminfo.csv 
brendan62269
  • 1,046
  • 1
  • 9
  • 12