-
-
Save pezhore/479cdcd4609b03b58c3fbfb2df3f30f5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.SYNOPSIS | |
Script that changes vRA managed machine reservations. | |
.DESCRIPTION | |
Leveraging an existing connection to vRA and a pre-configured cloudclient installation, | |
find all managed machines on a given OldReservation and attempt to change them to a | |
given NewReservation. | |
.EXAMPLE | |
PS C:\> Set-vRAMachineReservation.ps1 | |
Uses defaults to change all machines on the "Windows - 01" reservation | |
to the "Windows - 02" reservation | |
.EXAMPLE | |
PS C:\> Set-vRAMachineReservation.ps1 -OldReservation "Res - 01" -NewReservation "Res - 02" | |
Finds all vRA machines on the "Res - 01" reservation and changes them to the | |
"Res - 02" Reservation | |
.EXAMPLE | |
PS C:\> Set-vRAMachineReservation.ps1 -TargetVMs "Test01","Test02","Test03" | |
Attempts to find the three test VMs in vRA, and if they are a member of the default OldReservation, | |
"Windows - 01", will attempt to change their reservation to the default NewReservation, | |
"Windows - 02" | |
.EXAMPLE | |
PS C:\> Set-vRAMachineReservation.ps1 -TargetVMs "Test01" -OldReservation "Res - 01" | |
Attempts to find the Test01 VM in vRA, and if it is a member of the "Res - 01" reservation, will | |
attempt to change its reservation to the default NewReservation, "Windows - 02" | |
.PARAMETER OldReservation | |
The existing reservation that should be changed | |
.PARAMETER NewReservation | |
The new reservation for all machines found on existing | |
.PARAMETER TargetVMs | |
An array of VM names to target for reservation changes | |
#> | |
[CmdletBinding()] | |
param( | |
[System.String] $OldReservation = "Windows - 01", | |
[System.String] $NewReservation = "Windows - 02", | |
[ValidateScript( { | |
Test-Path -Path $_ -Type Container | |
})] | |
[System.String] $CloudClientPath = "C:\temp\cloudclient\4.4.0\VMware_vRealize_CloudClient-4.4.0-5511232", | |
[System.String[]] $TargetVMs | |
) | |
process { | |
# CloudClient doesn't like spaces. We need to escape them | |
$NewReservation = $NewReservation -replace " ", "\ " | |
# Initialize Values | |
$AllResources = @() | |
$Page = 1 | |
# If we're not asking for a specific set of VMs, grab everything. | |
if (! $TargetVMs) { | |
<# | |
vRA uses pagnation to restrict how many results are returned. It will return *at most* 100 | |
results for a given "page". To fully get all resources, we must leverage a $Count variable | |
to track how many results we have and increment the page for every time we get back 100 | |
results. | |
#> | |
do { | |
# Write out some progress | |
$ProgressSplat = @{ | |
Activity = "Analyzing Resources" | |
Status = "Current page $Page" | |
} | |
Write-Progress @ProgressSplat | |
# Get the vRA Resources (e.g. Machines) for this page. WithExtendedData includes the current | |
# reservation | |
$Group = Get-vRAResource -Page $Page -WithExtendedData | |
# Grab the count of how many objects were retrieved | |
$Count = $Group.count | |
# Increment our page counter | |
$Page ++ | |
# Add this group to the larger AllResources array | |
$AllResources += $Group | |
} while ($Count -ge 100) | |
} | |
else { | |
# Loop through every individual vm in the TargetVMs list | |
foreach ($TargetVM in $TargetVMs) { | |
$AllResources += Get-vRAResource -Name $TargetVM | |
} | |
} | |
# Initialize our list of machines | |
$List = @() | |
# Parse every resource we retrieved above | |
foreach ($Resource in $AllResources) { | |
# If this resource's reservation matches the old reservation, add it to the list | |
if ($Resource.data.machinereservationname -match $OldReservation) { | |
Write-Information -MessageData "$OldReservation participant: $($Resource.name)" -InformationAction Continue | |
$List += $Resource.name | |
} | |
} | |
# CloudClient needs the MachineIDs as a comma separated list | |
$MachineIDs = $List -join "," | |
# Call the cloudclient executable, passing in the MachineIDs and New Reservation | |
$CloudClientBat = Join-Path -Path $CloudClientPath -ChildPath "\bin\cloudclient.bat" | |
& $CloudClientBat vra machines change reservation --ids $MachineIDs --reservationName $NewReservation | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment