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
Have you looked at the PowerCLI forum on here? There are thousands of posts with scripts
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
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
Try something like this.
Remove the WhatIf switch on the Move-VM cmdlet, when you are sure the svMotions are done correctly.
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
Thank you for quick response.
QQ : What is $tgtDS? and how this code validating VMTags value before Move-VM.
-Dev
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
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.
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
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
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
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.
If you only want to check and take action for a specific VM, you could do
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
Thanks, i will test this and let you know.