PowerShell to Configure iSCSI and MPIO

At work we have started to make a push to get as many servers off of RDM (Raw Device Mapping) drives in the virtual environment as we can. We use the RDM drives with MSCS (Microsoft Server Cluster Service) with SQL, but we do have a few application failover cluster as well. With this change we have an influx of iSCSI (Internet Small Computer Systems Interface) drive configurations along with  MPIO (Multipath I/O) configured.  With most configurations of iSCSI it should be configured with multiple paths to help mitigate the effects of HBA (host bus adapter) failure. So to ensure that everybody on the team configures the iSCSI and MPIO the same, I have scripted the setup and configuration of it. This has reduced the configuration time from about 5-10 minutes per server to about 30 seconds.

Step 1. Configure iSCSI and MPIO

[PS]
get-service -name MSiSCSI | start-service Set-Service -Name msiscsi -StartupType Automatic
Install-WindowsFeature -name Multipath-IO
[/PS]

The first two lines of this snippet starts the iSCSI service, and changes the start up setting to automatic. The third line of this will install the windows feature for MPIO.

Step 2. Gather the Initiator ID

(get-initiatorport).nodeaddress

This command gets the iSCSI Initiator ID. This is the information that I pass to the storage team for them to do their work to provision the iSCSI LUN to this machine.

Step 3. Finish the iSCSI/MPIO configuration

This is where the magic happens. This part of the configuration goes very quickly with a this script.

$TargetPortalAddresses = @("192.168.10.6","192.168.10.7","192.168.10.8","192.168.10.9")
$LocaliSCSIAddress = "192.168.10.100"

Foreach ($TargetPortalAddress in $TargetPortalAddresses){
New-IscsiTargetPortal -TargetPortalAddress $TargetPortalAddress -TargetPortalPortNumber 3260 -InitiatorPortalAddress $LocaliSCSIAddress
}

New-MSDSMSupportedHW -VendorId MSFT2005 -ProductId iSCSIBusType_0x9

Foreach ($TargetPortalAddress in $TargetPortalAddresses){
Get-IscsiTarget | Connect-IscsiTarget -IsMultipathEnabled $true -TargetPortalAddress $TargetPortalAddress -InitiatorPortalAddress $LocaliSCSIAddress -IsPersistent $true
}

Set-MSDSMGlobalDefaultLoadBalancePolicy -Policy FOO

Breakdown of the script

The first line has the iSCSI initiators from the storage side

$TargetPortalAddresses = @("192.168.10.6","192.168.10.7","192.168.10.8","192.168.10.9")

This line is the local IP address that connects the server to the iSCSI initiators

$LocaliSCSIAddress = "192.168.10.100"

This generates the Portal Addresses based on the the number and IP addresses of $TargetPortalAddresses in the first line of the script.

Foreach ($TargetPortalAddress in $TargetPortalAddresses){
New-IscsiTargetPortal -TargetPortalAddress $TargetPortalAddress -TargetPortalPortNumber 3260 -InitiatorPortalAddress $LocaliSCSIAddress
}

This line enabled MPIO support for iSCSI

New-MSDSMSupportedHW -VendorId MSFT2005 -ProductId iSCSIBusType_0x9

This for-each loop makes the connections to the iSCSI drives and sets the multipath to be enabled.

Foreach ($TargetPortalAddress in $TargetPortalAddresses){
Get-IscsiTarget | Connect-IscsiTarget -IsMultipathEnabled $true -TargetPortalAddress $TargetPortalAddress -InitiatorPortalAddress $LocaliSCSIAddress -IsPersistent $true
}

The last line sets the failover policy to Failover only via MPIO

Set-MSDSMGlobalDefaultLoadBalancePolicy -Policy FOO

-Stuart

Find this script at GitHub.com

