VMware Cloud Community
MediaSat
Contributor
Contributor
Jump to solution

Problem with Powershell script for vSpherePowerCLI

Hi,

I'm trying to create a script that clones from one VMware-Template as many VM-environments as I need.

I found a script online which should do that:

____________________________________

Connect-VIServer -Server 192.168.0.1

Get-VMHost

$host1 = "192.168.0.2"

$template = "template1"

$datastore = "data1"

$customization = "templtest"

for($i = 1; $i -le 3; $i++)

{

$tempvm = "vServer-"+$i

$array += $tempvm

}

foreach ($vm in $array)

{

$vm=New-VM -Name $vm -Template $template -Host $host1 -Datastore $datastore -OSCustomizationSpec $customization -Confirm:$false

}

_____________________________

for example, that script should create 3 VMs of template1 with the names: vServer-1, vServer-2 and vServer3.

But it only creates one VM with the name vServer-1vServer-2vServer-3.

Can somebody help me with that problem?

Is there something wrong in the for-statement?

thanks in anticipation,

Alex

0 Kudos
1 Solution

Accepted Solutions
RvdNieuwendijk
Leadership
Leadership
Jump to solution

Hi Alex,

Before the for statement you have to insert the following line:

$array = @()

This will initialize the $array variable as an empty array.

Regards, Robert

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

0 Kudos
2 Replies
RvdNieuwendijk
Leadership
Leadership
Jump to solution

Hi Alex,

Before the for statement you have to insert the following line:

$array = @()

This will initialize the $array variable as an empty array.

Regards, Robert

Message was edited by: RvdNieuwendijk

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

oh yes, now it works, thank you very much!!!

0 Kudos