VMware Cloud Community
mmi9567
Contributor
Contributor

Set-Datastore cmdlet works against vCenter but not an ESXi host

Hi all:

I have a script that is designed to grab local datastores and rename them in series (e.g. if you have one or more datastores named anything in general, it will rename them to hostname-local-#, where it starts at 1 and increments upward until you're out of local datastores). If I run it against a vCenter server (I tried 4.0U3 and 5.0U1B), it works like a charm.

If I run it against a host directly or even just issue the cmdlet "get-datastore datastorename | set-datastore -Name newdatastorename" against the host, the output gives no errors and acts like it was successful but when I issue a get-datastore cmdlet, the original name is still there.

Has anyone seen this and/or has a workaround?

$vc = Read-Host "Connect to"

Connect-VIServer $vc

Get-VMHost | Sort Name | foreach {
$vmhost = Get-VMHost $_
$LocalName = $vmhost.NetworkInfo.HostName
$incr = 1
$luns = $vmhost | Get-ScsiLun | where { $_.LunType -eq "disk" -and $_.IsLocal -eq "True" }
$luns | foreach {
  $id = $_.CanonicalName
  $dsv = Get-VMHost $vmhost | Get-Datastore | where {$_.Type -eq "vmfs"} | Get-View
  foreach ( $ds in $dsv ) {
   $ds.Info.Vmfs.Extent | %{
    if($_.DiskName -eq $id) {
     Write-Host "$($ds.Name) is a local datastore"
     $newdsname = $LocalName + "-local-" + $incr
     Write-Host "Rename $($ds.Name) to $($newdsname)"
     Get-datastore $ds.Name | Set-Datastore -Name $newdsname
     $incr = $incr + 1
    }
   }
  }
}
}

Disconnect-VIServer $vc -Confirm:$false

Sorry if my code looks ugly... Tried pasting from PowerGUI Script Editor but the formatting got trashed in the process.

0 Kudos
2 Replies
LucD
Leadership
Leadership

Did you try the local datastore rename while you are connected to the ESXi server instead of the vCenter ?


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

0 Kudos
CRad14
Hot Shot
Hot Shot

Yeah, I am slightly confused as well by what you mean when you say against a host as well.. if you could clarify that would be awesome.

On a slightly different topic, I think there are ways you could make your script more efficient.

It looks like you do a Get-VMHost cmdlet 3 times I am seeing. Each time you do a cmdlet the script has to take extra to actually talk to vcenter again to get the information.

After you initial Get-VMHost on what looks like here to be line 3, I don't think you need to do  do a get-vmhost any other times...

Line 4 you should just be able to do $vmhost=$_

and this line

$dsv = Get-VMHost $vmhost | Get-Datastore | where {$_.Type -eq "vmfs"} | Get-View

You can just take out the "Get-VMhost" and it should work just the same...

Conrad www.vnoob.com | @vNoob | If I or anyone else is helpful to you make sure you mark their posts as such! 🙂
0 Kudos