powercli 学习随笔

powercli就不多介绍了,基于powershell的好东西,可以百度

#PowerShell基本语法

# 注释
<# .... #> 多行注释
$VAR 所有变量的声明或者使用都需要加上$
| 管道符,powershell拿到的都是对象,所以基于对象的传递和使用,请自行学习
$_ 当前对象,类似于某个语言的this,写错请纠正

#基本命令,随时补充

Get-command COMMAND_PART_NAME # 查找与参数相关的命令,可以使用通配符
help COMMAND_NAME # 查看帮助文档
$PSVersionTable # powershell版本信息,后续版本会弃用,建议用Get-Module
Get-Module # 查看已加载的模块及版本,如果有但没使用过,可能不显示
Get-Content FILE_PATH # 读取文件,并转成对象
Import-CSV # 导入csv文件

#关于PowerShell对象的理解

<#
自己很白话很粗浅的理解,如能帮助理解,纯属意外
用Import-csv来做演示,比如我有一个vms.csv,一共2列4行,第一列是name和ip,第2到4列分别是3台连接的虚拟机名和对应的ip
#>
# vms.csv
name,ip
vm1,10.0.0.1
vm2,10.0.0.2
vm3,10.0.0.3
# 说下这些数据的用途,有时候批量克虚拟机的时候,需要指定名字和IP,这个文件就是用来做定义的,省得写循环了
# 如何读取这个csv文件,并使用里面的数据呢
# 以下使用powershell终端
$VMS = Import-csv vms.csv # 引入csv文件,并赋值给$VMS
$VMS
<# 输出内容
name ip
------ --
vm1  10.0.0.1
vm2  10.0.0.2
vm3  10.0.0.3
这里显示的就不是用逗号分隔的了,而是以这种表格形式展示的对象
#>
# 取值
$VMS[0].name
<# 输出内容
vm1
#>
# 取值2
$VMS[0].name + "" + $VMS[0].ip
<# 输出内容
vm1:10.0.0.1
#>
# 这里可以看出来,Import-CSV的时候把符合条件的文件导入为一个组数对象,然后首行的为对象的参数名,下面行的为值,每行为一个,有多行,就有多个,而这里可以使用$VMS[0]这个下标来获取相关的对象
# 可以用一个foreach语言把所有数据全部取出来
foreach($VM in $VMS){
     $VM.name + "" + $VM.ip
}
<# 输出内容
vm1:10.0.0.1
vm2:10.0.0.2
vm3:10.0.0.3
#>

 

#安装PowerCLI,的PowerShell终端中操作,以下全是PowerShell 5.1环境下使用的

$PSVersionTable # 查看PowerShell版本,最好是5.1的,不是的话自己去升级吧
Install-Module -Name VMware.PowerCLI # 安装 Set-ExecutionPolicy RemoteSigned # 修改脚本执行权限为RemoteSigned

#基本命令

connect-viserver SERVER_NAME -USER USER_NAME -PASSWORD PASSWORD # 连接vcsa、esxi
get-vm # 获取虚拟机
new-vm # 新建虚拟机

#简单批量克隆

foreach($n in 1..10) # 遍历1-10
{
    new-vm -name vm$n -template temp1 -resourcepool resources -datastore datastore
}

 

#安装模块

起因有个需求,测试环境,旧集群集群迁到新集群。

想写个脚本,一批一批的把某个集群下的虚拟机关机,取消注册,在另外一套集群中启起来。

不要跟我说什么跨vcenter的迁移,独立sso安装,历史原因。

思路1:

按主机,一台上去关虚拟机-->取消注册-->去新集群-->找到存储-->找到相关目录-->注册虚拟机-->开机

思路2:

依次把主机从A vcenter直接加到B vcenter下,再做虚拟机vmotion,类似摆渡船

Get-Datastore | Search-Datastore
Search-Datastore –Datastore datastore1

找到案例,在存储里找文件,但是自己输的时候就报没有命令,后来查到需要VI-Module,下面这个git可以给powercli装modules

git clone https://github.com/rgel/PowerCLi.git

将clone下来的Vi-Module整个目录扔到你的用户文档WindowsPowerShell\Modules下,运行这个

Import-Module Vi-Module -Force -Verbose

Get-Command -module Vi-Module # 获取这个模块的命令清单,就验证一下是不是成功了

get-datastore iscsi_100G | Search-Datastore # 可以搜datastore了

 

 

参考文档:https://code.vmware.com/web/dp/tool/vmware-powercli/6.5.1

to be continue...

转载于:https://www.cnblogs.com/xddsq/p/10652055.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Key Features This is first book on the market that will enlighten you on the latest version of PowerCLI and how to implement it Effectively manage virtual machines, networks, and reports with the latest features of PowerCLI A comprehensive and practical book on automating VMware vSphere Book Description VMware vSphere PowerCLI, a free extension to Microsoft Windows PowerShell, enables you to automate the management of a VMware vSphere or vCloud environment. This book will show you how to automate your tasks and make your job easier. Starting with an introduction to the basics of PowerCLI, the book will teach you how to manage your vSphere and vCloud infrastructure from the command line. To help you manage a vSphere host overall, you will learn how to manage vSphere ESXi hosts, host profiles, host services, host firewall, and deploy and upgrade ESXi hosts using Image Builder and Auto Deploy. The next chapter will not only teach you how to create datastore and datastore clusters, but you'll also work with profile-driven and policy-based storage to manage your storage. To create a disaster recovery solution and retrieve information from vRealize Operations, you will learn how to use Site Recovery Manager and vRealize Operations respectively. Towards the end, you'll see how to use the REST APIs from PowerShell to manage NSX and vRealize Automation and create patch baselines, scan hosts against the baselines for missing patches, and re-mediate hosts. By the end of the book, you will be capable of using the best tool to automate the management and configuration of VMware vSphere. What you will learn Explore PowerShell and PowerCLI cmdlets and their output objects See how to manage virtual machines and work with virtual networks Manage vCloud Director from PowerCLI Use site recovery manager from PowerCLI to create a disaster recovery solution Managing NSX and vRealize Automation using REST API with PowerCLI Create and configure vSphere HA and DRS clusters Use vSphere Up
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值