Skip to content

Instantly share code, notes, and snippets.

@BenNeise
Last active August 11, 2016 14:01
Show Gist options
  • Save BenNeise/2f923ac582c4683c50fd63f4dc45572b to your computer and use it in GitHub Desktop.
Save BenNeise/2f923ac582c4683c50fd63f4dc45572b to your computer and use it in GitHub Desktop.
PowerShell function to return vRA Virtual Machine objects via the IaaS oData interface.
function Get-IaaSVirtualMachine {
<#
.SYNOPSIS
Gets information about a virtual machine from vRA's IaaS oData interface.
.DESCRIPTION
Gets information about a virtual machine from vRA's IaaS oData interface. This includes the option to get data from linked tables.
Which includes custom properties
.PARAMETER Server
The IaaS Server
.PARAMETER Name
Searches for a specific virtual machine
.PARAMETER Credential
Credentials for use on the IaaS server.
.PARAMETER IncludeCustomProperties
Specifies whether or not custom properties should be included. It does make the script take a lot longer to run.
.EXAMPLE
.NOTES
Contributors: Ben Neise
#>
[cmdletBinding()]
param (
[parameter(Mandatory = $true,
Position = 0
)]
[String]
$Server,
[parameter(Mandatory = $false,
Position = 1
)]
[String]
$Name,
[parameter(Mandatory = $false,
Position = 2
)]
[PSCredential]
$Credential,
[parameter(Mandatory = $false,
Position = 3
)]
[switch]
$IncludeCustomProperties
)
begin {
# Empty array for storing virtual machines
$arrvirtualMachines = @()
# Properties of the oData object
$arrProperties = @(
"VirtualMachineName",
"InitiatorType",
"Notes",
"GuestOS",
"VMUniqueID",
"PlatformDetails",
"VirtualMachineState",
"VMDNSName",
"ExternalReferenceId",
"StoragePath",
"GuestOSFamily"
)
# Properties of the oData object which need accessed via the "#text" sub-property
$arrPropertiesWithTextSubProperty = @(
"VirtualMachineID",
"Expires",
"VMCreationDate",
"VMDeleteDate",
"LastLoggedDate",
"LastLoggedUser",
"LastPowerOffDate",
"LastPowerOnDate",
"OwnerExists",
"UsageIndex",
"UsageIndexIgnoreBy",
"IsDeleted",
"IsMissing",
"IsRogue",
"IsRunning",
"RecCreationTime",
"RecDeleteTime",
"RecUpdateTime",
"Flags",
"Text1",
"Text2",
"VMCPUs",
"VMTotalMemoryMB",
"VMTotalStorageGB",
"CurrentTask",
"IsTemplate",
"VMUsedStorageGB",
"FileLevelCloneImageName",
"VMInitialUsedSpace",
"VMEstimatedUsedSpace",
"ExpireDays",
"ConnectToVdi",
"virtualMachineType",
"MachineType",
"IsManaged",
"IsComponent",
"ParentMachineID",
"VirtualMachineTemplateID",
"HostReservationID",
"HostID"
)
# Uri for the ManagementModelEntities service. Used to build some other URLs later on
$baseUri = "https://$($Server)/Repository/data/ManagementModelEntities.svc/"
}
process {
# Generate the URL for getting VirtualMachines
$uri = $baseUri + "VirtualMachines"
if ($Name){
# If a name has been specified, then we'll filter for that machine name
$uri = $uri + '?$filter=VirtualMachineName eq ' + "'" + $Name + "'"
}
$webRequestParameters = @{
Uri = $uri
UseBasicParsing = $true # Means we don't need IE installed
}
if ($Credential){
# If credentials have been specified, add them to the array of parameters
Write-Verbose -Message "Using the explicitly defined credentials"
$webRequestParameters.add("Credential", $Credential)
}
else {
# If no credentials have been specified, set -UseDefaultCredentials
Write-Verbose -Message "Using the current user's credentials"
$webRequestParameters.add("UseDefaultCredential", $true)
}
# Run InvokeWebrequest, with the splatted parameters
$virtualMachineRequest = try {
Invoke-WebRequest @webRequestParameters
}
catch {
Write-Error -Message $_
return
}
if (([xml]$virtualMachineRequest.content).feed.entry){
$virtualMachines = @(([xml]$virtualMachineRequest.content).feed.entry)
Write-Verbose -Message "VirtualMachines found: $($virtualMachines.count)"
forEach ($virtualMachine in $virtualMachines){
$objvirtualMachine = New-Object PSObject
forEach ($strProperty in $arrProperties){
$objvirtualMachine | Add-Member -Name $strProperty -MemberType "NoteProperty" -Value $virtualMachine.content.properties.$strProperty
}
forEach ($strProperty in $arrPropertiesWithTextSubProperty){
$objvirtualMachine | Add-Member -Name $strProperty -MemberType "NoteProperty" -Value $virtualMachine.content.properties.$strProperty."#text"
}
if ($IncludeCustomProperties){
Write-Verbose -Message "Requesting the custom properties for $($virtualMachine.virtualMachineName)"
$uri = $baseUri + ($virtualMachine.link | Where-Object {$_.title -eq "VirtualMachineProperties"}).href
$webRequestParameters = @{
Uri = $uri
UseBasicParsing = $true
}
if ($Credential){
Write-Verbose -Message "Using the explicitly defined credentials"
$webRequestParameters.add("Credential", $Credential)
}
else {
Write-Verbose -Message "Using the current user's credentials"
$webRequestParameters.add("UseDefaultCredential", $true)
}
$customPropertiesRequest = try {
Invoke-WebRequest @webRequestParameters
}
catch {
Write-Error $_
return
}
# Parse the properties into a PoSHer format
$properties = ([xml]$customPropertiesRequest.content).feed.entry
$arrCustomProperties = @()
$properties.content.properties | ForEach-Object {
$objNew = New-Object PSObject
$objNew | Add-Member -Name "PropertyName" -MemberType "NoteProperty" -Value $_.PropertyName
$objNew | Add-Member -Name "PropertyValue" -MemberType "NoteProperty" -Value $_.PropertyValue
$objNew | Add-Member -Name "IsHidden" -MemberType "NoteProperty" -Value $_.IsHidden.'#text'
$objNew | Add-Member -Name "IsRuntime" -MemberType "NoteProperty" -Value $_.IsRuntime.'#text'
$objNew | Add-Member -Name "IsEncrypted" -MemberType "NoteProperty" -Value $_.IsEncrypted.'#text'
$arrCustomProperties += $objNew
}
# Append the new properties array
$objvirtualMachine | Add-Member -Name "Properties" -MemberType "NoteProperty" -Value $arrCustomProperties
}
# Add the machine to the results array
$arrvirtualMachines += $objvirtualMachine
}
}
else {
Write-Verbose -Message "No virtual machines found matching the filter"
}
}
end {
return $arrvirtualMachines
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment