VMware Cloud Community
Cvolz
Contributor
Contributor

PowerCLI for datastore Identification

Environment is made up of A and B Server nodes, Also Each server should either be sitting on either an A or B datastore. Over the years some have seemed to be misplaced.

I'm having trouble writing a script that will display all Server A (or B) nodes and their respective datastores so I can identify them and manually migrate them to their appropriate Datastore, if there was a way to automate the migration that would also be a plus but would like to it to be separate or at least have the ability to self review or check for space before migrating

Tags (1)
0 Kudos
5 Replies
LucD
Leadership
Leadership

With Server nodes, do you mean VMs or ESXi nodes?

And who do you distinguish between the A and B servers?

Is there something in their name that allows to do this?


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

0 Kudos
Cvolz
Contributor
Contributor

I was able to get the information I need going about another way. All my VMs either end in "a" or "b" and datastores either contain "_a" or _b"

I wrote a script to return lets say all "_a" containing datastores and all the VMs hosted on them and export to excel, and then just filtered out all VMs ending in "a" leaving only "b" nodes which were living on "_a" datastores, and then swap a for b in the script to get the other half. Then just to go through and manually migrate each VM to the datastore it needs to be on. I could close this ticket but just want to see if anyone has any more efficient suggestions.Code printed below.

$report = @()

$VMs = Get-datastore | Where {$_.name -like '*_a*'} | Get-VM

Foreach ($VM in $VMs){

    $line = $VM | Select Name, @{N="Datastore";E={Get-Datastore -VM $_}}, @{N="Datastore Cluster";E={Get-DatastoreCluster -VM $_}}

    $report += $line

    }

$report | Export-csv C:\DatstoreA.csv -NoTypeInformation -UseCulture

0 Kudos
LucD
Leadership
Leadership

You could do something like this.
All the incorrectly placed VMs will be listed together with the datastore, i.e.

Datastore,Name

ds_a,vm_b

ds_b,vm_a

$extensions = '_a','_b'

$report = foreach($suffix in $extensions){

    foreach($ds in (Get-Datastore | where{$_.Name -match "$($suffix)$"})){

        Get-VM -Datastore $ds | where{$_.Name -notmatch "$($suffix)$" |

        select @{N='Datastore';E={$ds.Name}},Name

    }

}

$report | Export-csv C:\VMonIncorrectDatastore.csv -NoTypeInformation -UseCulture


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

0 Kudos
Cvolz
Contributor
Contributor

I could except theyre not suffixes...which my limited powercli knowledge I wouldnt know the appropriate term but most of my datastores are in the format Datastore_a_example123, the _a/_b is only a suffix of a couple datastore the rest its smack in the middle. This was my first goto and reason of the post I was needing a command more like Datastore where name is like "*_a*" or something that signified it just contains _a, my servers however do end in a or b

0 Kudos
LucD
Leadership
Leadership

You can still use the match operator, just drop the $ at the end.
That way the target string can sit anywhere in the name


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

0 Kudos