Automation

 View Only
  • 1.  Unmount datastore help, modifying existing function

    Posted Oct 25, 2012 07:13 PM

    I would like to modify the below function to be able to pass a single host parameter and a single datastore parameter instead of doing ALL hosts attached to lun.  I'd liek to be able to specify the info.

    Function Unmount-Datastore {

    [CmdletBinding()]

    Param

    (

    [Parameter(ValueFromPipeline

    =$true)]

    $Datastore

    )

    Process

    {

    if (-not $Datastore) {

    Write-Host "No Datastore defined as input"

    Exit

    }

    Foreach ($ds in $Datastore) {

    $hostviewDSDiskName = $ds.ExtensionData.Info.vmfs.extent[0].Diskname

    if ($ds.ExtensionData.Host) {

    $attachedHosts = $ds.ExtensionData.Host

    Foreach ($VMHost in $attachedHosts) {

    $hostview = Get-View $VMHost.Key

    $StorageSys = Get-View $HostView.ConfigManager.StorageSystem

    Write-Host "Unmounting VMFS Datastore $($DS.Name) from host $($hostview.Name)..."

    $StorageSys.UnmountVmfsVolume($DS.ExtensionData.Info.vmfs.uuid);

    }

    }

    }

    }

    I attempted to modify it to this, but don't think I have it correct.  I would run like this preferrably:

    $esx = get-vmhost hostname

    Unmount-Datastore -VMHost $esx -Datastore datastorename

    Function

    Unmount-Datastore {

    [CmdletBinding()]

    param(

    [

    VMware.VimAutomation.ViCore.Impl.V1.Inventory.VMHostImpl]$VMHost,

    [

    string]$Datastore

    )

    Process {

    if (-not $Datastore) {

    Write-Host "No Datastore defined as input"

    Exit

    }

    $hostview = Get-View $VMHost.Key

    $StorageSys = Get-View $HostView.ConfigManager.StorageSystem

    #Write-Host "Unmounting VMFS Datastore $($DS.Name) from host $($hostview.Name)..."

    $StorageSys.UnmountVmfsVolume($Datastore.ExtensionData.Info.vmfs.uuid);

    }

    }



  • 2.  RE: Unmount datastore help, modifying existing function
    Best Answer

    Posted Oct 25, 2012 07:57 PM

    Try it like this

    function Unmount-Datastore {
      [CmdletBinding()]
      param(
        [VMware.VimAutomation.ViCore.Impl.V1.Inventory.VMHostImpl]$VMHost,
        [
    string]$Datastore
      )  
    process {     if (-not $Datastore) {       Write-Host "No Datastore defined as input"
         
    exit
        }

       
    $ds = Get-Datastore -Name $Datastore
        $hostview = Get-View $VMHost
        $StorageSys = Get-View $HostView.ConfigManager.StorageSystem
       
    #Write-Host "Unmounting VMFS Datastore $($DS.Name) from host $($hostview.Name)..."     $StorageSys.UnmountVmfsVolume($ds.ExtensionData.Info.vmfs.uuid)
      } }

    Since Datastore is defined as a [string] I'm assuming you pass the datastorename.

    So you will need to get the datastore object in $ds



  • 3.  RE: Unmount datastore help, modifying existing function

    Posted Oct 25, 2012 08:28 PM

    Cool, that worked

    here is the final

    Just run

    $esx = get-vmhost hostname

    Unmount-Datastore -Vmhost $esx -Datastore datastore_name

    Function

    Unmount-Datastore {

    [CmdletBinding()]

    param(

    [

    VMware.VimAutomation.ViCore.Impl.V1.Inventory.VMHostImpl]$VMHost,

    [

    string]$Datastore

    )

    Process {

    if (-not $Datastore) {

    Write-Host "No Datastore defined as input"

    Exit

    }

    $ds = Get-Datastore -Name $Datastore

    $StorageSys = Get-View $esx.Extensiondata.ConfigManager.StorageSystem

    #Write-Host "Unmounting VMFS Datastore $($DS.Name) from host $($hostview.Name)..."

    $StorageSys.UnmountVmfsVolume($ds.ExtensionData.Info.vmfs.uuid);

    }

    }



  • 4.  RE: Unmount datastore help, modifying existing function

    Posted Oct 25, 2012 09:09 PM

    Here is a script to use the function to unmount a list of datastores against a list of hosts if anyone cares to use.

    files:

    unmount-function.ps1 with function above in the same directory

    datastores.csv

    contains one column with header datastore and datastores below

    hostlist (specify any name you want it will prompt)  header is host and then list of hostnames below

    #unmount-datastore.ps1

    . .\unmount-function.ps1

    $vcenter = Read-Host "Enter Vcenter Name"

    $list = read-host "Enter host list filename"

    Connect-VIServer $vcenter

    $hostslist = import-csv $list

    $datastorelist = import-csv datastores.csv

     

    foreach

    ($vmhost in $hostslist){

       $hostname=$vmhost.host

       $esx = get-vmhost $hostname

         foreach ($ds in $datastorelist){

         $datastore = $ds.datastore

         Write-Host "Unmounting $datastore from $hostname"

         Unmount-Datastore -VMhost $esx -Datastore $datastore

         }

    }

    Disconnect-VIServer $vcenter -Confirm:$false



  • 5.  RE: Unmount datastore help, modifying existing function

    Posted Aug 10, 2015 03:56 PM

    Thanks to both of you guys.  I have used both versions of these modified scripts for a large storage deprovisioning project and it has saved me and my team tons of time and effort.  Thank you!



  • 6.  RE: Unmount datastore help, modifying existing function

    Posted Aug 31, 2015 06:00 PM

    Hello Guys,

    It would be great if you would help me on this as i need detailed steps to unmount the list of Datastores from selected VM-Host. in ESXi5.5 

     

    Best Regards,

    Swamy Naveen

    naveens9038@gmail.com