Using Powershell to add iSCSI Targetname and IP.. RRS feed

  • Question

  • $svFile = Import-CSV -Path C:\Users\Administrator\Desktop\esxiHostsDeployment.csv
    foreach ($NFSRow in $csvFile)
          $virtualSwitch = $NFSRow.Switch
          $vmK = $NFSRow.VMK
          $IPArray = @()
          $ipa = $csvFile | Where {"iSCSI1","iSCSI2" -contains $_.VMK} | Select -Property @{N="Initiator";E={"IPAddress:"+ $_.IP}}
    New-IscsiServerTarget -TargetName "esxiHosts1" -InitiatorId $ipa

    I have the above script which retrieves IP addresses from a CSV file but does not add them to the -InitiatorId from the $ipa variable, however when I check the variable value as seen below, it has the IP addresses.


    Could someone advise what I'm doing wrong.

    Thank You

    Friday, October 11, 2019 5:58 AM

All replies

  • There is really no way to understand what you are trying to do. What is in the CSV file?


    Friday, October 11, 2019 6:09 AM
  • Is it possible that this is what you are trying to do?

    Import-CSV -Path C:\Users\Administrator\Desktop\esxiHostsDeployment.csv |
    		$virtualSwitch = $_.Switch
    		$ip = $csvFile | Where {$_.VMK -in ('iSCSI1','iSCSI2') }
    		$ipa = 'IPAddress:{0}' -f $ip.ip
    		New-IscsiServerTarget -TargetName esxiHosts1 -InitiatorId $ipa
    This assumes that the IP is always found and that there is only one entry that matches.


    • Edited by jrv Friday, October 11, 2019 6:28 AM
    Friday, October 11, 2019 6:26 AM
  • Thanks for the reply.

    Sorry about that.

    I'm trying to add IP addresses to to a Targetname from a CSV file.

    This is the CSV file.

    And thanks for the script, however, I ran it and got the following error.

    New-IscsiServerTarget : Cannot bind parameter 'InitiatorIds'. Cannot convert value "IPAddress:" to type
    "Microsoft.Iscsi.Target.Commands.InitiatorId". Error: "An invalid format was specified. The format must be of the form
    Parameter name: value"
    At line:6 char:61
    + New-IscsiServerTarget -TargetName "esxiHosts1" -InitiatorId $ipa
    +                                                             ~~~~
        + CategoryInfo          : InvalidArgument: (:) [New-IscsiServerTarget], ParameterBindingException
        + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.Iscsi.Target.Commands.NewIscsiServerTargetComma

    Thank You

    Friday, October 11, 2019 6:33 AM
  • There is no way to return only one IP. If you need all IPs that match then you have to convert them in a loop.

    A picture of the C SV is NOT helpful.  Post the CSV contents as text using the code posting tool provided.


    Friday, October 11, 2019 6:36 AM
  • Basically what I'm trying to do is to ONLY add IP addresses of iSCSI1 and ISCSI2 to each Targetname (as in 4 IP addresses to esxiHosts1 and 4 IP addresses to esxiHosts2.

    Now the format of the -InitiatorId is ("IPAddresses:", "IPAddresses:") and I'm unable to understand how to add it in this format.

    Friday, October 11, 2019 6:37 AM
  • You can use the current code like this:

    New-IscsiServerTarget -TargetName esxiHosts1 -InitiatorId$ipa.Initiator


    Friday, October 11, 2019 6:38 AM
  • Hosts	Cluster	Switch	NIC	VMK	IP
    esxi21.v.lab	vSAN_Cluster	vSwitch0	vmnic1	ManagementNetwork2
    esxi22.v.lab	vSAN_Cluster	vSwitch0	vmnic1	ManagementNetwork2
    esxi23.v.lab	vSAN_Cluster	vSwitch0	vmnic1	ManagementNetwork2
    esxi24.v.lab	iSCSI_Cluster	vSwitch0	vmnic1	ManagementNetwork2
    esxi25.v.lab	iSCSI_Cluster	vSwitch0	vmnic1	ManagementNetwork2
    esxi21.v.lab	vSAN_Cluster	NFSSwitch	vmnic2	NFS
    esxi22.v.lab	vSAN_Cluster	NFSSwitch	vmnic2	NFS
    esxi23.v.lab	vSAN_Cluster	NFSSwitch	vmnic2	NFS
    esxi24.v.lab	iSCSI_Cluster	NFSSwitch	vmnic2	NFS
    esxi25.v.lab	iSCSI_Cluster	NFSSwitch	vmnic2	NFS
    esxi21.v.lab	vSAN_Cluster	vSANSwitch	vmnic3	vSAN1
    esxi22.v.lab	vSAN_Cluster	vSANSwitch	vmnic3	vSAN1
    esxi23.v.lab	vSAN_Cluster	vSANSwitch	vmnic3	vSAN1
    esxi24.v.lab	iSCSI_Cluster	iSCSISwitch	vmnic3	iSCSI1
    esxi25.v.lab	iSCSI_Cluster	iSCSISwitch	vmnic3	iSCSI1
    esxi21.v.lab	vSAN_Cluster	vSANSwitch	vmnic4	vSAN2
    esxi22.v.lab	vSAN_Cluster	vSANSwitch	vmnic4	vSAN2
    esxi23.v.lab	vSAN_Cluster	vSANSwitch	vmnic4	vSAN2
    esxi24.v.lab	iSCSI_Cluster	iSCSISwitch	vmnic4	iSCSI2
    esxi25.v.lab	iSCSI_Cluster	iSCSISwitch	vmnic4	iSCSI2

    I have managed to retrieve the IP addresses I need in the variable $ipa as seen in the first post, just not able to understand how to add them to the -InitiatirId.

    Thank You

    • Edited by tryllzhuud Friday, October 11, 2019 6:41 AM
    Friday, October 11, 2019 6:39 AM
  • If I run my initial code I get no error but no result either..
    Friday, October 11, 2019 6:42 AM
  • You can also do it like this:

    $csvfile |
    		$virtualSwitch = $_.Switch
    		$ipa = $csvFile | 
    			Where {$_.VMK -in ('iSCSI1','iSCSI2') }  |
    			ForEach-Object{'IPAddress:' + $_.IP}
    		New-IscsiServerTarget -TargetName esxiHosts1 -InitiatorId $ipa


    • Edited by jrv Friday, October 11, 2019 6:44 AM
    Friday, October 11, 2019 6:42 AM
  • What you have posted is not a csv file. It is just text from Excel. A CSVfile is not Excel.


    Friday, October 11, 2019 6:45 AM
  • Thanks, but I encountered the following error.

    New-IscsiServerTarget : Cannot validate argument on parameter 'InitiatorIds'. The argument is null. Provide a valid
    value for the argument, and then try running the command again.
    At line:7 char:59
    + New-IscsiServerTarget -TargetName esxiHosts1 -InitiatorId $ipa
    +                                                           ~~~~
        + CategoryInfo          : InvalidData: (:) [New-IscsiServerTarget], ParameterBindingValidationException
        + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.Iscsi.Target.Commands.NewIscsiServerTargetCom

    • Edited by tryllzhuud Friday, October 11, 2019 6:48 AM
    Friday, October 11, 2019 6:47 AM
  • Your CSV wants you to do this:

    Import-CSV -Path C:\Users\Administrator\Desktop\esxiHostsDeployment.csv |
    	Where-Object{$_.VMK -in ('iSCSI1','iSCSI2') } |
     		New-IscsiServerTarget -TargetName $_.Host -InitiatorId @('IPAddress:' + $_.IP)


    Friday, October 11, 2019 6:57 AM
  • Thanks, that did not work either, I encountered the following error.

    New-IscsiServerTarget : Cannot bind argument to parameter 'TargetName' because it is null.
    At line:4 char:36
    +  New-IscsiServerTarget -TargetName $_.Host -InitiatorId @('IPAddress: ...
    +                                    ~~~~~~~
        + CategoryInfo          : InvalidData: (:) [New-IscsiServerTarget], ParameterBindingValidationException
        + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.Iscsi.Target.Commands.NewIscsiServerTargetComman

    And also I found from my initial script that if I get different results is I view the values of $ipa.

    If I use $ipa I get the following results


    If I use write-host $ipa I get

    @{IPAddress:=} @{IPAddress:=} @{IPAddress:=} @{IPAddress:=}

    I'm assuming at this point it is taking the values as above and passing it to the -InitiatorId and thus the invalid format error.

    Friday, October 11, 2019 7:07 PM
  • Sorry - "host" should be "hosts".

    I think that you need to learn the basics of PowerShell and iSCSI technology.  We cannot guess at what you are trying to do from the information you have provided.


    Friday, October 11, 2019 7:28 PM
  • Once you understand how this works you are going to have to use Group-Object to aggregate all hosts to obtain all of the required addresses at once - assuming that that may be what you are trying to do.


    Friday, October 11, 2019 7:29 PM
  • You still haven't posted a csv file. Open the file in notepad and copy and paste the contents here.


    Friday, October 11, 2019 7:37 PM
  • Here is how to test this to see what the CSV does. It will help you to clarify you issue.

    Import-CSV -Path C:\Users\Administrator\Desktop\esxiHostsDeployment.csv |
    		Write-Host 'Host-{0}  IPAddress:{1}' -f $_.Hosts, ($_.IP)
     		New-IscsiServerTarget -TargetName $_.Hosts -InitiatorId @('IPAddress:' + $_.IP) -WhatIf


    • Edited by jrv Friday, October 11, 2019 7:45 PM
    Friday, October 11, 2019 7:43 PM
  • Here

    Import-CSV -Path C:\Users\Administrator\Desktop\esxiHostsDeployment.csv |
    	Group-Object hosts |
    		$initiatorId = $_.Group | %{'IPAddress:{0}' -f $_.IP}
    		Write-Host ('Host-{0}  IPAddress:{1}' -f $_.Name, "$initiatorId")
    		New-IscsiServerTarget -TargetName $_.Hosts -InitiatorId $initiatorId -WhatIf

    is how to group the elements:


    Friday, October 11, 2019 7:57 PM
  • Hosts,Cluster,Switch,NIC,VMK,IP

    Find the CSV file content above.

    I thought the title is clear, I'm trying to add IP address obtained from the CSV file to the -InitiatorId, however, due to the format of the result it is failing.

    Friday, October 11, 2019 8:52 PM
  • Much different from anything you posted so far.

    Notice the commas and that it has well defined columns and headers,

    Here - read about what a CSV is and why,  Every new tech must know this,

    Comma-separated values


    Friday, October 11, 2019 8:57 PM