How to Use PowerShell to Discover iSCSI Target Information
Here’s what you need to know to retrieve information about an iSCSI target attached to a Windows Server.
December 4, 2020
For more technical explainers on PowerShell, read our PowerShell 101: A Technical Explainer for IT Pros report.
Throughout most of its history, Windows made it relatively easy to create, modify or remove an iSCSI target. In recent years, however, the trend has been to deploy headless Windows Servers that must be managed either through PowerShell or with a third-party tool. Although this somewhat complicates the process of managing iSCSI connectivity, it is possible to use PowerShell to configure iSCSI. In this blog post, I will show you how it works.
PowerShell includes a built-in cmdlet called Get-IscsiTarget that you can use to retrieve information about an iSCSI target that is attached to a Windows Server. The cmdlet is used varies slightly depending on whether the server is clustered.
If you need to retrieve iSCSI target information for a stand-alone server, then you can simply enter the Get-IscsiTarget cmdlet without specifying any additional parameters. Of course, that assumes you are trying to find a target that is attached to the local server. If you are trying to get target information for a remote machine, you will need to append the -ComputerName parameter, followed by the remote computer’s name.
If, on the other hand, you are trying to find an iSCSI target that is being used by a cluster, then you will need to use the -ComputerName parameter, but specify the cluster’s fully qualified domain name rather than the name of an individual host within the cluster. You will also need to append the ClusterGroupName parameter, followed by the name of the resource group.
Figure 1 shows an example of the type of information that is returned by the Get-iSCSITarget cmdlet.
iSCSI PowerShell 1
Figure 1
This is what happens when you use the Get-IscsiTarget cmdlet.
As you can see in the figure, this command displays a list of the targets that the Windows Server is aware of. It also tells you whether the Windows Server is connected to the various targets. In the case of what’s shown in the screen capture, for instance, PowerShell has identified two iSCSI targets. The IsConnected column indicates that the server is connected to one target, but not to the other.
So, with that in mind, pretend that you had a server like mine that is aware of multiple iSCSI targets, but not connected to all of them. Let’s also pretend that you want to attach the server to all available targets. One of the easiest ways to accomplish this is to filter the list of targets by connection state and then pipe the list of targets into the Connect-iSCSITarget cmdlet.
PowerShell gives you a few different filtering options, but the easiest option may be to use the Where-Object cmdlet. Here is an example of a command that you could use to filter targets by their connection state:
Get-iSCSITarget | Where-Object {$_.IsConnected -eq $False}
This command retrieves the list of known targets, and then filters the list to display only the disconnected targets by looking for targets where the IsConnected attribute is set to False. You can see what this looks like in Figure 2.
iSCSI PowerShell 2
Figure 2
This command causes PowerShell to display only the targets that are not connected.
If we then wanted to connect the disconnected targets, we could simply append the Connect-iSCSITarget cmdlet. Here is what the command would look like:
Get-iSCSITarget | Where-Object {$_.IsConnected -eq $False} | Connect-iSCSITarget
Keep in mind that the Connect-iSCSITarget cmdlet supports the use of additional parameters that you can use to customize the connection by enabling multipath, specifying an authentication type, etc. You can find more information about this command here.
If your goal is to get information about a server’s iSCSI connectivity, there are two more cmdlets you will need to be familiar with. The first of these cmdlets is Get-iSCSIConnection. This cmdlet returns basic information such as the IP addresses used by both the initiator and the target, as well as the port numbers that are being used. You can see this command’s output in Figure 3.
iSCSI PowerShell 3
Figure 3
The Get-iSCSIConnection cmdlet displays the initiator and target’s IP addresses.
The other command that you should be familiar with is Get-iSCSISession. This cmdlet gives you all of the vital information that you need about the server’s iSCSI sessions. You can use this cmdlet to determine the authentication type being used, whether the session is persistent and much more.
About the Author
You May Also Like