How To Rename Multiple ESXi Local Datastore With PowerCLI – VirtuallyThatGuy

This is a quick blog post to rename multiple datastore to Hostname-local using the residing cluster . Use case could be after auto deploy from stateless to stateful.

$cred = Get-Credential
$vCenters = "lab-vcenter01.lab.local", "lab-vcenter02.lab.local"
Connect-VIServer $vCenters -Credential $cred

### Script 1
[cmdletbinding(SupportsShouldProcess=$True)]
param(
  $cluster = "PRE_PROD-Cluster","PROD-Cluster",
  $suffix = "-local"
)
get-cluster $cluster | get-vmhost | % {
  $_ | get-datastore | ? {$_.name -match "^datastore1( \(\d+\))?$"} | set-datastore -name "$($_.name.split(".")[0])$suffix"
}

### One liner lovers like myself

Get-Datastore -Name datastore1* | %{ $n = '' + (Get-VMHost -Id $_.ExtensionData.Host[0].Key[0]).Name.Split('.')[0] + '-local';Set-Datastore -Datastore $_ -Name $n }


get-datastore | ?{$_.Name -like "*local*"}  | ft -a 
$cred = Get-Credential
$vCenters = "lab-vcenter01.lab.local", "lab-vcenter02.lab.local"
Connect-VIServer $vCenters -Credential $cred

### Script 1
[cmdletbinding(SupportsShouldProcess=$True)]
param(
  $cluster = "PRE_PROD-Cluster","PROD-Cluster",
  $suffix = "-local"
)
get-cluster $cluster | get-vmhost | % {
  $_ | get-datastore | ? {$_.name -match "^datastore1( \(\d+\))?$"} | set-datastore -name "$($_.name.split(".")[0])$suffix"
}

### One liner lovers like myself

Get-Datastore -Name datastore1* | %{ $n = '' + (Get-VMHost -Id $_.ExtensionData.Host[0].Key[0]).Name.Split('.')[0] + '-local';Set-Datastore -Datastore $_ -Name $n }


get-datastore | ?{$_.Name -like "*local*"}  | ft -a 

You May Also Like

About the Author: VirtuallyThatGuy

2 Comments

  1. Can you add this to auto deploy as part of the initial host profile attachment? If so would you be kind enough to cover this in a blog? Thanks for sharing these useful tips.

Leave a Reply

Your email address will not be published. Required fields are marked *