Using Powershell to add iSCSI Targetname and IP..

Ask your Windows 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.
tryllzhuud
Posts: 6
Joined: Fri Oct 11, 2019 12:36 pm

Using Powershell to add iSCSI Targetname and IP..

Post by tryllzhuud » Fri Oct 11, 2019 12:39 pm

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

Code: Select all

$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 :

Code: Select all

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

Code: Select all

write-host $ipa
@{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:

Code: Select all

Set-IscsiServerTarget : Cannot bind parameter ‘InitiatorIds’. Cannot convert the “@{IPAddress:=192.168.4.24}” 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:192.168.4.24, IPAddress:192.168.4.25 in the results.

Thank You

User avatar
jvierra
Posts: 13802
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post by jvierra » 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
	}

tryllzhuud
Posts: 6
Joined: Fri Oct 11, 2019 12:36 pm

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

Post by tryllzhuud » Fri Oct 11, 2019 11:42 pm

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: 6
Joined: Fri Oct 11, 2019 12:36 pm

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

Post by tryllzhuud » Fri Oct 11, 2019 11:49 pm

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.

User avatar
jvierra
Posts: 13802
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post by jvierra » Fri Oct 11, 2019 11:52 pm

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.

User avatar
jvierra
Posts: 13802
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post by jvierra » 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

tryllzhuud
Posts: 6
Joined: Fri Oct 11, 2019 12:36 pm

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

Post by tryllzhuud » Sat Oct 12, 2019 7:41 am

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}

User avatar
jvierra
Posts: 13802
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post by jvierra » 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.

tryllzhuud
Posts: 6
Joined: Fri Oct 11, 2019 12:36 pm

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

Post by tryllzhuud » Sat Oct 12, 2019 10:59 am

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.

User avatar
jvierra
Posts: 13802
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post by jvierra » Sat Oct 12, 2019 11:19 am

"PowerShell in Action Third Edition" by members of the PowerShell development team. Available for all major booksellers and from the publisher -Manning Publications.

Post Reply