[英]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. 对于C#来说还很陌生,我正在尝试编写一个简单的工具来检查服务器上的特定角色和功能,并显示是否已安装。 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): 问题是我一生都无法弄清楚如何捕获此Powershell命令(以C#字符串格式)的Installed State值:
"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". 该命令本身在Powershell中运行(删除\\时),并且仅返回“ 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. 代替“安装状态”,cmdout的VS中的值显示为
"System.Collections.ObjectModel.Collection1[System.Management.Automation.PSObject]"
,这很酷。 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. 我知道.Invoke()将返回一个集合,因此
.ToString()
应该采用结果(“ True”或“ False”,并将其作为字符串返回给cmdout)。
What am I missing here? 我在这里想念什么? It's amazing that Powershell can be so easy in the shell but so difficult in
C#
. 令人惊奇的是,Powershell在shell中如此简单,而在
C#
却如此困难。 I've been searching and reading for 2 days now and haven't been able to figure this out. 我已经搜寻和阅读2天了,还没弄清楚。
After invoking you need to try to get the output value using its variable name as below: ps.Runspace.SessionStateProxy.GetVariable("counter"). 调用后,您需要尝试使用其变量名来获取输出值,如下所示: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 否则您可以像下面这样进行操作,因为结果将是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... 只是直接获取值字符串与集合并尝试强制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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.