Assign Custom Attributes to VMs

Featured

There was a requirement in one of the projects to assign Custom Attributes to multiple Virtual Machines hosted in a vSphere Environment. We wrote a PowerCLI script to Assign the Custom Attributes to Virtual Machines using a CSV file which had all the details of the Custom Attributes.

Requirements:

  • PowerCLI
  • PowerShell 5.1 or later

Script:

Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false
$cred = Get-Credential
Connect-VIServer vCenter_Server_IP_Address/FQDN -Credential $cred
$files = Import-CSV "C:\Users\testuser\Desktop\CustomAttributes.csv"
foreach ($file in $files)
{
$vm = Get-VM -Name $file.VMName
$vm | Set-Annotation -CustomAttribute "AppName" -Value $file.AppName
$vm | Set-Annotation -CustomAttribute "Owner" -Value $file.Owner
$vm | Set-Annotation -CustomAttribute "AppDL" -Value $file.AppDL
$vm | Set-Annotation -CustomAttribute "Env" -Value $file.ENv
}

Disconnect-VIServer -Confirm:$false

Input CSV File:

CustomAttributes.csv

Just replace the vCenter_Server_IP_Address/FQDN and Path of CSV File and run the script to assign Custom Attributes to Virtual Machines in vSphere environment. BOOM!!

Happy Scripting!!

Assign Custom Attributes to VMs

Featured

There was a requirement in one of the projects to assign Custom Attributes to multiple Virtual Machines hosted in a vSphere Environment. We wrote a PowerCLI script to Assign the Custom Attributes to Virtual Machines using a CSV file which had all the details of the Custom Attributes.

Requirements:

  • PowerCLI
  • PowerShell 5.1 or later

Script:

Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false
$cred = Get-Credential
Connect-VIServer vCenter_Server_IP_Address/FQDN -Credential $cred
$files = Import-CSV "C:\Users\testuser\Desktop\CustomAttributes.csv"
foreach ($file in $files)
{
$vm = Get-VM -Name $file.VMName
$vm | Set-Annotation -CustomAttribute "AppName" -Value $file.AppName
$vm | Set-Annotation -CustomAttribute "Owner" -Value $file.Owner
$vm | Set-Annotation -CustomAttribute "AppDL" -Value $file.AppDL
$vm | Set-Annotation -CustomAttribute "Env" -Value $file.ENv
}

Disconnect-VIServer -Confirm:$false

Input CSV File:

CustomAttributes.csv

Just replace the vCenter_Server_IP_Address/FQDN and Path of CSV File and run the script to assign Custom Attributes to Virtual Machines in vSphere environment. BOOM!!

Happy Scripting!!

vRealize Automation Managed VMs Report with Machine Owner’s ID

Featured

We were working on a requirement last week to pull a report of all vRealize Automation Managed Machines using PowervRA Module version 3.7.0 which supports vRealize Automation 7.6. While generating the report using a PowerShell script we noticed that the output was only showing the name of the Machine owner instead of the ID.

Then we started digging into the function Get-vRAResource of PowervRA Module and we noticed that the method which is used by function Get-vRAResource in PowervRA Module is /catalog-service/api/consumer/resourceViews

vRealize Automation API Method used by function Get-vRAResource in PowervRA

After checking the vRealize Automation Catalog Service API 7.6 documentation we realized that this method was deprecated in vRealize Automation 7.5 version and this method is similar to /catalog-service/api/consumer/resources method.

Method /api/consumer/resourceViews has been deprecated since version 7.5

We used our Good Old API building Platform Postman to do a side by side comparison of vRA API methods /catalog-service/api/consumer/resourceViews and /catalog-service/api/consumer/resources. If you’ll look at the output of both the methods closely, you’ll notice that the resourceViews methods returns only the Name of the Owner where as the resources methods returns an Array which includes the values of tenantName, ref, type & value out of which ref is the Machine Owner’s ID.

Side-by-Side Comparison of API Methods /api/consumer/resourceViews and /api/consumer/resources using Postman

Armed with all that information we prepared our script to pull resource Information of all vRA Managed Machines using PowervRA function Get-vRAResource and then we used the vRealize Automation API method /catalog-service/api/consumer/resources/{ResourceID} to get the Machine Owner’s ID of each Resource to generate our vRA Managed VMs report with Owner’s ID.

Script:

Set-ExecutionPolicy RemoteSigned

$cred = Get-Credential
$vRAFQDN = "vRA_FQDN"
$vRATenant = "TenantName"

#Connecting to vRA Server using PowervRA 3.7.0 Module
Connect-vRAServer -Server $vRAFQDN -Tenant $vRATenant -Username $cred.UserName -Password $cred.Password -IgnoreCertRequirements

#Fetching List of vRealize Automation Managed Virtual Machines
$filter = Get-vRAResource | where {$_.ResourceType -eq "Infrastructure.Virtual"}
$date = Get-Date -Format "yyyy-MM-dd-hh-mm-tt"
$print =  $filter | Select Data,Owners,ResourceId
$output = $print | Select @{Name="VMName";Expression={$_.Data.MachineName}}, @{Name="BusinessGroup";Expression={$_.Data.MachineGroupName}},`
@{Name="Owner";Expression={$_.Owners}}, @{Name="OwnersID";Expression={""}}, @{Name="ResourceID";Expression={$_.ResourceId}}, @{Name="ReservationName";Expression={$_.Data.MachineReservationName}},`
@{Name="vCPUs";Expression={$_.Data.MachineCPU}}, @{Name="Memory (MB)";Expression={$_.Data.MachineMemory}}, @{Name="Storage (GB)";Expression={$_.Data.MachineStorage}}
 
$output = $output | where {$_.VMName -ne $null}
 
$output | ft -AutoSize -Wrap

[System.Net.ServicePointManager]::ServerCertificateValidationCallback = { $True }
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add('Accept','application/json')
$headers.Add('Content-Type','application/json')
 
$Body = @{
    username = $cred.UserName
    password = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($cred.Password))
    tenant = $vRATenant
} | ConvertTo-Json
 
#Generating API Bearer Token for vRA Login
 
$url = "https://" + $vRAFQDN
$tokenurl = $url + "/identity/api/tokens"
$token = Invoke-RestMethod -Method Post -Uri $tokenurl -Body $Body -Headers $headers -Verbose
$token = $token.id
$headers.Add('Authorization',"Bearer $token")
$printvalue = @{}
$i=0;

#Get Machine Owner's ID for each Managed Machine

foreach ($item in $output)
{
    $owneridurl = $url + "/catalog-service/api/consumer/resources/" + $item.ResourceId
    $ownersId = Invoke-RestMethod -Method Get -Headers $headers -uri $owneridurl -Verbose
    $item.OwnersID = $ownersId.owners.ref
    $printvalue[$i] = $item
    $i = $i + 1
}

$printvalue.Values | FT -AutoSize -Wrap
 
$printvalue.Values | Export-Csv C:\Users\Administrator\Desktop\vRAManagedMachineReport-$date.csv -NoTypeInformation

Output:

Sample Output

Just replace the vRA_FQDN and TenantName with the details of your vRA environment and supply the output file path to generate the report.

Happy Scripting!!