VMware Cloud Community
1an3
Enthusiast
Enthusiast
Jump to solution

one-liner to run a command on each host in $cluster

Hello

I'm getting to grips with PowerCLI and piping and the like.

I have a one-liner to enable SSH on each host in a cluster.

Get-Cluster | Get-VMHost | foreach {Start-VMHostService -HostService ($_ | Get-VMHostService | Where {$_.Key -eq "TSM-SSH"})}

I want one which will run a similar command - I want to retrieve the last line of vpxa.log

I have a command per-host:

get-vmhost <hostFQDN>  |get-log vpxa |select -expand Entries |select -Last 1

I want to run that get-log command on each host, in a one liner like the 'enable ssh' one.

I have tried:

Get-Cluster | Get-VMHost | foreach {get-log vpxa |select -expand Entries |select -Last 1}

but I get output:

get-log : 15/02/2017 11:38:21    Get-Log        Log with key 'vpxa' not found.

At line:1 char:37

+ Get-Cluster | Get-VMHost | foreach {get-log vpxa |select -expand Entries |select ...

+                                     ~~~~~~~~~~~~

    + CategoryInfo          : ObjectNotFound: (:) [Get-Log], VimException

    + FullyQualifiedErrorId : Core_GetLog_GetLogKeys_Key_Not_Found,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetLog

Can anyone with more powershell knowledge than me help?

Thanks

Ian

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Like this?

Get-Cluster | Get-VMHost | %{Get-Log -Key vpxa -VMHost $_  | select -ExpandProperty Entries | select -Last 1}

 


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

View solution in original post

Reply
0 Kudos
6 Replies
LucD
Leadership
Leadership
Jump to solution

Like this?

Get-Cluster | Get-VMHost | %{Get-Log -Key vpxa -VMHost $_  | select -ExpandProperty Entries | select -Last 1}

 


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

Reply
0 Kudos
1an3
Enthusiast
Enthusiast
Jump to solution

Ace. Thanks.

So the % is the output from the get-cluster |get-host, and that $_ is where the % is placed?

Next step...

Is it possible in a one-liner to have <hostname> either on preceding line, or at the start of the line, to identify which line belongs to which host?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Not exactly.

The % is an alias for the ForEach-Object cmdlet.

The $_ is the variable that contains the object that comes from the pipeline, and inside a ForEach-Object codeblock it represents the current object.

On the next stage, all the info is in the object returned by the Get-Log cmdlet.

With some calculated properties we can extract that.

Note1, in an array, the element indexed with -1, is the last element in the array.

Noet2, in fact we don't need the ForEach-Object loop, the Get-Log cmdlet accepts the VMHost over the pipeline

Get-Cluster | Get-VMHost | Get-Log -Key vpxa |

Select @{N='VMHost';E={$_.Host}},@{N='Log';E={$_.Entries[-1]}}


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

1an3
Enthusiast
Enthusiast
Jump to solution

I think I might love you.

To help me confirm this...

How do I remove the vast amount of blank space between the VMHost column and the Log column.

Smiley Wink

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You can pipe the result to 'Format-Table -AutoSize'


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

Reply
0 Kudos
1an3
Enthusiast
Enthusiast
Jump to solution

Thanks v much Smiley Happy

Reply
0 Kudos