Automation

 View Only
  • 1.  Set-Datastore cmdlet works against vCenter but not an ESXi host

    Posted Sep 05, 2012 09:07 PM

    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.



  • 2.  RE: Set-Datastore cmdlet works against vCenter but not an ESXi host

    Posted Sep 06, 2012 04:35 PM

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



  • 3.  RE: Set-Datastore cmdlet works against vCenter but not an ESXi host

    Posted Sep 06, 2012 06:23 PM

    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...