none
Renaming VMWare Datastores w/ PS - Need Help w/ Appending a Numerical Suffix RRS feed

  • Question

  • I've been having some trouble getting this to work out the way I want. The gist: we have completely automated the builds for our hosts, vSphere, and venter respectively. In this automation, it creates the required number of datastores.  VMware creates these datastores with a default name (vsanDatastore); because we do not want to send this out with this generic name, I've been tasked to get the datastores renamed during the automated build process.  All I am attempting to do is rename the datastore(s) from "vsanDatastore" to something like "RenameThisDatastore##"; however, if there is more than 1 datastore, I want the script to append a number to the end.  For example, if I have to rename 3 datastores, I want them named "RenameThisDatastore01", "RenameThisDatastore02, "RenameThisDatastore03, and so on. 

    Everything seems to work fine if there is only 1 datastore, but the moment it sees the second one, it throws out an error.  The error basically states "RenameThisDatastore01 already exists" instead of creating "RenameThisDatastore02".  Can someone help me get this right?  I know it's probably something so painfully obvious to the seasoned scripter.

    This is what I have:

    $currentDatastore = Get-Datacenter | Get-Datastore | Where {$_.Name -like '*vsan*'}
    $RenameDS = "RenameThisDatastore"
    $int = 1
    foreach ($vmstore in $currentDatastore)
    {
     Set-Datastore $vmstore -Name ($RenameDS + '0' + $int.ToString().Length)
    }


    • Edited by JargonFree Friday, June 23, 2017 11:17 AM
    • Moved by Bill_Stewart Wednesday, July 26, 2017 7:46 PM This is not "scripts on demand"
    Friday, June 23, 2017 11:16 AM

All replies

  • $newname ='RenameThisDatastore{0:D2}' -f $i++

    \_(ツ)_/

    Friday, June 23, 2017 11:30 AM
  • Thanks for the reply, JRV.  I actually tried something similar to that and it threw a few errors; I tried yours, but it's giving me the same results as described above.  Using your recommendation, I changed the script (directly below); can you take a look and let me know what I need to change.  I'm wondering if I'm not inserting it correctly into the script.

    $currentDatastore = Get-Datacenter | Get-Datastore | Where {$_.Name -like '*vsan*'}

    $int = 1

    $RenameDS = 'RenameThisDatastore{0:D2}' -f $i++

    foreach ($vmstore in $currentDatastore)
    {

    Set-Datastore $vmstore -Name $RenameDS

    }

    Friday, June 23, 2017 11:43 AM