VMware Cloud Community
BReed01
Contributor
Contributor
Jump to solution

How to incorporate If/Else csv check when deploying multiple VMs

I have a script that I'm working on that will deploy multiple VMs from a csv.  I'm trying to get the script to look at a field in the csv, and if blank, do this, if data, do it this other way.

There's a lot of extra fluff in the script, but here's the relevant info:

$VMs = Import-CSV ".\VM_Deploy.csv"

foreach ($vm in $VMs)

{

try

{

$Name = $vm.Name

$Template = Get-Template -Name $vm.Template

$VMVersion = $vm.VMVersion

$ResourcePool = $vm.ResourcePool

$Cluster = Get-Cluster -Name $vm.Cluster

$VMHost = Get-VMHost -Name $vm.VMHost

$Datastore = Get-Datastore -Name $vm.Datastore

$OSCust = Get-OSCustomizationSpec -Name $vm.OSCustomizationSpec

New-VM -Name $Name -Template $Template -ResourcePool $Cluster -VMHost $VMHost -Datastore $Datastore -OSCustomizationSpec $OSCusSpec

Not every site I am working on will have vCenter clusters setup (or may have multiple).  So what I was trying to do was check if the Cluster cell is blank, and if so, use the New-VM line above, and if it's not blank, then add in a -ResourcePool $Cluster.

I was trying to replace the above New-VM line with something like:

IF ([string]::IsNullOrEmpty($vm.Cluster))

{

New-VM -Name $Name -Template $Template -VMHost $VMHost -Datastore $Datastore -OSCustomizationSpec $OSCusSpec

}

Else

New-VM -Name $Name -Template $Template -ResourcePool $Cluster -VMHost $VMHost -Datastore $Datastore -OSCustomizationSpec $OSCusSpec

}

I also tried something like If ($vm.Cluster -eq $Null) , or IF ($Cluster -eq ' ') etc etc.

I just can't seem to figure out how to get it to read a blank cell and do X, and if not blank, do Y.  It typically just fails right away and nothing gets deployed no matter how I attempt it.

Anyone have any suggestions?  I would greatly appreciate it.

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

I would need to see your CSV file to make any definite conclusions, but a simple test CSV the following works for me.

I used an inline CSV for testing, but you can replace the ConvertFrom-Csv with Import-Csv and you CSV file

$data = @'

VM,Cluster,Datastore

vm1,cluster1,ds1

vm2,,ds2

'@


ConvertFrom-Csv -InputObject $data -UseCulture -PipelineVariable row |

ForEach-Object -Process {

    if($row.Cluster){

        Write-Host "For $($row.VM) cluster value is $($row.Cluster)"

    }

    else{

        Write-Host "For $($row.VM) cluster field is empty "

    }

}


A basic concept with Import-Csv (or ConvertFrom-Csv) is that each property in a row is a string.
An empty string is translated to $false in a test, while a string with anything in there is considered $true.

The other tests you used

$row.Cluster -eq ''

or

[string]::IsNullOrEmpty($row.Cluster)

should also work to discover empty cells in the CSV.

There must be something in your CSV that makes these tests not work for you.


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

View solution in original post

Reply
0 Kudos
3 Replies
LucD
Leadership
Leadership
Jump to solution

I would need to see your CSV file to make any definite conclusions, but a simple test CSV the following works for me.

I used an inline CSV for testing, but you can replace the ConvertFrom-Csv with Import-Csv and you CSV file

$data = @'

VM,Cluster,Datastore

vm1,cluster1,ds1

vm2,,ds2

'@


ConvertFrom-Csv -InputObject $data -UseCulture -PipelineVariable row |

ForEach-Object -Process {

    if($row.Cluster){

        Write-Host "For $($row.VM) cluster value is $($row.Cluster)"

    }

    else{

        Write-Host "For $($row.VM) cluster field is empty "

    }

}


A basic concept with Import-Csv (or ConvertFrom-Csv) is that each property in a row is a string.
An empty string is translated to $false in a test, while a string with anything in there is considered $true.

The other tests you used

$row.Cluster -eq ''

or

[string]::IsNullOrEmpty($row.Cluster)

should also work to discover empty cells in the CSV.

There must be something in your CSV that makes these tests not work for you.


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

Reply
0 Kudos
BReed01
Contributor
Contributor
Jump to solution

Thank you, LucD.  I'm using an Import-CSV process.

I just went ahead and attached the whole script and csv to the orig post.  I do know there are several things I can probably do better, and do probably have a lot of unnecessary fluff in the script.  The attachment does not have any of the testing I was trying to do.

Any feedback is most welcome though. 

On a side note, I think I was reading that you don't have to use the x86 of Powershell for this type of script anymore.  Do you know if that's true?  I am testing for that in the script, so am curious if it's even necessary.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Your CSV looks like a normal CSV.
When I tested with that CSV, the if-statements I gave in my previous reply all worked as expected.

And yes, you can don't have to use the 32-bit PS version anymore (when you are on a recent PowerCLI version).


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

Reply
0 Kudos