Using Powershell to add iSCSI Targetname and IP..

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 4 years and 5 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
tryllzhuud
Posts: 7
Last visit: Tue Nov 09, 2021 5:30 am

Using Powershell to add iSCSI Targetname and IP..

Post 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
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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

Post 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
	}
tryllzhuud
Posts: 7
Last visit: Tue Nov 09, 2021 5:30 am

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

Post 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
tryllzhuud
Posts: 7
Last visit: Tue Nov 09, 2021 5:30 am

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

Post 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.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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

Post 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.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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

Post 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
tryllzhuud
Posts: 7
Last visit: Tue Nov 09, 2021 5:30 am

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

Post 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}
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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

Post 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.
tryllzhuud
Posts: 7
Last visit: Tue Nov 09, 2021 5:30 am

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

Post 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.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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

Post 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.
This topic is 4 years and 5 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked