RVTools PowerShell

626 views Asked by At

I currently have a powershell script that is trying to export all the information from VMware vCenter utilising the RVTools powershell commands.

However I have approximately 10 vcenters and instead of copying these lines out for each vcenter, I know there is a way to do this with "foreach ($vcenter in $vcenters) and then save the output to each directory\file ($vcenter1Dir1 \ $vcenter1File1) but I'm not experienced enough with PS to write this out.

# 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
}

So was reaching out to see if someone could help with this, I'd really appreciate it.

Many Thanks

1

There are 1 answers

2
Travis Blubaugh On

The easiest way to do this assuming your current code works for individual runs (I didn't try it with RVtools as I don't have it) would be to create a simple array, another variable and possibly edit your file structure to make this work.

#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
}

Hopefully that works, but if all you are after is license info for hosts, you can do that with this powercli command as well...

#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)
}