VMware Cloud Community
friedeva
Contributor
Contributor
Jump to solution

Configure vswitches starting from a .csv file - problem

I have a script which is working fine for the setup of my virtual switches using the updatevirtualswitch method. (thanks to LucD, see here : http://communities.vmware.com/message/1556669#1556669)

I would like now to go a step further and feed my script with variables coming from a .csv file.

This is working fine for everything, except for the vmnic definition. Problem description:

Excerpt from my .csv file :

lannics;dmznic1;dmznic2;dmznic3;storagenics;vmotionnics;

@("vmnic0");@("vmnic1", "vmnic2"); ; ; ; ;

Then I import the parameters from the csv file, example:

$lannics = $parameterfile.dmznic1

Now, if I check what's in $dmznic1, I have the correct : @("vmnic1", "vmnic2")

But it seems to be a string, not a real array. Therefore, I can't pass it to my updatevirtualswitch function :

function standardvswitch {

param ($esx, $vs, [string[]]$dmznic1)

....

$ns.UpdateVirtualSwitch( $vs, $vsSpec)

}

So the question is: in which way could I enter the informations in my .csv file, so that it can be used for a vmnic definition compatible with the UpdateVirtualSwitch method?

Thanks for your help or ideas!

Reply
0 Kudos
1 Solution

Accepted Solutions
RvdNieuwendijk
Leadership
Leadership
Jump to solution

I did some more testing and saw that my previous solution doesn't work. But the next one does ;-). I will try to explain how it works. In the .csv file a semicolon is used as a field seperator. This means that you can use a comma within a field to seperate array members. The Import-CSV cmdlet reads the .csv file and the -Delimiter ";" parameter tells the cmdlet that a semicolon is the field seperator. The output of the Import-CSV cmdlet is piped into a ForEach-Object cmdlet. In the scriptblock following ForEach-Object the string value of the dmznic1 property is split by the comma's so each string before, between and after the comma's will become a seperate array member. This array is assigned to the dmznic1 property. The for loop loops through all the array members and displays them on separate lines. So you can see that it is really an array.

Import-CSV -Path LanNics.CSV -Delimiter ";" | `
ForEach-Object { 
  $_.dmznic1 = ($_.dmznic1).split(",")
  for ($i=0;$i -lt $_.dmznic1.length; $i++) {
    Write-Output $_.dmznic1[$i]
  }
}

See the attached screenshot for the output.

I think this solution is more beautiful than creating a different column for each vmnic because in my solution you don't have to know in advance how many nics you will have.

Message was edited by: RvdNieuwendijk

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition

View solution in original post

Reply
0 Kudos
7 Replies
RvdNieuwendijk
Leadership
Leadership
Jump to solution

You can try something like this:

[vSphere PowerCLI] C:\users\robert> type lannics.csv
lannics;dmznic1;dmznic2;dmznic3;storagenics;vmotionnics
vmnic0;vmnic1,vmnic2; ; ; ;
[vSphere PowerCLI] C:\users\robert> import-csv -path lannics.csv -delimiter ";" | %{ $_.dmznic1 = @($_.dmznic1); $_}


lannics     : vmnic0
dmznic1     : {vmnic1,vmnic2}
dmznic2     :
dmznic3     :
storagenics :
vmotionnics :

Regards, Robert

After some more testing I realized that this solution doesn't work. See my post below for a correct solution.

Message was edited by: RvdNieuwendijk

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

In my opinion you are misusing the CSV filetype.

A CSV file is not intended to contain nested properties or complex data types.

If you want to export structured data I would advise to use the Export-Clixml and Import-Clixml cmdlets.

An XML file is probably not as easily editable with notepad as a CSV file, but the free XMLPad can help.

____________

Blog: LucD notes

Twitter: lucd22


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
friedeva
Contributor
Contributor
Jump to solution

Thank you Robert, but I'm not sure I really understand your oneliner Smiley Happy

Nevertheless, I modified the .csv file as you did, and imported the dmznic1 parameter from the csv file this way:

$dmznic1=@($parameters.dmznic1)

I suppose that it is equivalent to your import command (please correct me if I'm wrong).

Anyway, my function still fails with the same message : Exception calling "UpdateVirtualSwitch" with "2" argument(s): "The object or item referred to could not be found."

This exact message appeared already when I was calling the function with an incorrect parameter type. (see the first post for a link to the original thread).

Reply
0 Kudos
friedeva
Contributor
Contributor
Jump to solution

LucD, I am quite far now with my project. In fact, I'm automating a complete esxi post-install, so the .csv file contains a lot more information (ip adresses, computer names...) and I would have been happy to make it work with my .csv file...

After a first look, the import-clixml does not seem as easy as import-csv... But ok, if it really can't work I'll take a look at these command.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

No, no, I'm not saying it can't work with a CSV file. I just wanted to point out that a CSV file is not intended for structured objects.

For example, it's not advisable to store an array object in a single column in a CSV file.

But you can find other solutions with the CSV file.

For example: instead of storing the active NICs as an array in the aNic column, you could introduce several columns (up to the maximum you have on your servers).

Something like:

anic1,anic2,anic3,anic4

vmnic2,vmnic3,vmnic4,vmnic5

If you use less than the maximum, you leave the value blank, like:

anic1,anic2,anic3,anic4

vmnic2,vmnic3,,

If you import this CSV file you just have to test if the variable is blank or not and re-create the required array in your script.

____________

Blog: LucD notes

Twitter: lucd22


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

RvdNieuwendijk
Leadership
Leadership
Jump to solution

I did some more testing and saw that my previous solution doesn't work. But the next one does ;-). I will try to explain how it works. In the .csv file a semicolon is used as a field seperator. This means that you can use a comma within a field to seperate array members. The Import-CSV cmdlet reads the .csv file and the -Delimiter ";" parameter tells the cmdlet that a semicolon is the field seperator. The output of the Import-CSV cmdlet is piped into a ForEach-Object cmdlet. In the scriptblock following ForEach-Object the string value of the dmznic1 property is split by the comma's so each string before, between and after the comma's will become a seperate array member. This array is assigned to the dmznic1 property. The for loop loops through all the array members and displays them on separate lines. So you can see that it is really an array.

Import-CSV -Path LanNics.CSV -Delimiter ";" | `
ForEach-Object { 
  $_.dmznic1 = ($_.dmznic1).split(",")
  for ($i=0;$i -lt $_.dmznic1.length; $i++) {
    Write-Output $_.dmznic1[$i]
  }
}

See the attached screenshot for the output.

I think this solution is more beautiful than creating a different column for each vmnic because in my solution you don't have to know in advance how many nics you will have.

Message was edited by: RvdNieuwendijk

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Reply
0 Kudos
friedeva
Contributor
Contributor
Jump to solution

Hello Robert,

thank you very much for spending time on this, and then again for explaining your solution in the detail.

Your solution works beautifuly and allows me to keep working with my good old .csv file.

Thanks a lot!

Regards, Friedrich

Reply
0 Kudos