VMware Cloud Community
eaphilipp
Contributor
Contributor

For Each Question

What am I missing?

$vCenter ='vCenter01'

Connect-VIServer $vCenter

$S = get-content "c:\lockdown.txt"

foreach ($S in $S ) {

(get-vmhost $S | get-view).EnterLockdownMode()

}

Disconnect-VIServer -Server $vCenter -Confirm:$false 

Ugh, this seems so basic!

Thanks for the help.

Reply
0 Kudos
7 Replies
LucD
Leadership
Leadership

Your foreach loop variable and the full collection have the same name, $S

$vCenter ='vCenter01'

Connect-VIServer $vCenter

$S = get-content "c:\lockdown.txt"

foreach ($row in $S ) {

    (get-vmhost $row | get-view).EnterLockdownMode()

}

Disconnect-VIServer -Server $vCenter -Confirm:$false


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

msuvanto_MNIT
Enthusiast
Enthusiast

Made the change:

$vCenter = 'vCenter01'

Connect-VIServer $vCenter

$S = get-content "c:\lockdown.txt"

foreach ($row in $S) {

(get-vmhost $row | get-view).EnterLockdownMode()

}

now getting this error for each host:

get-vmhost : 10/16/2019 12:50:30 PM Get-VMHost VMHost with name 'ESXihostname.domain ' was not found using the specified filter(s). At line:5 char:2 + (get-vmhost $row | get-view).EnterLockdownMode() # To ENABLE Lockdown ... +  ~~~~~~~~~~~~~~~     + CategoryInfo          : ObjectNotFound: (:) [Get-VMHost], VimException     + FullyQualifiedErrorId : Core_OutputHelper_WriteNotFoundError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetVMHost You cannot call a method on a null-valued expression. At line:5 char:1 + (get-vmhost $row | get-view).EnterLockdownMode() # To ENABLE Lockdown ...

Reply
0 Kudos
LucD
Leadership
Leadership

That is most probably due to what is the .txt file.

Are these the names of the ESXi hosts, one per line?

The error message seems to say that there is a line with ESXihostname.domain on a line


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

LucD
Leadership
Leadership

Btw, are you the same person (Erik) who started this thread?

Confusing


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

Reply
0 Kudos
msuvanto_MNIT
Enthusiast
Enthusiast

The file is a straight up textfile with FQDM hostnames one on each row.

No headers or any extra "."

Nope..  not Eric. Eric and myself are co-workers and I initially asked him for assistance on this "should be simple" script.

Reply
0 Kudos
LucD
Leadership
Leadership

Ok, then the ESXi nodes are not connected to the VCSA with their FQDN.
I suspect they might be connected by their IP address.

What do you get when you do a Get-VMHost (after connecting to the VCSA)?


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

Reply
0 Kudos
msuvanto_MNIT
Enthusiast
Enthusiast

When I do a get-vmhost I get a straight up listing of my hosts in the expected format:

We did make it work and the tip about the row was the key info here.

The lockdown.txt file with the hostnames had a extra whitespace after each hostname, that broke it.

Making the change:

foreach ($row in $S) {

(get-vmhost $row.trim() | get-view)

and cleaning up the txt file made it work.

Reply
0 Kudos