VMware Cloud Community
DevD
Enthusiast
Enthusiast

Svmotion Script with conditions

Looking for svmotion script with following condition :-

      -On provided VM name need to check which datastore it is.

     - Check what is the DRTier vmtag value : T0, T1, T2, T3.

     - On the basis on Tag value if VM is not in right datastore cluster  svmotion  in to on respective datastore-cluster.

Datastore Name example

  • DSC4OD-SRM-ABC-T0
  • DSC4OD-SRM-ABC-T1
  • DSC4OD-SRM-ABC-T2
  • DSC4OD-SRM-ABC-T3
Reply
0 Kudos
13 Replies
depping
Leadership
Leadership

Have you looked at the PowerCLI forum on here? There are thousands of posts with scripts

Reply
0 Kudos
LucD
Leadership
Leadership

I repeat Duncan's question, have you searched this community?
What do you already have?

And where do you get stuck?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
DevD
Enthusiast
Enthusiast

I tried but didn't find any, i have this but trying to figure out how to use Tag as a condition for check and only move if its not in right datastorecluster.

Get-Datastore "CurrentDatastoreName" | Get-VM | Move-VM -DiskStorageFormat Thin -Datastore "NewDatastoreName" –RunAsync

Reply
0 Kudos
LucD
Leadership
Leadership

Try something like this.

Remove the WhatIf switch on the Move-VM cmdlet, when you are sure the svMotions are done correctly.

$dsNames = 'DSC4OD-SRM-ABC-T0', 'DSC4OD-SRM-ABC-T2', 'DSC4OD-SRM-ABC-T3', 'DSC4OD-SRM-ABC-T4'

foreach ($dsName in $dsNames)

{

   foreach ($tgtDS in $dsNames | where { $_ -ne $dsName })

   {

     Get-VM -Datastore $dsName -Tag $tgtDs.Split('-')[-1] -ErrorAction SilentlyContinue |

     Move-VM -Datastore $tgtDS -Confirm:$false -WhatIf

   }

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
DevD
Enthusiast
Enthusiast

Thank you for quick response.

QQ : What is $tgtDS? and how this code validating VMTags value before Move-VM.

-Dev

Reply
0 Kudos
LucD
Leadership
Leadership

The internal ForEach loop defines and uses the variable $tgtDS.
It holds the datastores, one by one, of where a VM should be located.

I use the Tag parameter on the Get-VM to find VMs on the datastore that should not be there.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
DevD
Enthusiast
Enthusiast

So if i understood correctly its running  for all VM's matching the condition, but i want to do for specific one so i can add this script while building the VM's.

Reply
0 Kudos
LucD
Leadership
Leadership

You can add a condition in the Where-clause and test for the name of the VM, that way only specific VMs will be actually moved.

It would be nice if you have all the requirements when asking a question, and not add requirements as we go along.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
DevD
Enthusiast
Enthusiast

Sorry for confusion but I think i did, here is what i initially requested

Looking for svmotion script with following condition :-

      -On provided VM name need to check which datastore it is.

     - Check what is the DRTier vmtag value : T0, T1, T2, T3.

     - On the basis on Tag value if VM is not in right datastore cluster  svmotion  in to on respective datastore-cluster.

Datastore Name example

  • DSC4OD-SRM-ABC-T0
  • DSC4OD-SRM-ABC-T1
  • DSC4OD-SRM-ABC-T2
  • DSC4OD-SRM-ABC-T3
Reply
0 Kudos
LucD
Leadership
Leadership

That is not in the code you showed.

Get-Datastore "CurrentDatastoreName" | Get-VM |

Move-VM -DiskStorageFormat Thin -Datastore "NewDatastoreName" –RunAsync


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
DevD
Enthusiast
Enthusiast

Yeah but that was not a final code, i shared what i have with my limited powercli knowledge.

Anyways thank you for all your help and support !! and sorry for confusion.

Reply
0 Kudos
LucD
Leadership
Leadership

If you only want to check and take action for a specific VM, you could do

Get-VM -Name TestVM |

ForEach-Object -Process {

   $tier = (Get-TagAssignment -Entity $_ -Category 'DRTier').Tag.Name

   $dsName = (Get-Datastore -RelatedObject $_).Name

   if ($dsName -notmatch $tier)

   {

   Move-VM -VM $_ -Datastore "DSC4OD-SRM-ABC-$($tier)" -Confirm:$false

   }

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
DevD
Enthusiast
Enthusiast

Thanks, i will test this and let you know.

Reply
0 Kudos