VMware Cloud Community
Beansley
Contributor
Contributor
Jump to solution

Adding a loop

Hello,

I've got yet another issue with creating a loop around some simple code - always seems to get me.  If I use the code found here http://communities.vmware.com/message/1850265#1850265  at a pCLI prompt, everything works as advertised.  Then I try to structure a loop around it and it blows up.  Here's my script:

get-vmhost -location "cluster name" | foreach-object {
$esxcli = Get-EsxCli -VMHost $_
$esxcli.system.coredump.network.set($null,"vmk0","x.x.x.x",6500)
$esxcli.system.coredump.network.set(1)
$esxcli.system.coredump.network.get()

}

The problem seems to be my use of $_ (the code is not making a substitution for the $_), but I've no idea how to get around it.

thanks

Dave

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Ok, one step further.

Try this

get-vmhost -location "cluster" | foreach-object {
    $esxcli = Get-EsxCli -VMHost $_
   
$esxcli.system.coredump.network.get() }

Which PowerCLI version are you using ?

Do a

Get-PowerCLIVersion

Before PowerCLI 5.x you had to do a Connect-VIServer to the ESX(i) host for the Get-EsxCli cmdlet.


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

View solution in original post

0 Kudos
8 Replies
LucD
Leadership
Leadership
Jump to solution

Are you sure that

get-vmhost -location "cluster name"

is returning objects ?

The $_ should give you the VMHost object in the foreach code block.


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

0 Kudos
Beansley
Contributor
Contributor
Jump to solution

Are you sure that

      get-vmhost -location "cluster name"

is returning objects ?

Yes.  I set up a variable at a prompt like such:

$a = get-vmhost -location "cluster name"

then gave the prompt

$a

and it listed hosts in the cluster.  I went down the path of referencing $_.name (vs. just $_) and passed that through the loop - didn't help (probably made matters worse, actually ):smileywink: .

Then I performed

$a | get-member just to be sure it was returning what appears to be hosts.  Sure looks like it is.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I tried your code in several variations...and it just works for me :smileyconfused:

From where are you running the code ? From the PowerCLI prompt ? Or a Gui ?

Can you try this

get-vmhost -location "cluster name" | foreach-object {
    $_.GetType().Name
}

and see if it returns the VMHost object name ?

To make sure there is nothing going wrong while copying the code, I attached the same lines as a file.


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

0 Kudos
Beansley
Contributor
Contributor
Jump to solution

Can you try this

get-vmhost -location "cluster name" | foreach-object {

    $_.GetType().Name

}


and see if it returns the VMHost object name ?

It lists them as

VMHostImpl
VMHostImpl

which I suppose is to be expected (2 entries to reflect the 2 hosts in my test cluster).

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Ok, one step further.

Try this

get-vmhost -location "cluster" | foreach-object {
    $esxcli = Get-EsxCli -VMHost $_
   
$esxcli.system.coredump.network.get() }

Which PowerCLI version are you using ?

Do a

Get-PowerCLIVersion

Before PowerCLI 5.x you had to do a Connect-VIServer to the ESX(i) host for the Get-EsxCli cmdlet.


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

0 Kudos
Beansley
Contributor
Contributor
Jump to solution

PowerCLI 5.0 build 435427

I typically make the server connection before I run code manually, which is the case here.

You provided code errors in the same manner my script does:

"Cannot complete the operation due to an incorrect request to the server
At script.ps1:9 char 25
+    $esxcli = Get-EscCli <<<< -VMhost $_

<<additional text omitted>> "

Thanks for looking at this.

Dave

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You must be using a vCenter 5.x and running the script against ESX(i) 4.x hosts ?

No, I'm not clairvoyant Smiley Wink

I suspect you are encountering a known issue that is documented in the CLI 5 release notes.


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

0 Kudos
Beansley
Contributor
Contributor
Jump to solution

You nailed it.  I've just about completed the upgrade to 5, with the exception of my legacy test cluster, which will be decommed shortly.  My script runs great on a real cluster.

I'd say that's a good gotcha to look out for if you're in a mixed environment.

Thanks for your time, LucD!

0 Kudos