#该Python脚本主要用来生产格式化字符串,awk也可以实现,并且更加方便快捷。
#!/usr/bin/env python
#!_*_ coding:utf-8 _*_
file = open("host.txt")#host.txt是一个文本文件,里面是要添加的主机EXSI的ip地址,一个IP占一行,中间可以有空行。file是一个文件句柄
hosts = file.readlines()#file.readlines()返回一个迭代器,每一行包含特殊字符,所以后面要处理这些特殊字符。
for host in hosts: #遍历每一行中的元素
host=host.strip('\n') #去除该元素中的换行符
if host != '': #如果该元素是个空行,也去除
print "Add-VMHost -Server $myServer -Name %s -Location Datacenter -User root -Password HStc@root&2018 -Force" % host
#这个是将EXSi主机添加到vCenter中的语句,将所生产的所有的语句添加到".ps1"格式的文本中就可以放到powercli解析器下执行了
-----------------------------------------做好的实例----------------------------------------------------------------------------
$myServer = Connect-VIServer -Server 192.168.0.152 -User administrator@vsphere.local -Password 111111Qq. -Force
Add-VMHost -Server $myServer -Name 188.103.118.220 -Location Datacenter -User root -Password HStc@root&2018 -Force
Add-VMHost -Server $myServer -Name 188.103.118.219 -Location Datacenter -User root -Password HStc@root&2018 -Force
Add-VMHost -Server $myServer -Name 188.103.118.218 -Location Datacenter -User root -Password HStc@root&2018 -Force
Add-VMHost -Server $myServer -Name 188.103.118.217 -Location Datacenter -User root -Password HStc@root&2018 -Force
Add-VMHost -Server $myServer -Name 188.103.118.216 -Location Datacenter -User root -Password HStc@root&2018 -Force
具体的powercli命令查看帮助文档
PowerCLI E:\> get-help connect-viserver -examples
名称
Connect-VIServer
摘要
This cmdlet establishes a connection to a vCenter Server system.
-------------- Example 1 --------------
C:\PS>Connect-VIServer -Server 10.23.112.235 -Protocol https -User admin -Password pass
Connects to a vSphere server using the User and Password parameters.
PowerCLI E:\> get-help add-vmhost -examples
名称
Add-VMHost
摘要
This cmdlet adds a host to be managed by a vCenter Server system.
-------------- Example 1 --------------
C:\PS>$myServer = Connect-VIServer -Server 10.23.112.235
Add-VMHost -Server $myServer -Name MyVMHost1 -Location MyDatacenter1 -User MyUsername1 -Password MyPassword1
Adds a VM host to a specified vCenter Server system and provides a username and password for authentication.