Post

Making an iSCSI target persistent with PowerShell

Making an iSCSI target persistent with PowerShell

One of the iSCSI targets in my homelab began to not automatically reconnect after reboots, for unknown reason(s). This quick-tip post shows how to remedy it.

Symptoms

After the latest Patch Tuesday my server didn’t reconnect a particular iSCSI target.

ISCSI iSCSI Initiator Properties - Targets

Being a designated favorite target it should have at least attempted a reconnect.

ISCSI iSCSI Initiator Properties - Favorite targets

Troubleshooting

There’s no “persistence” flag in the UI for iSCSI; in theory every favorited target should be persistent by default, and this has worked for a couple of years with the target in question. No matter why it fell out of “persistence grace”, I turned to PowerShell in my troubleshooting efforts.

The Get-IscsiSession cmdlet, a wrapper for the MSFT_iSCSISession class, shows the current iSCSI sessions - excluding my favorite that didn’t successfully connect.

1
Get-IscsiSession

I connected the session in iSCSI Initiator Properties manually to have a look at the properties.

ISCSI iSCSI Initiator Properties - Connect to Target

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Get-IscsiSession

AuthenticationType      : NONE
InitiatorInstanceName   : ROOT\ISCSIPRT\0000_0
InitiatorNodeAddress    : iqn.1991-05.com.microsoft:lab
InitiatorPortalAddress  : 0.0.0.0
InitiatorSideIdentifier : 400001370000
IsConnected             : True
IsDataDigest            : False
IsDiscovered            : False
IsHeaderDigest          : False
IsPersistent            : False
NumberOfConnections     : 1
SessionIdentifier       : ffffc8094f9d7010-40000213800000009
TargetNodeAddress       : iqn.2004-04.com.qnap:ts-664:iscsi.files.8c8fe8
TargetSideIdentifier    : 2400

Pressingly the session’s properties states that IsPersistent is false, meaning that it won’t re-establish commerece, as it were, on reboots.

There’s another cmdlet, Register-IscsiSession, a wrapper for the MSFT_iSCSISession class’ Register method, that makes sessions persistent.

Since I only have one session, I could do:

1
Get-IscsiSession | Register-IscsiSession

Otherwise filter by e.g. the TargetNodeAddress property which correlates with the target name in iSCSI Initiator Properties.

1
Get-IscsiSession | Where-Object { $_.TargetNodeAddress -eq 'iqn.2004-04.com.[..]' }

However, the IsPersistent flag was still showing non-persistence.

Resolution

What ultimately worked was running the Unregister-IscsiSession cmdlet first (another option might’ve been to remove the target and recreating it):

1
2
3
Get-IscsiSession | Unregister-IscsiSession

Get-IscsiSession | Register-IscsiSession

At which point the property gleamed True.

After a reboot, the iSCSI target now connects every time!

.NET class

To instead use the underlying .NET class directly, access the class with Get-CimInstance and the register- and unregister methods with Invoke-CimInstance:

1
2
3
4
5
6
7
8
9
10
11
12
13
$session = Get-CimInstance -ClassName 'MSFT_iSCSISession' -Name 'Root\Microsoft\Windows\Storage'

Invoke-CimMethod -InputObject $session -MethodName Unregister

ReturnValue PSComputerName
----------- --------------
          0

Invoke-CimMethod -InputObject $session -MethodName Register -Arguments @{ 'IsMultipathEnabled' = $false }

ReturnValue PSComputerName
----------- --------------
          0

Credits

Some photos showcasing Microsoft technology. Featured image from episode 3 of season 3 of TV series Silicon Valley.

This post is licensed under CC BY-NC-SA 4.0 by the author.
Written by human, not by AI