[英]Connecting Mulitple VCenter Servers and collect information with Powercli
I have a list of VCenter servers. 我有一个VCenter服务器列表。 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.
我已经创建了一个文本文件,其中包含所有vcenter服务器和凭据,如下所示。我有20多个Vcenter服务器。 I need to collect information of VM, Datastores, etc.(for which I have scripts).
我需要收集VM,数据存储等信息(我有脚本)。
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. 我想连接到每个VCenter服务器并执行我的脚本。 Please help me.
请帮我。 Thanks in Advance.
提前致谢。
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. 之后,您可以将多个vCenter Server名称以字符串格式或数组格式传递给Connect-VIServer cmdlet的“ Server”参数。
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. 最后,由于您可能正在使用本地帐户而不是单个域帐户,因此可以考虑集成VICredentialStore。 This saves your credentials in an XML file that will be referenced automatically at time of authentication.
这样会将您的凭据保存在XML文件中,该文件将在身份验证时自动引用。
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
Suppose you have a top secret csv file where you store the connection info (ie vi server fqdn, logon user name and passwords) that looked like this: 假设您有一个顶级的csv文件,用于存储如下所示的连接信息(即vi服务器fqdn,登录用户名和密码):
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: 它保存在:
c:\\mysecretdocs\\custviservers.csv
您可以使用import-csv和一个foreach语句通过如下所示的函数来执行清单脏工作:
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. 您可以在那里运行任何PowerCLI清单或自定义命令,然后选择所需的任何输出,这只是使用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:
然后执行它,然后将输出放入csv中,如下所示:
get-vminventory | Export-Csv c:\mycustomerdata\vminfo.csv
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.