c# - 在 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 天,但无法弄清楚这一点。
解决方案
直接获取值字符串与集合并尝试强制在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
推荐阅读
- ruby - 使用 SRT 将图像缩放到使用 imagemagick 的最小比例
- javascript - 当前滚动位置更新错误编号
- laravel - laravel 在 where 子句中有很多多态关系
- sql - 如何计算 SQL 中返回的记录数?
- r - 如何在R中找到一列的最高编号并打印该行的两列?
- python-3.x - 在张量流中,如何计算从多列中具有 NaN 的 csv 生成的批次的每一列的平均值?
- xml - 带有 RSS 的 truepush.com 推送通知图标
- css - CSS 使两个不同比例的图像能够填充容器宽度并具有相同的高度
- java - 如何使用 .flex 文件解决此语法问题?
- javascript - 如何获取标记位置(x,y)AR.js