简体   繁体   中英

How to get the output of a powershell command into an array

I am trying to get the output of a powershell command into an array, but it seems not to work. In fact I want to address the output with a row and a column index. eg

$a=Get-Service

With output (part)

Status   Name               DisplayName                           
------   ----               -----------                           
Stopped  AeLookupSvc        Application Experience                
Stopped  ALG                Application Layer Gateway Service     
Stopped  AppIDSvc           Application Identity                  
Running  Appinfo            Application Information               
Stopped  AppMgmt            Application Management    

I want to address for the second line the DisplayName, eg

$a[2][2]

And it should give then

Application Layer Gateway Service

But this does not seem to work.

Can anybody help?

This type of question makes me think that you're probably coming from a Unix background, and are accustomed to having to deal with indicies and column index, that sort of thing.

Fundamentally, PowerShell is an object-oriented scripting language. You simply don't need to do what you're asking about here.

For instance, if you want to capture the results, then grab a property for one of the objects, here's how that's done.

First, capture the output.

$a=Get-Service

Now, you want a particular property of a particular entity. To get that, index into the object you want.

>$a[2]
Status   Name               DisplayName                           
------   ----               -----------                           
Stopped  AJRouter           AllJoyn Router Service                

To select the .DisplayName , all you have to do is append that to the end of your previous command.

> $a[2].DisplayName
AllJoyn Router Service

If you want to select multiple values, you could use this approach instead.

#Select multiple values from one entity
$a[2] | select DisplayName, Status

>DisplayName                        Status
-----------                        ------
Application Layer Gateway Service Stopped

#Select multiple values from each in the array
$a | select DisplayName, Status

>DisplayName                                               Status
-----------                                               ------
Adobe Acrobat Update Service                             Running
AllJoyn Router Service                                   Stopped
Application Layer Gateway Service                        Stopped
Application Identity                                     Stopped

This is not possible without a mapping from property names to array indices. Note that what you see in the output is just a partial list of properties (defined in an XML file somewhere). So there isn't even an easy way to convert those to array indices.

However, I also don't quite understand your need here. You can get the second service with $a[1] , as expected. And then you can get its DisplayName property value with $a[1].DisplayName . PowerShell uses objects throughout. There is simply no need to fall back to text parsing or cryptic column indices just to get your data. There's an easier way.

The output from Get-Service that you see in the Console may look like an array (as it is formatted as a table when sent to the Console), but it is actually an 'System.ServiceProcess.ServiceController' object.

Rather than using row and column designations, you need to use the name of the property to retrieve it, so for your example:

$a[2].DisplayName will return Application Layer Gateway Service

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM