【问题标题】:Trouble getting result of PowerShell command in C#在 C# 中无法获取 PowerShell 命令的结果
【发布时间】:2019-07-16 03:12:40
【问题描述】:

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

问题是我一辈子都不知道如何捕获这个 Powershell 命令的 Installed State 值(以 C# 字符串格式):

"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 中的值显示为 "System.Collections.ObjectModel.Collection1[System.Management.Automation.PSObject]",而不是已安装状态,我猜这很酷。我知道 .Invoke() 将返回一个集合,因此 .ToString() 应该获取结果(“True”或“False”并将其作为字符串返回给 cmdout。

我在这里缺少什么?令人惊奇的是,Powershell 在 shell 中可以如此简单,但在 C# 中却如此困难。我已经搜索和阅读了 2 天,但无法弄清楚这一点。

【问题讨论】:

  • 以为我已经弄明白了,但没有。删除此评论。

标签: c# powershell


【解决方案1】:

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

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

否则你可以像下面那样做,因为结果将是 PSObject 的集合

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

【讨论】:

    【解决方案2】:

    如何直接获取值字符串而不是集合并尝试在 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-18
      • 1970-01-01
      • 2017-05-14
      • 2016-03-21
      • 1970-01-01
      相关资源
      最近更新 更多