VMware Cloud Community
okalb
Contributor
Contributor

Help trying to rename a Datastore using the PowerCLI

Hi All,

Let me start by saying that scripting is not my strength and what little I do is NOT usually in PowerShell.  Here is my situation.  I need to automate the process of renaming the local datastore on a host.  The problem is that I will never know what the current name is.  There will only be the one local datastore on the host, so it is not like I have to tell it which one to rename.  Is there an easy way to script it to query for the current name of the datastore then change that to a name that I specify? Can anyone give me a rough idea of what the syntax should look like? I need to do this weekly for around 50 lab servers, so I would really like to be able to automate it if possible.

Any help would be appreciated

Thanks

-OK

0 Kudos
3 Replies
ssbkang
Enthusiast
Enthusiast

Not quite sure this script will fit your situation but try!

foreach ($datastore in (Get-Cluster -Name "Name of the cluster"| Get-VMHost | Get-Datastore | Sort Name)) {

  $continue = $true

  $answer = Read-Host "Current datastore is $datastore would you like to rename it? Y/N"

  while ($continue) {

    if ($answer -match "^Y$|^y$") {

      $new_datastore_name = Read-Host "Please specify the new datastore name"

      Get-Datastore -Name $datastore | Set-Datastore -Name $new_datastore_name

      $continue = $false

    } elseif ($answer -match "^N$|^n$") {

      $continue = $false

    } else {

      $answer = Read-Host "Please type Y or N"

    }

  }

}


Here is a sample output:


Current datastore is TMP99 would you like to rename it? Y/N:

Current datastore is datastore1 would you like to rename it? Y/N: n

Current datastore is datastore1 (1) would you like to rename it? Y/N: n

Current datastore is TMP8 would you like to rename it? Y/N: y

Please specify the new datastore name: TMP1                                                                                                                                                                                                                                                                        

                                                                                                                                                                                                                                                                                                                   

Name                               FreeSpaceGB      CapacityGB

----                               -----------      ----------

TMP1                                     0.731           0.750

Current datastore is TMP9 would you like to rename it? Y/N: n


If you don't have to specify which one to rename, the following should be better:


foreach ($datastore in (Get-Cluster -Name "Name of the cluster"| Get-VMHost | Get-Datastore | Sort Name)) {

   Write-Host "Current datastore is $datastore"

  $new_datastore_name = Read-Host "Please specify the new datastore name"

  Get-Datastore -Name $datastore | Set-Datastore -Name $new_datastore_name

}


Hope this helps,

Steven.

mattboren
Expert
Expert

Hello, okalb-

The ways that ssbkang presents should be helpful if you want to specify a new name for each datastore at script run time.  If you prefer it to be a bit less interactive, you could do something like the following:

Get-VMHost | %{
   
$oThisVMHost = $_
   
## make a new base name for the local datastore
    $strNewDStoreName = "localDstore_$($oThisVMHost.Name)"
   
## get the non-shared datastores on this VMHost, rename each to something like "localDstore_vmhostName_0"
    Get-Datastore -VMHost $oThisVMHost | ?{$false -eq $_.ExtensionData.Summary.MultipleHostAccess} | % -begin {$intI = 0} -process {Set-Datastore -Datastore $_ -Name "${strNewDStoreName}_$intI" -WhatIf; $intI++}
}
## end foreach-object

This gets, for each VMHost, the datastores that are not shared by other hosts (generally, the local datastores), and renames them to the given name pattern ("localDstore_<hostname>_0", for instance).  In case there is more than one such local datastore on a host, this will increment the number at the end of the string used for each local datastore on that host.

And, I left the -WhatIf parameter in there for the actual Set-Datastore portion, so you can run as-is to see what it would do.  Then, if it seems like a winner, remove that -WhatIf and enjoy.  How does that do?

okalb
Contributor
Contributor

Thanks guys, these are both helpful as a starting point.  Let me give you a bit more detail.  So I have about 50 hosts (named esxi01-esxi50).  They are in a folder in vcenter but not a cluster.  They each have a local datastore on them.  The local datastore will be renamed during the week by the students using the lab.  It will be renamed using their first name, so I will have no way to know the current name.  In the end what I want is esxi01 to have a local datastore named local01 and esxi02 to have a local datastore named Local02 and so on. This is part of resetting the lab environment back for the new group the following week.

0 Kudos