Using Powershell to add iSCSI Targetname and IP from CSV file..

I have the following script which retrieves IP addresses from a CSV file but does not add them to the -InitiatorId.

$csvFile = Import-CSV -Path C:\Users\Administrator\Desktop\esxiHostsDeployment.csv
foreach ($NFSRow in $csvFile)
$virtualSwitch = $NFSRow.Switch
$vmK = $NFSRow.VMK
$ipa = $csvFile | Where {“iSCSI1″,”iSCSI2″ -contains $_.VMK} | Select -Property @{N=”IPAddress:”;E={$_.IP}}

When I check the variable value with $ipa I get :


If I use write-host $ipa I get

write-host $ipa
@{IPAddress:=} @{IPAddress:=} @{IPAddress:=} @{IPAddress:=}

And when this is passed to the -InitiatorId I get the following error:

Set-IscsiServerTarget : Cannot bind parameter 'InitiatorIds'. Cannot convert the “@{IPAddress:=}” value of type
“Selected.System.Management.Automation.PSCustomObject” to type “Microsoft.Iscsi.Target.Commands.InitiatorId”.
At line:1 char:61
+ Set-IscsiServerTarget -TargetName “esxiHosts1” -InitiatorId $ipa
+ ~~~~
+ CategoryInfo : InvalidArgument: (:) [Set-IscsiServerTarget], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.Iscsi.Target.Commands.SetIscsiServerTargetCommand

Any suggestions how I can get the results in the format : IPAddress:, IPAddress: in the results.

Thank You

Try this:

$ipa = $csvFile | Where {“iSCSI1”,“iSCSI2” -contains $.VMK} | %{ “IPAddress:{0}” -f $.IP}