12 December 2013

Use PowerShell to conditionally add VM's to a VMM cloud

I've had a few instanced where I needed to add a whole bunch of existing VM's to an existing cloud in SCVMM.  I wanted to only add the machines where the VM name matched a certain naming convention.

The easiest way to do this was to pipe the details together PowerShell

Set the Variable to the Cloud's Name - in this case "Service Management"

$Cloud = Get-SCCloud -VMMServer scvmmc01.fixmyitsystem.com | where {$_.Name -eq "Service Management"}

I wanted to add all the machine that contained "CAA" in the machine name

Check that the correct list of VM's will be added

Get-SCVirtualMachine -VMMServer scvmmc01.fixmyitsystem.com | where {$_.Name -Match "CAA"} | select name

Once I was happy, the command pipe below added the machines to the cloud

Get-SCVirtualMachine -VMMServer scvmmc01.fixmyitsystem.com | where {$_.Name -Match "CAA"} | ForEach-Object {Set-SCVirtualMachine -VM $_.Name  -Cloud $Cloud }



You can use this method to use any of the virtual machine properties and you can use any of the conditional operators. As an example you could add all machines where the owner is someone, or even move all machines from one cloud to another.

Some of the VM Properties:

Accessibility
AddedTime
BackupEnabled
CapabilityProfile
CapabilityProfileCompatibilityState
Cloud
CloudId
CloudVmRoleId
CloudVmRoleName
ClusterNonPossibleOwner
ClusterPreferredOwner
ComputerName
ComputerNameString
ComputerTier
ConnectToAddresses
CostCenter
CPUCount
CreationSource
CustomProperties
CustomProperty
Description
Generation
HasSavedState
HasSharedStorage
HasVirtualFibreChannelAdapters
HasVMAdditions
HAVMPriority
HostId
HostName
HostType
ID

To get a full list of the available properties you can use

Get-SCVirtualMachine |gm

The compare or evaluate the property to your query you can use the Conditional Operators

 -eq             Equal
 -ne             Not equal
 -ge             Greater than or equal
 -gt             Greater than
 -lt             Less than
 -le             Less than or equal
 -like           Wildcard comparison
 -notlike        Wildcard comparison
 -match          Regular expression comparison
 -notmatch       Regular expression comparison
 -replace        Replace operator
 -contains       Containment operator
 -notcontains    Containment operator


No comments:

Post a Comment