2

In PowerShell I'm trying to get the drive letter that an ISCSI target is mapped to. I'm using the following to get the ISCSI initiator name.

Get-IscsiTarget | ? {$_.IsConnected -eq $True} | Select -ExpandProperty NodeAddress

I have tried using Get-Disk | Select * and Get-PSDrive | Select * but these cmdlets do not seem to have any fields that I can link a target to, to obtain its drive letter.

Richard
  • 6,812
  • 5
  • 45
  • 60
  • An iSCSI target may present more than one logical unit, such that you may have more than one drive. Check out this other question that takes a different approach: look at the list of drives both before and after bringing the iSCSI session up: http://stackoverflow.com/questions/36286366/get-iscsi-mapped-drive-letter-from-iscsi-initiator-name – Mike Andrews Mar 30 '16 at 14:31
  • @gubblebozer the link you have given me isn't a diffrent question, it links back to is this question? – Richard Mar 30 '16 at 14:37
  • Ugh... cut and paste fail. This is the question I had intended: http://stackoverflow.com/questions/30957901/find-the-new-drives-connected-through-iscsi – Mike Andrews Mar 30 '16 at 19:07

1 Answers1

2

As long as you have one active partition (not including reserved) per ISCSI target, you can use the following to match an ISCSI address to its corresponding drive letter.

foreach ($disk in (Get-Disk | ?{$_.BusType -Eq "iSCSI"})){

    $DriveLetter = ($disk | Get-Partition | ?{$_.Type -eq "Basic"}).DriveLetter
    $ISCSI = $disk | Get-IscsiSession

    [pscustomobject]@{
        DiskNumber=$disk.Number; 
        DriveLetter=$DriveLetter; 
        InitiatorNodeAddress=$ISCSI.InitiatorNodeAddress;
        InitiatorIP=$ISCSI.InitiatorPortalAddress;
        Size=$disk.Size;
    }  
}

This will check all connected ISCSI disks and get their corresponding drive letter, then it will put all the information into a customer PowerShell object and return it.

Richard
  • 6,812
  • 5
  • 45
  • 60