用过azureapck的同学应该都清楚,SCVMM没有一键导出云虚拟机的信息,例如一个云中有100台虚拟机,那么我想知道这个云中所有虚拟机的名称、IP地址、虚拟机的所有者、内存多大、虚拟机所在的物理主机、和创建时间,只能一台一台去查,有些虚拟机的IP甚至要通过pwoershell去查,这里还有一个前提是你的云使用的是静态ip地址池,非常的麻烦,领导如果要你提供一个云中详细列表,可能就傻了。下面我们通过powershell,利用脚本在excel中导出scvmm中云虚拟机的所需信息:(如对azurepack感兴趣的同学可以加群我们一起学习)
先来看一个样例:(由于数据是真实数据,所以做了马赛克)
目的:利用计划任务,利用powershell脚本定时在D盘建立一个excel表格,导出SCVMM云中虚拟机的所需信息
首先检查自己的环境:是否安装excel2013,你的scvmm云是否使用的是ip地址池不是dhcp
用到SCVMM 的Powershell只有三个:
Get-SCCloud VMCloud | vm | sort Name #获取VMCloud中的虚拟机,以名称排序
$SCSIPAP=Get-SCStaticIPAddressPool -Name “ip pool” #获取静态IP地址池,注意紫色区域为变量,需要改为自己环境中的IP地址池的名称
Get-SCIPAddress -StaticIPAddressPool $SCSIPAP | Sort Description #IP地址池是描述进行排序
定义几个excel的变量 :
$excel = New-Object -ComObject Excel.Application
$excel.visible = $true #visible 属性让 Excel 显示出来
$workbook = $excel.workbooks.add()
$workbook.WorkSheets.item(1).Name = “VMinfo”
$sheet = $workbook.WorkSheets.item(“VMinfo”) #创建一个vminfo的excel工作本$x = 2 #从excel第二行开始写入
For($b = 1 ; $b -le 6 ; $b++)
{
$sheet.cells.item(1,$b).font.bold = $true
$sheet.cells.item(1,$b).borders.LineStyle = $lineStyle::xlDashDot
$sheet.cells.item(1,$b).borders.ColorIndex = $colorIndex::xlColorIndexAutomatic
}#设置excel表格样式,具体可以百度excel对像的用法
$sheet.cells.item(1,1) = “Name”
$sheet.cells.item(1,2) = “IPAddress”
$sheet.cells.item(1,3) = “Owner”
$sheet.cells.item(1,4) = “Memory”
$sheet.cells.item(1,5) = “VMHost”
$sheet.cells.item(1,6) = “AddedTime”
$y=0#excel1行1列为name依次类推
Foreach($ll in $vmname)
{
$sheet.cells.item($x, 1) = $vmname[$y].name
$sheet.cells.item($x, 2) = $ips[$y].Address
$sheet.cells.item($x, 3) = $vmname[$y].Owner
$sheet.cells.item($x, 4) = $vmname[$y].Memory
$sheet.cells.item($x, 5) = $vmname[$y].HostName
$sheet.cells.item($x, 6) = $vmname[$y].AddedTime
$x++
$y++
} #end foreach$range = $sheet.usedRange
$range.EntireColumn.AutoFit() | out-null
$strPath = “D:\vminfo.xlsx”#遍历Get-SCCloud VMCloud | vm | sort Name 并写入excel到D盘根目录
IF(Test-Path $strPath)
{
Remove-Item $strPath
$Excel.ActiveWorkbook.SaveAs($strPath)
}
ELSE
{
$Excel.ActiveWorkbook.SaveAs($strPath)
}
$Excel.Workbooks.Close()
$Excel.Quit()#检查D盘目录是否有相同的文件,如果有测覆盖
下面放上完整代码:
$excel = New-Object -ComObject Excel.Application
$excel.visible = $true
$workbook = $excel.workbooks.add()
$workbook.WorkSheets.item(1).Name = “VMinfo”
$sheet = $workbook.WorkSheets.item(“VMinfo”)
$x = 2
For($b = 1 ; $b -le 6 ; $b++)
{
$sheet.cells.item(1,$b).font.bold = $true
$sheet.cells.item(1,$b).borders.LineStyle = $lineStyle::xlDashDot
$sheet.cells.item(1,$b).borders.ColorIndex = $colorIndex::xlColorIndexAutomatic
}
$vmname = Get-SCCloud VMCloud | vm | sort Name
$SCSIPAP = Get-SCStaticIPAddressPool -Name “cloudnet”
$ips = Get-SCIPAddress -StaticIPAddressPool $SCSIPAP | Sort Description
$sheet.cells.item(1,1) = “Name”
$sheet.cells.item(1,2) = “IPAddress”
$sheet.cells.item(1,3) = “Owner”
$sheet.cells.item(1,4) = “Memory”
$sheet.cells.item(1,5) = “VMHost”
$sheet.cells.item(1,6) = “AddedTime”
$y=0
Foreach($ll in $vmname)
{
$sheet.cells.item($x, 1) = $vmname[$y].name
$sheet.cells.item($x, 2) = $ips[$y].Address
$sheet.cells.item($x, 3) = $vmname[$y].Owner
$sheet.cells.item($x, 4) = $vmname[$y].Memory
$sheet.cells.item($x, 5) = $vmname[$y].HostName
$sheet.cells.item($x, 6) = $vmname[$y].AddedTime
$x++
$y++
} #end foreach
$range = $sheet.usedRange
$range.EntireColumn.AutoFit() | out-null
$strPath = “D:\vminfo.xlsx”
IF(Test-Path $strPath)
{
Remove-Item $strPath
$Excel.ActiveWorkbook.SaveAs($strPath)
}
ELSE
{
$Excel.ActiveWorkbook.SaveAs($strPath)
}
$Excel.Workbooks.Close()
$Excel.Quit()最可将脚本保存为ps1,加入操作系统的计算任务,可以每天生成最新的Excel数据。
脚本不完善,如果有更了想法的同学可以再完善下(大喵对powershell还是个新手,请多多指教)