VMware Cloud Community
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Get LUN ID of the device

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')}}
}
}
}

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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


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

View solution in original post

0 Kudos
4 Replies
LucD
Leadership
Leadership
Jump to solution

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


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

0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

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

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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


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

0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Got it. that worked 🙂

Thank you very much.

0 Kudos