Search code examples
powershelliscsi

Find the New Drives connected through iSCSI


I am writing a powerscript that connects to a target through ISCSi. I need to find the new drive letters (F:, G:, …) that is created after connecting. Is there any direct way to find that? MY script would be

New-IscsiTargetPortal -TargetPortalAddress $VirtualDeviceIp
Connect-IscsiTarget -NodeAddress $VirtualDeviceIQN
#Get the drives newly attached 

Though not straight I tried another way of doing it.

$initial=Get-Volume
New-IscsiTargetPortal -TargetPortalAddress $VirtualDeviceIp
Connect-IscsiTarget -NodeAddress $VirtualDeviceIQN
$final=Get-Volume
#Now compare $initial and $final to find the newly attached disks

But I dont know implement the second idea too :(


Solution

  • Compare the DriveLetter property of the two sets:

    Compare-Object $initial $final -Property 'DriveLetter'
    

    Expanding the property will give you just the drive letter:

    $driveLetter = Compare-Object $initial $final -Property 'DriveLetter' |
                   select -Expand 'DriveLetter'
    

    To be on the safe side you could add a filter that restricts results to "right side" items (i.e. newly added drives), thus excluding "left side" items (i.e. removed drives):

    $driveLetter = Compare-Object $initial $final -Property 'DriveLetter' |
                   ? { $_.SideIndicator -eq '=>' } |
                   select -Expand 'DriveLetter'