Automation

 View Only
  • 1.  Get LUN ID of the device

    Posted Apr 29, 2021 11:37 AM

    Hi,

    I am unable to get the LUN ID of the device in the report as it is showing blank.

    Please help!!

    $report = @()
    foreach ($vmhost in $hostslist){
    $hostname=$vmhost.host
    write-host "Starting $hostname"
    $esx = get-vmhost $hostname
    foreach ($lun in $luns){
    $naa=$lun.naa
    $lunState = Get-DiskState -VMHost $esx -CanonicalName $naa
    write-host "Detaching LUN $naa from $esx"
    if($lunState -eq 'attached'){
    Detach-Disk -vmhost $esx -CanonicalName $naa
    }
    $report += New-Object PSObject -Property @{
    VMHost = $esx.Name
    NAA= $naa
    PreState = $lunState

    LUNID ={
    $esx = Get-View -Id $_.ExtensionData.Host[0].Key -Property Name
    $dev = $_.ExtensionData.Info.Vmfs.Extent[0].DiskName
    $esxcli = Get-EsxCli -VMHost $esx.Name -V2
    $esxcli.storage.nmp.path.list.Invoke(@{'device'=$dev}).RuntimeName.Split(':')[-1].TrimStart('L')}}
    }
    }
    }



  • 2.  RE: Get LUN ID of the device

    Posted Apr 29, 2021 11:50 AM

    There is quite a bit of info missing in that code.
    How are $hostlist and $luns populated?
    What is Get-DiskState and Detach-Disk doing?
    For LUNID you seem to be using the properties of a Datastore object, but $_ definitely does not contain a Datastore object.
    You assign a codeblock to LUNID, not a value



  • 3.  RE: Get LUN ID of the device

    Posted Apr 29, 2021 11:55 AM

    Hi LucD,

    I am unable to get the LUN ID as below in the output

    ganapa2000_0-1619697174548.png

    below is the complete script

    function Detach-Disk{
    param(
    [VMware.VimAutomation.ViCore.Types.V1.Inventory.VMHost]$VMHost,
    [string]$CanonicalName
    )

    $storSys = Get-View $VMHost.Extensiondata.ConfigManager.StorageSystem
    $lunUuid = (Get-ScsiLun -VmHost $VMHost | where {$_.CanonicalName -eq $CanonicalName}).ExtensionData.Uuid

    $storSys.DetachScsiLun($lunUuid)
    }

    function Get-DiskState
    {
    param(
    [VMware.VimAutomation.ViCore.Types.V1.Inventory.VMHost]$VMHost,
    [string]$CanonicalName
    )
    $storSys = Get-View $VMHost.Extensiondata.ConfigManager.StorageSystem
    $lun = Get-ScsiLun -CanonicalName $CanonicalName -VmHost $VMHost -ErrorAction SilentlyContinue
    if(!$lun){'detached'}
    else{'attached'}
    }


    $hostslist = import-csv HostList.csv
    $luns = import-csv LunList.csv
    $report = @()
    foreach ($vmhost in $hostslist){
    $hostname=$vmhost.host
    write-host "Starting $hostname"
    $esx = get-vmhost $hostname
    foreach ($lun in $luns){
    $naa=$lun.naa
    $lunState = Get-DiskState -VMHost $esx -CanonicalName $naa
    write-host "Detaching LUN $naa from $esx"
    if($lunState -eq 'attached'){
     Detach-Disk -vmhost $esx -CanonicalName $naa
    }
    $report += New-Object PSObject -Property @{
    VMHost = $esx.Name
    NAA = $naa
    PreState = $lunState
    }
    }
    }

    $report



  • 4.  RE: Get LUN ID of the device
    Best Answer

    Posted Apr 29, 2021 12:11 PM

    First you need to get the LUN object

    $sLun = Get-ScsiLun -CanonicalName $naa -VmHost $esx
    

    From there you can get the LUNid

    LUNID = $sLun.RuntimeName.split(':')[-1].TrimStart('L')
    


  • 5.  RE: Get LUN ID of the device

    Posted Apr 29, 2021 01:00 PM

    Got it. that worked

    Thank you very much.