VMware Cloud Community
jconry
Contributor
Contributor
Jump to solution

import-csv with If statement

Hi guys,

I'm trying to add an option in my PS script whereby it parses a .csv file for specific names and applies the OS customization argument to them. For example:

If "Name" = xxx-xxx-DCxx in "filename.csv" Then

Foreach New-VM with OSCustomizationSpec argument

Else

Foreach New-VM without OSCustomizationSpec argument

Thanks.

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Not the most optimal solution but this should do the trick

$entries = Import-Csv "C:\Test.csv"
$entries | Where-Object {$_.Name -like "???-???-DC??"} | %{
	New-VM -Name $_.Name -OSCustomizationSpec $custom
}
$entries | Where-Object {-not ($_.Name -like "???-???-DC??")} | %{
	New-VM -Name $_.Name
}

The CSV file I used for testing looks like this

"Name","Field1","Field2"
"abc-123-DC01",1,"abc"
"server1",2,"abc"
"another-server",1,"abc"
"abc-456-DC02",5,"abc"
"abc-abc-DC03",7,"abc"
"abc-abc-DC3",7,"abc"
"klm-789-SR09",3,"abc"


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

View solution in original post

0 Kudos
7 Replies
LucD
Leadership
Leadership
Jump to solution

Not the most optimal solution but this should do the trick

$entries = Import-Csv "C:\Test.csv"
$entries | Where-Object {$_.Name -like "???-???-DC??"} | %{
	New-VM -Name $_.Name -OSCustomizationSpec $custom
}
$entries | Where-Object {-not ($_.Name -like "???-???-DC??")} | %{
	New-VM -Name $_.Name
}

The CSV file I used for testing looks like this

"Name","Field1","Field2"
"abc-123-DC01",1,"abc"
"server1",2,"abc"
"another-server",1,"abc"
"abc-456-DC02",5,"abc"
"abc-abc-DC03",7,"abc"
"abc-abc-DC3",7,"abc"
"klm-789-SR09",3,"abc"


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

0 Kudos
jconry
Contributor
Contributor
Jump to solution

Lovely, thanks for that.

As a side note, is there anyway of running a runonce command without having to use an OS customization?

I've got dcpromo /answer:c:blah as a runonce in my OS customization spec for the dc templates, but I'm coming unstuck on the network config side. The templates are already configured with the right network settings, but with the OS customization spec I can either configure it for DHCP, (which I don't want) or set a static in which case all of the new dc's will get the same ip. Or is there another way?

Or could I do a netsh >answer file as a runonce as well to configure the network before doing the dcpromo?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

The only real solution I can think of is to

1) prepare a sysprep.inf file per target guest.

This should contain the necessary sections to configure the network (\[Networking\], \[NetProtocols\]...) with the correct network parameters.

2) create a customisation profile that uses the sysprep.inf file defined in step 1)

Unfortunately the current New-OSCustomizationSpec cmdlet doesn't foresee this as a parameter.

It means you have to use the SDK method CreateCustomizationSpec

3) deploy the new guest with the customisation profile defined in step 2)

Let me know if you have a problem with any of these steps?


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

jconry
Contributor
Contributor
Jump to solution

I might have to do a bit of playing around with SDK but I should be able to work it out. Thanks again.

Sorry, one more question. In the OS customization wizard, choosing a method for selecting a static ip, the third option (greyed out), "Use an application configured on the VC server to generate the IP address". Can I enable this and get it to set my IP's? And the app that it is referring to can it just be a .csv or similar that generates the ip's?

Cheers.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

That feature was rather mysterious in VI.

See .

I haven't checked if it is enabled in vSphere as was apparently promised.

The vSphere manuals don't seem to give any details though.


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

0 Kudos
DougBaer
Commander
Commander
Jump to solution

I believe this option becomes enabled if you create a vApp, add your VM(s) to the vApp and specify the IP address parameters on the vApp object.

Doug Baer, Solution Architect, Advanced Services, Broadcom | VCDX #019, vExpert 2012-23
0 Kudos
jconry
Contributor
Contributor
Jump to solution

Excellent, that might give me another reason to push these guys to vSphere.

I might have to give the SDK road a go until then though.

Cheers guys.

0 Kudos