Automation

 View Only
Expand all | Collapse all

Svmotion Script with conditions

  • 1.  Svmotion Script with conditions

    Posted Jul 03, 2019 02:47 AM

    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


  • 2.  RE: Svmotion Script with conditions

    Posted Jul 03, 2019 06:28 AM

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



  • 3.  RE: Svmotion Script with conditions

    Posted Jul 08, 2019 12:00 PM

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

    And where do you get stuck?



  • 4.  RE: Svmotion Script with conditions

    Posted Jul 08, 2019 12:47 PM

    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



  • 5.  RE: Svmotion Script with conditions

    Posted Jul 08, 2019 01:26 PM

    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

       }

    }



  • 6.  RE: Svmotion Script with conditions

    Posted Jul 08, 2019 01:45 PM

    Thank you for quick response.

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

    -Dev



  • 7.  RE: Svmotion Script with conditions

    Posted Jul 08, 2019 01:47 PM

    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.



  • 8.  RE: Svmotion Script with conditions

    Posted Jul 08, 2019 01:53 PM

    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.



  • 9.  RE: Svmotion Script with conditions

    Posted Jul 08, 2019 02:07 PM

    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.



  • 10.  RE: Svmotion Script with conditions

    Posted Jul 08, 2019 02:11 PM

    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


  • 11.  RE: Svmotion Script with conditions

    Posted Jul 08, 2019 02:18 PM

    That is not in the code you showed.

    Get-Datastore "CurrentDatastoreName" | Get-VM |

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



  • 12.  RE: Svmotion Script with conditions

    Posted Jul 08, 2019 02:29 PM

    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.



  • 13.  RE: Svmotion Script with conditions

    Posted Jul 08, 2019 02:45 PM

    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

       }

    }



  • 14.  RE: Svmotion Script with conditions

    Posted Jul 08, 2019 09:10 PM

    Thanks, i will test this and let you know.