5 Replies Latest reply on Aug 31, 2015 11:00 AM by swamynaveen

    Unmount datastore help, modifying existing function

    markdjones82 Expert

      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);

      }

      }

        • 1. Re: Unmount datastore help, modifying existing function
          LucD Guru
          User ModeratorsCommunity WarriorsvExpert

          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

          • 2. Re: Unmount datastore help, modifying existing function
            markdjones82 Expert

            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);

            }

            }

            • 3. Re: Unmount datastore help, modifying existing function
              markdjones82 Expert

              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

              • 4. Re: Unmount datastore help, modifying existing function
                bsd1977jda Lurker

                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!

                • 5. Re: Unmount datastore help, modifying existing function
                  swamynaveen Novice

                  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