VMware Cloud Community
bernz
Enthusiast
Enthusiast

Renaming default ESXi datastores

By default, new ESXi creates a datastore on the local disk called "datastore1" on all new hosts.

When you add a batch of new hosts to vCenter, it will create duplicate names by appending a sequence number as below:

datastore1 datastore1 (1) datastore1 (2) datastore1 (3)

datastore2 datastore2 (1) datastore2 (2) datastore2 (3)

datastore3 datastore3 (1) datastore3 (2) datastore3 (3)

datastore4 datastore4 (1) datastore4 (2) datastore4 (3)

I would like to create powercli script that renames these datastores per new host to format ( short hostname + local + N )

example: esx-local1, esx-local2, esx-local3, esx-local4

Here's my initial function that i suppose to call within the script. There is a CSV file of hostlist that being called.

Function Rename-datastore {

Foreach ($Item in $CSV) {

$ESXHost=$Item.Hostname + ".domain.com"

$ds = get-vmhost $ESXHost | Get-Datastore -name datastore* | sort

Foreach ($datastore in $ds) {

$id = 0

        $id+=1

       

        Set-Datastore -Datastore $datastore -name (Get-VMHost $ESXHost -Id $_.ExtensionData.Host[0].Key[0]).Name.Split('.')[0] + '-local'+$id

        }

}

}

Thanks in advance. 😃

0 Kudos
4 Replies
LucD
Leadership
Leadership

Are all these datastores local datastore, or are there some that are shared?

Do you get errors when you run your code?


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

0 Kudos
bernz
Enthusiast
Enthusiast

These are all local datastore, running the script nothing changes to  it.

0 Kudos
bernz
Enthusiast
Enthusiast

Cannot index into a null array.

At line:13 char:9

+         Set-Datastore -Datastore $datastore -name (Get-VMHost $ESXHos ...

+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException

    + FullyQualifiedErrorId : NullArray

This was the error I encountered.

0 Kudos
LucD
Leadership
Leadership

Can you try like this.

As a safety measure I filter out any shared datastore that might exist.

Function Rename-datastore {

   Foreach ($Item in $CSV) {

   $esx = Get-VMHost -Name "$($Item.Hostname).domain.com"

   $id = 0

   Get-Datastore -Name datastore* -RelatedObject $esx |

   where {-not $_.ExtensionData.Summary.MultipleHostAccess} |

   ForEach-Object -Process {

   $id += 1

   Set-Datastore -Datastore $_ -Name "$($esx.Name.Split('.')[0])-local$($id)" -Confirm:$false

   }

   }

}


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

0 Kudos