Quantcast
Channel: Stijn Vermoesen » vSphere
Viewing all articles
Browse latest Browse all 5

PowerCLI: Organizing datastores in folders

$
0
0

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 down 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.


$vCenter = "vcenter.yourdomain.com"
$ApplicationClusterDatastoresFolder = "Application Cluster Datastores"
$OracleClusterDatastoresFolder = "Oracle Cluster Datastores"
$DatabaseClusterDatastoresFolder = "Database Cluster Datastores"
$TestClusterDatastoresFolder = "Test Cluster Datastores"
$AppDatastoreCluster = "App-DatastoreCluster01"
$TestDatastoreCluster = "Tst-DatastoreCluster01"
#################################################################################################

Connect-VIServer -Server $vCenter

$Datastores = Get-Datastore | where { ($_.Name -like "dc1-lun*") -and (($_.ParentFolder -eq $null) -or ($_.ParentFolder -like "datastore"))}

$AppDatastoreClusterView = Get-DatastoreCluster -Name $ApplicationDatastoreCluster | Get-View
$AppDatastoreClusterChilds = Get-View $AppDatastoreClusterView.ChildEntity | ForEach-Object {$_.Name}

$TstDatastoreClusterView = Get-DatastoreCluster -Name $TestDatastoreCluster | Get-View
$TstDatastoreClusterChilds = Get-View $TstDatastoreClusterView.ChildEntity | ForEach-Object {$_.Name}

$Datastores | ForEach-Object -Process {

	if ($_.Name -like "*-app-*")
	{
		if ($AppDatastoreClusterChilds -contains $_.Name)
		{
			Write-Host "Can't move datastore. Datastore" $_.Name "part of datastore cluster." -ForegroundColor Yellow
		}
		else
		{
			Write-Host "Moving datastore" $_.Name "to" $ApplicationClusterDatastoresFolder "folder." -ForegroundColor blue
			$_ | Move-Datastore -Destination $ApplicationClusterDatastoresFolder
		}
	}
	if ($_.Name -like "*-ora-*")
	{
		Write-Host "Moving datastore" $_.Name "to" $OracleClusterDatastoresFolder "folder." -ForegroundColor blue
		$_ | Move-Datastore -Destination $OracleClusterDatastoresFolder
	}
	if ($_.Name -like "*-db-*")
	{
		Write-Host "Moving datastore" $_.Name "to" $DatabaseClusterDatastoresFolder "folder." -ForegroundColor blue
		$_ | Move-Datastore -Destination $DatabaseClusterDatastoresFolder
	}

	if ($_.Name -like "*-tst-*")
	{
		if ($TstDatastoreClusterChilds -contains $_.Name)
		{
			Write-Host "Can't move datastore. Datastore" $_.Name "part of datastore cluster." -ForegroundColor Yellow
		}
		else
		{
			Write-Host "Moving datastore" $_.Name "to" $TestClusterDatastoresFolder "folder." -ForegroundColor blue
			$_ | Move-Datastore -Destination $TestClusterDatastoresFolder
		}
	}

}

Filed under: Uncategorized Tagged: datastore, datastorecluster, PowerCLI, PowerShell, Scripting, VMware, vSphere

Viewing all articles
Browse latest Browse all 5

Latest Images

Trending Articles





Latest Images