提问人:SuperZee 提问时间:9/7/2023 最后编辑:SuperZee 更新时间:9/8/2023 访问量:149
RVTools PowerShell
RVTools PowerShell
问:
我目前有一个powershell脚本,该脚本正在尝试使用RVTools powershell命令从VMware vCenter导出所有信息。
但是,我大约有 10 个 vcenter,而不是为每个 vcenter 复制这些行,我知道有一种方法可以用“foreach”($vcenters 中的 $vcenter)来做到这一点,然后将输出保存到每个目录\文件 ($vcenter 1Dir1 \ $vcenter 1File1) 但我没有足够的 PS 经验来写出来。
# Start cli of RVTools
Write-Host "Start export for vCenter $vcenter1 -ForegroundColor DarkYellow
$Arguments1 = "-u $User -p $EncryptedPassword -s $vcenter1 -c ExportvLicense2xlsx -d $vcenter1Dir1 -f $vcenter1File1"
Write-Host $Arguments1
$Process = Start-Process -FilePath ".\RVTools.exe" -ArgumentList $Arguments1 -NoNewWindow -Wait -PassThru
if($Process.ExitCode -eq -1)
{
Write-Host "Error: Export failed! RVTools returned exitcode -1, probably a connection error! Script is stopped" -ForegroundColor Red
exit 1
}
所以伸出援手,看看是否有人可以帮忙,我真的很感激。
非常感谢
答:
0赞
Travis Blubaugh
9/8/2023
#1
假设您当前的代码适用于单个运行(我没有尝试使用 RVtools,因为我没有它),最简单的方法是创建一个简单的数组、另一个变量,并可能编辑您的文件结构以使其工作。
#create the array
$vcenters = @('vc1','vc2','vc3') # keep going in that format to 10
$basedir = 'C:\temp\' # as an example, you can do whatever you want here.
#loop your code using the array data
foreach ($vc in $vcenters) # this is saying for each entry in your array call it $vc
{
### Your code below
# Start cli of RVTools
Write-Host "Start export for vCenter $vc -ForegroundColor DarkYellow"
#^ in the original the last quotation was missing, I added it. Also changed the
# $vcenter1 variable to $vc
$Arguments1 = "-u $User -p $EncryptedPassword -s $vc -c ExportvLicense2xlsx -d $basedir -f $vc"
<# edited the base directory to use the variable above and then up also the file to use just the variable name we are calling so it should look like
C:\temp\vc1.xlsx (assuming that is what the exportvLicense exports to...)
C:\temp\vc2.xlsx
and so on.
#>
Write-Host $Arguments1
$Process = Start-Process -FilePath ".\RVTools.exe" -ArgumentList $Arguments1 -NoNewWindow -Wait -PassThru
if($Process.ExitCode -eq -1)
{
Write-Host "Error: Export failed! RVTools returned exitcode -1, probably a connection error! Script is stopped" -ForegroundColor Red
exit 1
}
希望这可行,但如果您所追求的只是主机的许可证信息,您也可以使用此 powercli 命令来做到这一点......
#install the powercli module
install-module VMware.PowerCLI -Scope CurrentUser -Force #-SkipPublisherCheck
#variable
$vcenters = @('vc1','vc2','vc3') # keep going in that format to 10
#connect to each vcenter (assumes you have rights with the same account you opened the powershell window in.
foreach ($vc in $vcenters){connect-viserver $vc}
#Now get the license info for each host
get-vmhost | Select -property Name,LicenseKey
# now get the vcenter info... this is not my code I borrowed from
# http://vcloud-lab.com/entries/powercli/powercli-get-vcenter-licenses-information
foreach ($licenseManager in (Get-View LicenseManager)) #-Server $vCenter.Name
{
$vCenterName = ([System.uri]$licenseManager.Client.ServiceUrl).Host
#($licenseManager.Client.ServiceUrl -split '/')[2]
foreach ($license in $licenseManager.Licenses)
{
$licenseProp = $license.Properties
$licenseExpiryInfo = $licenseProp | Where-Object {$_.Key -eq 'expirationDate'} | Select-Object -ExpandProperty Value
if ($license.Name -eq 'Product Evaluation')
{
$expirationDate = 'Evaluation'
} #if ($license.Name -eq 'Product Evaluation')
elseif ($null -eq $licenseExpiryInfo)
{
$expirationDate = 'Never'
} #elseif ($null -eq $licenseExpiryInfo)
else
{
$expirationDate = $licenseExpiryInfo
} #else #if ($license.Name -eq 'Product Evaluation')
if ($license.Total -eq 0)
{
$totalLicenses = 'Unlimited'
} #if ($license.Total -eq 0)
else
{
$totalLicenses = $license.Total
} #else #if ($license.Total -eq 0)
$licenseObj = New-Object psobject
$licenseObj | Add-Member -Name Name -MemberType NoteProperty -Value $license.Name
$licenseObj | Add-Member -Name LicenseKey -MemberType NoteProperty -Value $license.LicenseKey
$licenseObj | Add-Member -Name ExpirationDate -MemberType NoteProperty -Value $expirationDate
$licenseObj | Add-Member -Name ProductName -MemberType NoteProperty -Value ($licenseProp | Where-Object {$_.Key -eq 'ProductName'} | Select-Object -ExpandProperty Value)
$licenseObj | Add-Member -Name ProductVersion -MemberType NoteProperty -Value ($licenseProp | Where-Object {$_.Key -eq 'ProductVersion'} | Select-Object -ExpandProperty Value)
$licenseObj | Add-Member -Name EditionKey -MemberType NoteProperty -Value $license.EditionKey
$licenseObj | Add-Member -Name Total -MemberType NoteProperty -Value $totalLicenses
$licenseObj | Add-Member -Name Used -MemberType NoteProperty -Value $license.Used
$licenseObj | Add-Member -Name CostUnit -MemberType NoteProperty -Value $license.CostUnit
$licenseObj | Add-Member -Name Labels -MemberType NoteProperty -Value $license.Labels
$licenseObj | Add-Member -Name vCenter -MemberType NoteProperty -Value $vCenterName
$licenseObj
} #foreach ($license in $licenseManager.Licenses)
}
评论
0赞
SuperZee
9/11/2023
有没有办法检查是否有任何重复的许可证密钥正在使用?并将该信息导出到单独的结果文件中?
0赞
SuperZee
9/12/2023
当我可以再次访问系统时,我将很快检查此代码。提前致谢
评论