无法在C#中获取PowerShell命令的结果

时间:2019-02-22 04:27:42

标签: c# powershell

对于C#来说还很陌生,我正在尝试编写一个简单的工具来检查服务器上的特定角色和功能,并显示它们是否已安装。简单!

问题是我一生都无法弄清楚如何捕获此Powershell命令(以C#字符串格式)的Installed State值:

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

命令本身在Powershell中运行(删除\时),并且仅返回“ false”。我的代码尝试捕获此结果。

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

cmdout的VS值显示为{{1},而不是Installed State,这很酷。我知道.Invoke()将返回一个集合,因此"System.Collections.ObjectModel.Collection1[System.Management.Automation.PSObject]"应该接受结果(“ True”或“ False”,然后将其作为字符串返回给cmdout。

我在这里想念什么?令人惊讶的是,Powershell在shell中如此容易,而在.ToString()中却如此困难。我已经搜索和阅读了2天了,还没弄清楚。

2 个答案:

答案 0 :(得分:0)

调用后,您需要尝试使用其变量名称来获取输出值,如下所示: ps.Runspace.SessionStateProxy.GetVariable(“ counter”)。

您需要检查结果的变量名。

否则您可以按照下面的步骤进行操作,因为结果将是PSObject的集合

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

答案 1 :(得分:0)

直接获取值字符串和集合并尝试强制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