none
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.

    $ipa
    
    Initiator
    ---------
    IPAddress:192.168.4.24
    IPAddress:192.168.4.25
    IPAddress:192.168.4.34
    IPAddress:192.168.4.35

    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 |
    	ForEach-Object{
    		$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
    "Type:Value".
    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
       nd

    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:192.168.2.1", "IPAddresses:192.168.2.1") 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	192.168.2.31
    esxi22.v.lab	vSAN_Cluster	vSwitch0	vmnic1	ManagementNetwork2	192.168.2.32
    esxi23.v.lab	vSAN_Cluster	vSwitch0	vmnic1	ManagementNetwork2	192.168.2.33
    esxi24.v.lab	iSCSI_Cluster	vSwitch0	vmnic1	ManagementNetwork2	192.168.2.34
    esxi25.v.lab	iSCSI_Cluster	vSwitch0	vmnic1	ManagementNetwork2	192.168.2.35
    esxi21.v.lab	vSAN_Cluster	NFSSwitch	vmnic2	NFS	192.168.3.21
    esxi22.v.lab	vSAN_Cluster	NFSSwitch	vmnic2	NFS	192.168.3.22
    esxi23.v.lab	vSAN_Cluster	NFSSwitch	vmnic2	NFS	192.168.3.23
    esxi24.v.lab	iSCSI_Cluster	NFSSwitch	vmnic2	NFS	192.168.3.24
    esxi25.v.lab	iSCSI_Cluster	NFSSwitch	vmnic2	NFS	192.168.3.25
    esxi21.v.lab	vSAN_Cluster	vSANSwitch	vmnic3	vSAN1	192.168.5.21
    esxi22.v.lab	vSAN_Cluster	vSANSwitch	vmnic3	vSAN1	192.168.5.22
    esxi23.v.lab	vSAN_Cluster	vSANSwitch	vmnic3	vSAN1	192.168.5.23
    esxi24.v.lab	iSCSI_Cluster	iSCSISwitch	vmnic3	iSCSI1	192.168.4.24
    esxi25.v.lab	iSCSI_Cluster	iSCSISwitch	vmnic3	iSCSI1	192.168.4.25
    esxi21.v.lab	vSAN_Cluster	vSANSwitch	vmnic4	vSAN2	192.168.5.31
    esxi22.v.lab	vSAN_Cluster	vSANSwitch	vmnic4	vSAN2	192.168.5.32
    esxi23.v.lab	vSAN_Cluster	vSANSwitch	vmnic4	vSAN2	192.168.5.33
    esxi24.v.lab	iSCSI_Cluster	iSCSISwitch	vmnic4	iSCSI2	192.168.4.34
    esxi25.v.lab	iSCSI_Cluster	iSCSISwitch	vmnic4	iSCSI2	192.168.4.35

    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 |
    	ForEach-Object{
    		$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
       mand



    • 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') } |
    	ForEach-Object{
     		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
       d

    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

    $ipa
    
    IPAddress:
    ----------
    192.168.4.24
    192.168.4.25
    192.168.4.34
    192.168.4.35

    If I use write-host $ipa I get

    @{IPAddress:=192.168.4.24} @{IPAddress:=192.168.4.25} @{IPAddress:=192.168.4.34} @{IPAddress:=192.168.4.35}

    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 |
    	ForEach-Object{
    		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 |
    	ForEach-Object{
    		$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
    esxi21.v.lab,vSAN_Cluster,vSwitch0,vmnic1,ManagementNetwork2,192.168.2.31
    esxi22.v.lab,vSAN_Cluster,vSwitch0,vmnic1,ManagementNetwork2,192.168.2.32
    esxi23.v.lab,vSAN_Cluster,vSwitch0,vmnic1,ManagementNetwork2,192.168.2.33
    esxi24.v.lab,iSCSI_Cluster,vSwitch0,vmnic1,ManagementNetwork2,192.168.2.34
    esxi25.v.lab,iSCSI_Cluster,vSwitch0,vmnic1,ManagementNetwork2,192.168.2.35
    esxi21.v.lab,vSAN_Cluster,vSANSwitch,vmnic3,vSAN1,192.168.5.21
    esxi22.v.lab,vSAN_Cluster,vSANSwitch,vmnic3,vSAN1,192.168.5.22
    esxi23.v.lab,vSAN_Cluster,vSANSwitch,vmnic3,vSAN1,192.168.5.23
    esxi24.v.lab,iSCSI_Cluster,iSCSISwitch,vmnic3,iSCSI1,192.168.4.24
    esxi25.v.lab,iSCSI_Cluster,iSCSISwitch,vmnic3,iSCSI1,192.168.4.25
    esxi21.v.lab,vSAN_Cluster,vSANSwitch,vmnic4,vSAN2,192.168.5.31
    esxi22.v.lab,vSAN_Cluster,vSANSwitch,vmnic4,vSAN2,192.168.5.32
    esxi23.v.lab,vSAN_Cluster,vSANSwitch,vmnic4,vSAN2,192.168.5.33
    esxi24.v.lab,iSCSI_Cluster,iSCSISwitch,vmnic4,iSCSI2,192.168.4.34
    esxi25.v.lab,iSCSI_Cluster,iSCSISwitch,vmnic4,iSCSI2,192.168.4.35

    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