简体   繁体   中英

Trouble getting result of PowerShell command in C#

Pretty new to C# and I'm trying to write a simple tool that checks specific Roles and Features on a server and displays whether they are installed or not. Simple!

The problem is that I can't for the life of me figure out how to capture the Installed State value of this Powershell command (formatted in C# string):

"Get-WindowsFeature | ? {$_.Name -match \"Web-Mgmt-Console\"} | Select -exp Installed State"

The command itself runs in Powershell (when the \\ are removed) and just returns "false". My code tries to capture this result.

cmd = "Get-WindowsFeature | ? {$_.Name -match \""+winFeatures[i]+
                            "\"} | Select -exp Installed State";
cmdout = ps.AddScript(cmd).Invoke().ToString();

Instead of the Installed State, the value in VS of cmdout shows as "System.Collections.ObjectModel.Collection1[System.Management.Automation.PSObject]" , which, cool I guess. I understand that .Invoke() will return a collection, so the .ToString() is supposed to take the result ("True" or "False" and return it to cmdout as a string.

What am I missing here? It's amazing that Powershell can be so easy in the shell but so difficult in C# . I've been searching and reading for 2 days now and haven't been able to figure this out.

After invoking you need to try to get the output value using its variable name as below: ps.Runspace.SessionStateProxy.GetVariable("counter").

You need to check the variable name of your result.

Or else you can do like below since the result will be a collection of PSObject

 foreach (PSObject result in ps.Invoke())
    {
    MessageBox.Show(result.BaseObject.ToString() + "\n");   
    }

How about just getting the value string directly vs a collection and trying to coerce a sting in the cmd...

(Get-WindowsFeature | ? {$_.Name -match 'Web-Mgmt-Console'})

Display Name                                            Name                       Install State
------------                                            ----                       -------------
        [X] IIS Management Console                      Web-Mgmt-Console               Installed



(Get-WindowsFeature | ? {$_.Name -match 'Web-Mgmt-Console'}) | Get-Member


   TypeName: Microsoft.Windows.ServerManager.Commands.Feature

Name                      MemberType Definition                                                                                                            
----                      ---------- ----------                                                                                                            
Equals                    Method     bool Equals(System.Object obj), bool IEquatable[Feature].Equals(Microsoft.Windows.ServerManager.Commands.Feature ot...
GetHashCode               Method     int GetHashCode()                                                                                                     
GetType                   Method     type GetType()                                                                                                        
SetProperties             Method     void SetProperties(string displayName, string description, bool installed, Microsoft.Windows.ServerManager.Commands...
ToString                  Method     string ToString()                                                                                                     
AdditionalInfo            Property   hashtable AdditionalInfo {get;}                                                                                       
BestPracticesModelId      Property   string BestPracticesModelId {get;}                                                                                    
DependsOn                 Property   string[] DependsOn {get;}                                                                                             
Depth                     Property   int Depth {get;}                                                                                                      
Description               Property   string Description {get;}                                                                                             
DisplayName               Property   string DisplayName {get;}                                                                                             
EventQuery                Property   string EventQuery {get;}                                                                                              
FeatureType               Property   string FeatureType {get;}                                                                                             
Installed                 Property   bool Installed {get;}                                                                                                 
InstallState              Property   Microsoft.Windows.ServerManager.Commands.InstallState InstallState {get;}                                             
Name                      Property   string Name {get;}                                                                                                    
Notification              Property   Microsoft.Windows.ServerManager.ServerComponentManager.Internal.Notification[] Notification {get;}                    
Parent                    Property   string Parent {get;}                                                                                                  
Path                      Property   string Path {get;}                                                                                                    
PostConfigurationNeeded   Property   bool PostConfigurationNeeded {get;}                                                                                   
ServerComponentDescriptor Property   psobject ServerComponentDescriptor {get;}                                                                             
SubFeatures               Property   string[] SubFeatures {get;}                                                                                           
SystemService             Property   string[] SystemService {get;}                                                                                         



(Get-WindowsFeature | ? {$_.Name -match 'Web-Mgmt-Console'}).Installed
True

(Get-WindowsFeature | ? {$_.Name -match 'Web-Mgmt-Console'}).InstallState
Installed

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