How to Associate a Cloud to Multiple Virtual Machines in VMM with PowerShell #SCVMM #PowerShell

2 Min. Read

Introduction

Hello folks,

As you know that in System Center Virtual Machine 2012 R2, we can create “Clouds” as pools of delegated resources that abstract the underlying physical fabric such as compute, network and storage. We can create multiple Clouds for varying business units needs or tenants, and then present each Cloud with a defined capacity that we can increase later, decrease or customize as needed. In this manner, Virtual Machine Manager provides us with a lot of flexibility for mapping our data center capacity to changing business requirements while not exposing the full fabric capacity details to customers.

SmallCloud

In SCVMM in order to deploy a service to a cloud, you must have the following requirements in place:

Service Templates that are configured through the use of many VMM resources such as virtual hard disks, VM Templates, Guest OS profiles, Hardware profiles, and other VMM library resources. The service template allows us to configure how many Virtual Machines are initially deployed as part of the service deployment, then configure the applications to be installed at deployment time.

For example, when you deploy a multi-tier application to a tenant or business unit, the virtual machines that belong to this service will be associated with a Cloud.

The following figure will illustrate the picture:

AssociateCloud_VMM07

The first question is what if you already have a virtual machine that was manually deployed without Service Template, would you be able to associate it to a Cloud?

The answer is YES! Smile

How to do it?

You can browse to the VM and follow the steps below:

VM not associated with a cloud

AssociateCloud_VMM02

VM associated with a cloud

AssociateCloud_VMM03

The second question is what if you have 100 virtual machines that were manually deployed without Service Template, would you still be able to associate them to a Cloud?

The answer is YES!

How to do it?

Well, same as we did above… very simple.

Really, NOOOO!

So I have created the following simple PowerShell tool that will save you 400 clicks on your mouse, 4 clicks per VM.

<#
.SYNOPSIS
Set the Associate Cloud to Multiple Virtual Machines.

.DESCRIPTION
This script will set or remove cloud association for all Virtual Machines to a specific Tenant and Host Group.

.NOTES
File Name : Set-AssociateCloud.ps1
Author    : Charbel Nemnom
Version   : 2.0
Requires  : PowerShell Version 3.0 or above
OS        : Windows Server 2016 or above with System Center Virtual Machine Manager 2016 and 2019

.LINK
To provide feedback or for further assistance please visit:
https://charbelnemnom.com


.EXAMPLE
./Set-AssociateCloud -TenantCloudName <CloudProvider> -HostGroupName <Hosts>
This example will set the cloud association to all virtual machines for a specific Tenant and Host Group.
.EXAMPLE
./Set-AssociateCloud -TenantCloudName <CloudProvider> -HostGroupName <Hosts> -AssociateCloud No
This example will remove the cloud association from all virtual machines for a specific Tenant and Host Group.
#>

[CmdletBinding()]
param(
     [Parameter(Mandatory=$true)]
     [Alias('CloudName')]
     [String]$TenantCloudName,

     [Parameter(Mandatory=$true)]
     [Alias('GroupName')]
     [String]$HostGroupName,

     [ValidateSet("Yes","No")]
     [String]$AssociateCloud="Yes"
)

$Cloud = Get-SCCloud -VMMServer localhost | where {$_.Name -eq $TenantCloudName}
$hostGroups = Get-SCVMHostGroup -Name $HostGroupName
Foreach ($hostGroup in $hostGroups.AllChildHosts) {
    $VMs = Get-SCVirtualMachine -VMMServer localhost -VMHost $HostGroup
    foreach ($VM in $VMs) {
        If($AssociateCloud -eq 'Yes') {
        Set-SCVirtualMachine -VM $VM.Name -Cloud $Cloud
        }
        Else {
        Set-SCVirtualMachine -VM $VM.Name -RemoveFromCloud
        }
    }
}

If you need to associate all virtual machines to a specific tenant and host group, you can run the following cmdlet:

AssociateCloud_VMM04

AssociateCloud_VMM05

The default parameter to Associate Cloud is Yes, if you want to dissociate a Cloud from all Virtual Machines, then you need to add the parameter –AssociateCloud No

AssociateCloud_VMM06

AssociateCloud_VMM01

Hope this helps!

Until then… see you in the Cloud Smile

Cheers,
/Charbel

Related Posts

Previous

StarWind Free Webinars, Protecting Virtual Machines, Building Hyper-V Cluster and Building Scale-Out File Server Without JBODs #HyperV #StarWind

Free New Webinar & eBook: Microsoft Licensing for Hyper-V #Altaro #HyperV

Next

Let me know what you think, or ask a question...

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Subscribe to Stay in Touch

Never miss out on your favorite posts and our latest announcements!

The content of this website is copyrighted from being plagiarized!

You can copy from the 'Code Blocks' in 'Black' by selecting the Code.

Please send your feedback to the author using this form for any 'Code' you like.

Thank you for visiting!