python实现最简单个格式化打印脚本,该脚本可以用来批量添加exsi主机到vCenter中。

#该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.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值