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

Home Forums Scripting PowerShell Using Powershell to add iSCSI Targetname and IP from CSV file..

Tagged: 

This topic contains 1 reply, has 2 voices, and was last updated by Rems Rems 5 days, 10 hours ago.

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • Avatar
    huud
    Participant
    #624060

    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 :

    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

    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:

    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

    Rems
    Rems
    Moderator
    #624126

    Hi huud,

    It is not really necessary to go through $csvFile row by row, because Import-Csv imports the data as objects into a hash table.

    Because we are only interested in lines where VMK is equal to “iSCSI1” or “iSCSI2”, you can filter $csvFile, I did this in line 1 in the example below.

    1. $csvFile = Import-CSV -Path C:\Users\Administrator\Desktop\esxiHostsDeployment.csv‘ | Where {iSCSI1iSCSI2 -Contains $_.VMK}
    2. <# Check content of #>; $csvFile | Format-Table
    3. $virtualSwitch = $csvFile.Switch | Select -Unique
    4. $vmK = ($csvFile.VMK | Select -Unique) -Join  and 
    5. $ipa = @(“ + (($csvFile | % { “IPAddress:$($_.IP)” } | Select -Unique) -Join “,”) + “)
    6. $virtualSwitch ; $vmK ; $ipa

    In the lines 3 and 4, we did not iterate the entire list to set the values on the variables. In line 5 we did used the ForEach-Object cmdlet on $csvFile.IP, because this enabled us to append our standard text in front of each of the values. We could also have achieved this by creating a calculated property using the Select-Object cmdlet, for example:

    $ipa = @(“ + (($csvFile | Select-Object -Unique @{N=”IPmodified”;E={“IPAddress:$($_.IP)”}}).IPmodified -join “,”) + “)

    or, by using just a simple creative approach,

    $ipa = @(“IPAddress: + (($csvFile.IP | Select -Unique) -Join “,”IPAddress:) + “)

    The -join operator concatenates a set of values into a single string. As the <Delimiter> we used the ‘string’ and/or ‘quotes’ that we wanted to add between the values.

    For filtering $csvFile I have used your filter, which is perfect. An alternative could have been:

     | Where {$_.VMK -Like “iSCSI*”}

     

    Note When copying and pasting the code, make sure that the quotation marks ‘ and ” used in the examples are pasted as ‘Straight quotes‘ (as opposed to ‘Curly quotes’).

    /Rems

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.