PowerCLI: Organizing datastores in folders

Share on:

I wrote a PowerCLI script for a customer to organize his datastores in a couple of folders. The script moves the datastore to a folder according to the vSphere hosts cluster that uses the datastore.

This is done based on the name of the datastore which contains an identifier for the cluster it’s connected to. The dastores which are already organised in folders aren’t moved nor are the datastores which are part of a datastorecluster.

 1$vCenter = "vcenter.yourdomain.com"  
 2$ApplicationClusterDatastoresFolder = "Application Cluster Datastores"  
 3$OracleClusterDatastoresFolder = "Oracle Cluster Datastores"  
 4$DatabaseClusterDatastoresFolder = "Database Cluster Datastores"  
 5$TestClusterDatastoresFolder = "Test Cluster Datastores"  
 6$AppDatastoreCluster = "App-DatastoreCluster01"  
 7$TestDatastoreCluster = "Tst-DatastoreCluster01"  
 8
 9
10Connect-VIServer -Server $vCenter
11
12$Datastores = Get-Datastore | where { ($_.Name -like "dc1-lun*") -and (($_.ParentFolder -eq $null) -or ($_.ParentFolder -like "datastore"))}
13
14$AppDatastoreClusterView = Get-DatastoreCluster -Name
15$ApplicationDatastoreCluster | Get-View  
16$AppDatastoreClusterChilds = Get-View
17$AppDatastoreClusterView.ChildEntity | ForEach-Object {$_.Name}
18
19$TstDatastoreClusterView = Get-DatastoreCluster -Name
20$TestDatastoreCluster | Get-View  
21$TstDatastoreClusterChilds = Get-View
22$TstDatastoreClusterView.ChildEntity | ForEach-Object {$_.Name}
23
24$Datastores | ForEach-Object -Process {
25
26    if ($_.Name -like "*-app-*") {  
27        if ($AppDatastoreClusterChilds -contains $_.Name) {  
28            Write-Host "Can't move datastore. Datastore" $_.Name "part of datastore cluster." -ForegroundColor Yellow  
29        }  
30        else {  
31            Write-Host "Moving datastore" $_.Name "to" $ApplicationClusterDatastoresFolder "folder." -ForegroundColor blue  
32            $_| Move-Datastore -Destination $ApplicationClusterDatastoresFolder  
33        }  
34    }  
35    if ($_.Name -like "*-ora-*") {  
36        Write-Host "Moving datastore" $_.Name "to" $OracleClusterDatastoresFolder "folder." -ForegroundColor blue  
37        $_| Move-Datastore -Destination $OracleClusterDatastoresFolder  
38    }  
39    if ($_.Name -like "*-db-*") {  
40        Write-Host "Moving datastore" $_.Name "to" $DatabaseClusterDatastoresFolder "folder." -ForegroundColor blue  
41        $_| Move-Datastore -Destination $DatabaseClusterDatastoresFolder  
42    }
43
44    if ($_.Name -like "*-tst-*") {  
45        if ($TstDatastoreClusterChilds -contains $_.Name) {  
46            Write-Host "Can't move datastore. Datastore" $_.Name "part of datastore cluster." -ForegroundColor Yellow  
47        }  
48        else {  
49            Write-Host "Moving datastore" $_.Name "to" $TestClusterDatastoresFolder "folder." -ForegroundColor blue  
50            $_| Move-Datastore -Destination $TestClusterDatastoresFolder  
51        }  
52    }
53
54}