VMware Cloud Community
pjapk
Contributor
Contributor
Jump to solution

Trying to understand PowerCLI script problem

I'm a rank beginner to PowerShell/CLI but it's been on my list of things to  get my head around for some time so I've been playing around with it for a week or so and have managed to resolve most issues with research but at the moment I'm trying to adapt a PowerCLI script I've come across as part of a personal project to automate the creation of a vLAB and I don't understand the problem well enough to be able to research it properly so could do with a pointer if possible.

The script I've started with is at http://poshcode.org/?show=1547

The part of the code I'm having issues with is line 25 inthe original code which is:

$rpool = for ( $i = 0; $i -lt $vmhost.length; $i++ )

If I run just this line from a prompt I get the same error which is posted below:

[vSphere PowerCLI] C:\> $rpool = for ( $i = 0; $i -lt $vmhost.length; $i++ )
Missing closing ')' in expression.
At line:1 char:22
+ $rpool = for ( $i = 0; <<<<  $i -lt $vmhost.length; $i++ )

I've found that if I omit the "$rpool =" then this line of code executes, but then I can't make use of the results (although I concede that someone who knows what they're doing may well be able to! Smiley Happy)

So, what am I not understanding well enough to make this code work?

Most of the rest of the script is adapted to my environment where I'm creating VMs from scratch rather than from templates, plus I'm dealing with three hosts but other than that it's mostly the same.

Any pointers appreciated!

(I would have commented on the site hosting the original code but I couldn't find any means of doing so).

Regards,

Paul

Reply
0 Kudos
1 Solution

Accepted Solutions
RvdNieuwendijk
Leadership
Leadership
Jump to solution

For me the command returns the numbers from 0 to 6, just like you expected. It seems that there is something in your PowerShell session that prevents the for statement from running correctly. It looks like you have redefined the for command to something else. Maybe an alias or a function.

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
16 Replies
RvdNieuwendijk
Leadership
Leadership
Jump to solution

Hi Paul,

in the script you mentioned line 25 to 28 is one command. So you can't just execute only line 25. This piece of code creates a resourcepool for every host. Line 25 is just the loop through the number of hosts. The scriptblock behind it creates the resourcepools.

Regards, Robert

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

Thanks for the prompt reply Robert!

You are quite correct, however I do have that whole code-block within my script & still get the same error (I was just attempting to break down the code into manageable chunks to trouble-shoot.

e.g. with my beginners eyes I can do the following:

[vSphere PowerCLI] C:\> $vmhost = "vESXi03"; for ( $i = 0; $i -lt $vmhost.length; $i++ ) {write-host $i}
0
1
2
3
4
5
6

But if I try this I get the same error:

[vSphere PowerCLI] C:\> $vmhost = "vESXi03"; $rpool = for ( $i = 0; $i -lt $vmhost.length; $i++ ) {write-host $i}
Missing closing ')' in expression.
At line:1 char:43
+ $vmhost = "vESXi003"; $rpool = for ( $i = 0; <<<<  $i -lt $vmhost.length; $i++ ) {write-host $i}

As I said, I fully accept that this is only a snippet of code & likely to not run correctly in isolation, but all the rest is in the main script & it still fails at this line.

The most significant change I've made is reducing the number of hosts, and therefore the number of "customer codenames" to just three.

Thanks again,

Paul

Reply
0 Kudos
RvdNieuwendijk
Leadership
Leadership
Jump to solution

For me the command returns the numbers from 0 to 6, just like you expected. It seems that there is something in your PowerShell session that prevents the for statement from running correctly. It looks like you have redefined the for command to something else. Maybe an alias or a function.

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

Not sure I understand why you need to do: $rpool = for...

There's no point that I can see in assigning the output of the for command to the variable $rpool. The next line writes out 0-6 because the length of the $vmhost variable is seven characters. Perhaps it might be easier if you were to post a copy of the script so that we can take a look at it?

Twitter: @sixfootdad Blog: damiankarlson.com Podcast: professionalvmware.com/brownbags
Reply
0 Kudos
pjapk
Contributor
Contributor
Jump to solution

Hmm, I haven't experimented with aliases yet and this is a new installation of PowerCLI (and only used PowerShell for the first time I believe)

I appreciate that trouble-shooting snippets of code isn't the easiest of tasks so here's the whole thing, warts 'n' all (don't laugh!) Smiley Happy

# Connect to vCenter
Connect-VIServer <my.viserver.here>

$MyVMNetwork = "VM Network"

# root folder is used for datacenter location
$rootfolder = Get-Folder -NoRecursion

# datacenter
$dc = New-Datacenter -Name "vFarm" -Location $rootfolder

# Build hostname strings for ESX servers in format sssrrrnn (site/role/number)
$esxname = 2..4 | ForEach-Object { "vESXi{0:00}" -f $_ }

# prompt for ESX server credentials
$esxcred = Get-Credential

# Add ESX servers to vCenter
$vmhost = $esxname | ForEach-Object {
Add-VMHost -Name $_ -Credential $esxcred
}

# Customer codenames, same number as there are hosts
$custname = "AAA", "BBB", "CCC"

# Create customer resource pools, one per ESX host
$rpool = for ( $i = 0; $i -lt $vmhost.length; $i++ ) {
# Can set resource settings such as mem or cpu limit here
New-ResourcePool -Location $vmhost[$i] -Name $custname[$i]
}

# Create array of hashtables (think of it like a spreadsheet)
# describing role names and number of VMs in each
$roleinfo = @(
@{ Name = "Prx"; Num = 1 },
@{ Name = "Web"; Num = 1 },
@{ Name = "DBS"; Num = 1 }
)

# Create role resource pools and all VMs
foreach ( $custpool in $rpool ) {
foreach ( $role in $roleinfo ) {
  # Create role resource pool
  $rolepool = New-ResourcePool -Name $role["Name"] -Location $custpool
 
  # Use number field to determine how many VMs to make and what to name them
  1..$role["Num"] | ForEach-Object {

   # Create VM name, e.g. KRO-WebServer-1
   $vmname = $custpool.Name + "-" + $role["Name"] + "-$_"
  
   # Create VM based on predefined templates
   # New-VM -Name $vmname -ResourcePool $rolepool -Template $role["Name"]

   # I don't have suitable templates available so creating from scratch for now

      New-VM -Name $vmname -ResourcePool $rolepool `
      -DiskMB 10 `
      -DiskStorageFormat thin `
      -MemoryMB 256 `
      -GuestId rhel5_64Guest `
      -NetworkName $MyVMNetwork

  }
}
}

Reply
0 Kudos
damiankarlson
Enthusiast
Enthusiast
Jump to solution

I agree with Robert -- I can't see anything wrong with the syntax of the code you posted. There doesn't appear to be an error.

Twitter: @sixfootdad Blog: damiankarlson.com Podcast: professionalvmware.com/brownbags
pjapk
Contributor
Contributor
Jump to solution

Thanks again both, looks like Robert is onto something.

I've now tried running the script on another machine & although it has other errors (relating to credentials) it doesn't stumble at the same place as in this thread so there *must* be something funky about the original machine I'm trying to run it on.

Will continue experimenting on another machine & see how much further I get.

Appreciate the help for a noob!

(Hopefully I've also awarded points correctly)

Paul

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Did you check on the first machine if there is an alias or a function with the name 'for' present ?

It might have come from one of your profile files.

Do a

Get-Alias for

Get-Command for

If any of these return anything, there is a definition somewhere that causes the problem.


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

Reply
0 Kudos
pjapk
Contributor
Contributor
Jump to solution

Just checked but doesn't look like it:

[vSphere PowerCLI] C:\> get-alias for
Get-Alias : Cannot find alias because alias 'for' does not exist.
At line:1 char:10
+ get-alias  <<<< for
[vSphere PowerCLI] C:\> get-command for
Get-Command : The term 'for' is not recognized as a cmdlet, function, operable
program, or script file. Verify the term and try again.
At line:1 char:12
+ get-command  <<<< for

Not a clue what else it could be then!

Paul

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

And I assume you stopped/started the PowerCLI prompt in the mean time ?


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

Reply
0 Kudos
pjapk
Contributor
Contributor
Jump to solution

I hadn’t, but have now with the same results.

For completeness I tried it both from a PowerShell and a PowerCLI prompt with the same results.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

What does

Get-Culture

return ?

And is this different on both PCs ?


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

Reply
0 Kudos
pjapk
Contributor
Contributor
Jump to solution

I shall give it a try when I'm home later (it's a home-lab) although I have a lab here at work as well so will also try it there just out of interest if I can spare the time.

Will report back later...

Thanks again!

Reply
0 Kudos
pjapk
Contributor
Contributor
Jump to solution

Results are the same on both machines (at home):

[vSphere PowerCLI] C:\> get-culture

LCID             Name             DisplayName
----             ----             -----------
2057             en-GB            English (United Kingdom)

Interestingly it seems to work fine on the machine at work too! (Tried the code earlier which mostly works fine except for a few unexpecteds which I'll work through).

So, I've no idea what's different about the one I was having grief with yesterday...

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Must have been a dirty bit Smiley Wink


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

Reply
0 Kudos
pjapk
Contributor
Contributor
Jump to solution

Indeed Smiley Happy I'll see what a patch-reboot does to the box in the next couple of days.

Reply
0 Kudos