首页 > 解决方案 > 在 C# 中无法获取 PowerShell 命令的结果

问题描述

对 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

解决方案


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

推荐阅读