Page 1 of 2

Using Powershell to add iSCSI Targetname and IP..

Posted: Fri Oct 11, 2019 12:39 pm
by tryllzhuud
I have the following script which retrieves IP addresses from a CSV file but does not add them to the -InitiatorId.
  1. $csvFile = Import-CSV -Path C:\Users\Administrator\Desktop\esxiHostsDeployment.csv
  2. foreach ($NFSRow in $csvFile)
  3.    {
  4.       $virtualSwitch = $NFSRow.Switch
  5.       $vmK = $NFSRow.VMK
  6.       $ipa = $csvFile | Where {“iSCSI1″,”iSCSI2″ -contains $_.VMK} | Select -Property @{N=”IPAddress:”;E={$_.IP}}
  7.    }
When I check the variable value with $ipa I get :
  1. IPAddress:
  2. ———-
  3. 192.168.4.24
  4. 192.168.4.25
  5. 192.168.4.34
  6. 192.168.4.35
If I use write-host $ipa I get
  1. write-host $ipa
  2. @{IPAddress:=192.168.4.24} @{IPAddress:=192.168.4.25} @{IPAddress:=192.168.4.34} @{IPAddress:=192.168.4.35}
And when this is passed to the -InitiatorId I get the following error:
  1. Set-IscsiServerTarget : Cannot bind parameter ‘InitiatorIds’. Cannot convert the “@{IPAddress:=192.168.4.24}” value of type
  2. “Selected.System.Management.Automation.PSCustomObject” to type “Microsoft.Iscsi.Target.Commands.InitiatorId”.
  3. At line:1 char:61
  4. + Set-IscsiServerTarget -TargetName “esxiHosts1” -InitiatorId $ipa
  5. + ~~~~
  6. + CategoryInfo : InvalidArgument: (:) [Set-IscsiServerTarget], ParameterBindingException
  7. + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.Iscsi.Target.Commands.SetIscsiServerTargetCommand
Any suggestions how I can get the results in the format : IPAddress:192.168.4.24, IPAddress:192.168.4.25 in the results.

Thank You

Re: Using Powershell to add iSCSI Targetname and IP..

Posted: Fri Oct 11, 2019 1:24 pm
by jvierra
Here is what we have already discussed:

Code: Select all

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
	}

Re: Using Powershell to add iSCSI Targetname and IP..

Posted: Fri Oct 11, 2019 11:42 pm
by tryllzhuud
jvierra wrote: Fri Oct 11, 2019 1:24 pm Here is what we have already discussed:

Code: Select all

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
	}
Appreciate that, I believe you are confusing the Targetname esxiHost1 as the names of esxi hosts in the csv file, both are different. I'm acquiring the IP addresses from the csv file based on the VMK, nothing more.

Code: Select all

Write-Host : Cannot bind parameter 'ForegroundColor'. Cannot convert value "esxi21.v.lab,192.168.2.31" to type "System.ConsoleColor".
Error: "Unable to match the identifier name esxi21.v.lab,192.168.2.31 to a valid enumerator name. Specify one of the following
enumerator names and try again:
Black, DarkBlue, DarkGreen, DarkCyan, DarkRed, DarkMagenta, DarkYellow, Gray, DarkGray, Blue, Green, Cyan, Red, Magenta, Yellow, White"
At line:3 char:41
+ Write-Host 'Host-{0}  IPAddress:{1}' -f $_.Hosts, ($_.IP)
+                                         ~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Write-Host], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.WriteHostCommand

Re: Using Powershell to add iSCSI Targetname and IP..

Posted: Fri Oct 11, 2019 11:49 pm
by tryllzhuud
To make matters simpler, I have done with GUI what I'm trying to achieve via Powershell so I can explain better.

I have uploaded screenshots here : https://imgur.com/a/UTdjzfX

Thanks for all the assistance.

Re: Using Powershell to add iSCSI Targetname and IP..

Posted: Fri Oct 11, 2019 11:52 pm
by jvierra
So you are saying that you want to bind all addresses to one host. Just change the target name and all will get bound to the same target.

Re: Using Powershell to add iSCSI Targetname and IP..

Posted: Sat Oct 12, 2019 12:25 am
by jvierra
If the following isn't what you are asking then I am sure I will never guess correctly unless you can figure out how to ask the correct question

Code: Select all

$initiatorId = Import-CSV C:\Users\Administrator\Desktop\esxiHostsDeployment.csv |
	Where-Object{ $_.DMK -match 'iSCSI1|iSCSI2'} |
	ForEach-Object{'IPAddress:{0}' -f $_.IP}

Write-Host ('Host-{0}  IPAddress:{1}' -f 'esxiHosts1', "$initiatorId")
New-IscsiServerTarget -TargetName esxiHosts1 -InitiatorId $initiatorId -WhatIf

Re: Using Powershell to add iSCSI Targetname and IP..

Posted: Sat Oct 12, 2019 7:41 am
by tryllzhuud
jvierra wrote: Sat Oct 12, 2019 12:25 am If the following isn't what you are asking then I am sure I will never guess correctly unless you can figure out how to ask the correct question

Code: Select all

$initiatorId = Import-CSV C:\Users\Administrator\Desktop\esxiHostsDeployment.csv |
	Where-Object{ $_.DMK -match 'iSCSI1|iSCSI2'} |
	ForEach-Object{'IPAddress:{0}' -f $_.IP}

Write-Host ('Host-{0}  IPAddress:{1}' -f 'esxiHosts1', "$initiatorId")
New-IscsiServerTarget -TargetName esxiHosts1 -InitiatorId $initiatorId -WhatIf
Thanks, this is exactly what I wanted to know..

I'm guessing -f switch is for find ?!

And how does adding braces with zero work in ForEach-Object{'IPAddress:{0}' -f $_.IP}

Re: Using Powershell to add iSCSI Targetname and IP..

Posted: Sat Oct 12, 2019 9:19 am
by jvierra
You are asking questions that show you have not taken the time to learn PowerShell basics. Please note that it is not possible to learn PowerShell by asking questions in forums. You will get misleading information and never actually learn how to use PowerShell. It is necessary to take a good tutorial or purchase a book to gain the fundamentals of PowerShell.

I recommend a book as it is easier to check back to and refresh you knowledge.
As with any advanced technology you must take the time to understand the basic technology in order to use it correctly.

Re: Using Powershell to add iSCSI Targetname and IP..

Posted: Sat Oct 12, 2019 10:59 am
by tryllzhuud
jvierra wrote: Sat Oct 12, 2019 9:19 am You are asking questions that show you have not taken the time to learn PowerShell basics. Please note that it is not possible to learn PowerShell by asking questions in forums. You will get misleading information and never actually learn how to use PowerShell. It is necessary to take a good tutorial or purchase a book to gain the fundamentals of PowerShell.

I recommend a book as it is easier to check back to and refresh you knowledge.
As with any advanced technology you must take the time to understand the basic technology in order to use it correctly.
Thanks, yes I did go through the basics, however, not completely.

Could you recommend a good one.

Re: Using Powershell to add iSCSI Targetname and IP..

Posted: Sat Oct 12, 2019 11:19 am
by jvierra
"PowerShell in Action Third Edition" by members of the PowerShell development team. Available for all major booksellers and from the publisher -Manning Publications.