9 thoughts on “PowerShell to Configure iSCSI and MPIO”

  1. Hi Stuart,

    Would you be able to share the PowerCLI script which can show the MTU of the current vSwitch with iSCSI conenction?

    Thanks in advance.

    1. Albert,
      This one took me a bit of thought to get to the solution, but i hope this help to get you on the right path.

      Get-cluster ClusterName | Get-VMhost | Select -First 1 | Get-VirtualSwitch | Select -First 1 | Select Name, MTU

  2. Hi

    Thank you so much for script, it is great.

    But it give me one error. Maybe you will know why.

    enviroment: target server: 2x adapter, iniciator server: 2x adapter
    So I took that scropt, copied to 2x (each time for one iniciatior adapter), changed policy to RR.

    Result is 3 connection and one failure.

    Connection Failed.
    + CategoryInfo : NotSpecified: (MSFT_iSCSITarget:ROOT/Microsoft/…SFT_iSCSITarget) [Connect-IscsiTarget], CimException
    + FullyQualifiedErrorId : HRESULT 0xefff0003,Connect-IscsiTarget
    + PSComputerName : R710

    Any idea?

    Thank you!

    1. Ok, I figure out what is wrong.

      When you set
      PS New-MSDSMSupportedHW -VendorId MSFT2005 -ProductId iSCSIBusType_0x9
      then is required restart of machine (at last when you do that in GUI).

      So I did it this way:
      PS Set-Service -Name msiscsi -StartupType Automatic
      PS Start-Service msiscsi
      PS Install-WindowsFeature -name Multipath-IO
      -then reboot
      PS New-MSDSMSupportedHW -VendorId MSFT2005 -ProductId iSCSIBusType_0x
      – then reboot

      then your script:

      [script]
      #set of adapter no.1
      $TargetPortalAddresses = @(“192.168.1.1″,”192.168.1.2”)
      $LocaliSCSIAddress = “192.168.1.5”

      Foreach ($TargetPortalAddress in $TargetPortalAddresses){
      New-IscsiTargetPortal -TargetPortalAddress $TargetPortalAddress -TargetPortalPortNumber 3260 -InitiatorPortalAddress $LocaliSCSIAddress
      }

      Foreach ($TargetPortalAddress in $TargetPortalAddresses){
      Get-IscsiTarget | Connect-IscsiTarget -IsMultipathEnabled $true -TargetPortalAddress $TargetPortalAddress -InitiatorPortalAddress $LocaliSCSIAddress -IsPersistent $true
      }

      Set-MSDSMGlobalDefaultLoadBalancePolicy -Policy RR

      #set of adapter no.2
      $TargetPortalAddresses = @(“192.168.1.1″,”192.168.1.2”)
      $LocaliSCSIAddress = “192.168.1.6”

      Foreach ($TargetPortalAddress in $TargetPortalAddresses){
      New-IscsiTargetPortal -TargetPortalAddress $TargetPortalAddress -TargetPortalPortNumber 3260 -InitiatorPortalAddress $LocaliSCSIAddress
      }

      Foreach ($TargetPortalAddress in $TargetPortalAddresses){
      Get-IscsiTarget | Connect-IscsiTarget -IsMultipathEnabled $true -TargetPortalAddress $TargetPortalAddress -InitiatorPortalAddress $LocaliSCSIAddress -IsPersistent $true
      }

      Set-MSDSMGlobalDefaultLoadBalancePolicy -Policy RR
      [/script]

      After this it is working correctly.
      Big Thank you for your script, it helps a lot.

      1. Jakub,
        Sorry that you had issues, but glad that you were able to find a solution. I’m happy that you found the script useful.

        – Stuart

  3. Wonderful script, thanks much. Would you be kind to guide me towards the PowerShell equivalent of the button “Auto Configure” under the volumes and devices tab of the iSCSI initiator window? Thanks in advance!

    1. Deepak,
      I have looked at some of the commands used for configuring iSCSI, but was unable to determine a way to perform the auto config for the volumes. As I mentioned in another thread, I no longer make use of iSCSI and don’t have a way to test this.

      -Stuart

    1. Brian,
      Sorry for the delay in my response. It looks like you can pass the Chap Username and Chap password with the either the new-iscsitargetportal or connect-iscsitarget command. I no longer have iSCSI devices in my environment so I’m unable to test this.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.