Making iSCSI Targets via CMD

Adding iSCSI targets can be cumbersome when you have several volumes on the SAN and several network cards in several hosts.  At Orion there were 8 hosts with 4 NICs and 8 volumes to connect too.  That’s 256 iSCSI connections, and if you use the GUI to make these connections, then you’re talking about doing at least 15 clicks per connection (3840 total) and then try to keep everything straight.  Good luck!   

Rather than clicking 4,000 times (taking into account a few mistakes) I wrote a script that uses a command that Nate gave me to make a Multipath IO connection with iscsicli.exe.  It’s easy to use and should work on any flavor of Windows that uses Microsoft’s iSCSI Initiator (2008 Standard, Core, Hyper-V Server R2, Enterprise, Windows 7, etc).  You just need to discover what “position” your network iSCSI cards are in to prepare the script.   

First, log in to the server and from command line run “iscsicpl” to open the control panel.  Select any target, click Connect and then Advanced.  From here select the Microsoft iSCSI Initiator and then pull down the Initiator IP drop down list.  Use the screenshot below to determine what “Position ID” your iSCSI NICs use.  Then hit Cancel a couple times to back out without making changes.   


Now you can edit iSCSINICs.txt to list only the ID’s for each NIC that you want to connect.  Put one number per line for each NIC.  In this example, because the 172.16.0.* NIC’s are being used for iSCSI, you would use:  


I do not understand where the order for this list comes from, so I can’t explain why it is listed that way, but I can tell you that if you make changes to your network hardware (like a driver update or share the nic with a virtual network) the order can and likely will change and you’ll have to re-create the iSCSI connections.  So try to get that stuff settled before bothering with this too much.  

Next you can edit iSCSITargets.txt to list each of the iSCSI targets that you want to connect those NICs to.  For example:

You can pull the target names from the “Discovered Targets” list in iscsicpl by copying it from the “Connect to Target” window or copying it from the Properties page.  

Finally, save the txt files and run the makeTargets.cmd script.  It’ll loop through the list of targets and make a Multi-Path (MPIO) enabled connection for each NIC to it.  Repeat this process on each host.  Make sure to check the NIC ID’s, but you’ll likely only need to get the Targets once. 

@echo off
ECHO Create MPIO "Favorite Targets" for each Nic and Target in the txt files...
FOR /F %%T IN (iSCSITargets.txt) DO (
 echo %%T
 FOR /F %%N IN (iSCSINICs.txt) DO (
  echo Targeting Network Interface %%N
  iscsicli persistentlogintarget %%T T * * * %%N * 0x00000002 * * * * * * * * * 0
  REM Sleep for one second, just to let things sort of settle...
  PING -n 2>nul

start iscsicpl

Now that they are connected, your next task it to initialize the volume and create a partition…

Categories: Scripts Tags: ,