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
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
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
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?
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
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.
You can pipe the result to 'Format-Table -AutoSize'
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks v much