Matt Leyland
Matt Leyland

Reputation: 2329

Format Powershell Command

To Start my command

Get-Clustergroup | ? { $_.groupType -eq "VirtualMachine" }

which returns

Name            OwnerNode    State
----            ---------    -----

TESTBED         NODE1        Online  
MACHINE01       NODE1        Online

So I run the following command to return only values and strip white space and empty lines.

Get-Clustergroup | ? { $_.groupType -eq "VirtualMachine" } | foreach { $_Name }

Which returns the machine names as expected,

TESTBED
MACHINE01

I have an issue because I also want to pull out the Status (State) of that machine. I have tried.

Get-Clustergroup | ? { $_.groupType -eq "VirtualMachine" } | foreach { $_Name,$_State }

but this does not return the status also just the name still. I wanted to be able to retrieve something like.

TESTBED,Online
MACHINE01,Online

Upvotes: 0

Views: 257

Answers (2)

oɔɯǝɹ
oɔɯǝɹ

Reputation: 7625

You can use

Get-ClusterGroup |
    Where-Object  { $_.groupType -eq "VirtualMachine" } |
    Select-Object Name, State

To query the properties you need and return them as objects to the command line, which will then be displayed as:

Name            State
----            -----
TESTBED         Online  
MACHINE01       Online

Or, you can use

Get-ClusterGroup |
    Where-Object   { $_.groupType -eq 'VirtualMachine' } |
    ForEach-Object { '{0},{1}' -f $_.Name, $_.State }

To return the string values exactly as you requested:

TESTBED,Online  
MACHINE01,Online

Upvotes: 1

mjolinor
mjolinor

Reputation: 68341

You can use a format string:

$timeframe = (get-date).AddMinutes(-80)
$log | ? { $_.failures -like '*Denied*' -or $_.failures -like '*error*'  -and [datetime]$_.'date-time' -ge $timeframe }

Get-Clustergroup | ? { $_.groupType -eq "VirtualMachine" } | foreach { '{0},{1}' -f $_.Name,$_.State }

Upvotes: 1

Related Questions