我使用System.Management.Automation命名空间是为了在C# .NET程序中使用powershell。
但是,根据http://technet.microsoft.com/en-us/library/ee662309.aspx,Add-WindowsFeature返回Microsoft.Windows.ServerManager.Commands.FeatureOperationResult类型。
这个对象在MSDN上是没有文档的,但是我能够使用PowerShell和Get-Member命令检索它的成员。
PS C:\Users\Administrator> $RES[0] | Get-Member
TypeName: Microsoft.Windows.ServerManager.Commands.FeatureOperationResult
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
ExitCode Property Microsoft.Windows.ServerManager.Commands.FeatureOperationExitCode ExitCode {get;}
FeatureResult Property Microsoft.Windows.ServerManager.Commands.FeatureResult[] FeatureResult {get;}
RestartNeeded Property Microsoft.Windows.ServerManager.Commands.RestartState RestartNeeded {get;}
Success Property System.Boolean Success {get;}现在,我想在C#中使用这种类型,但是为此,我需要有具有这个命名空间的库。我只能找到
C:\Windows\winsxs\amd64_microsoft.windows.servermanager_31bf3856ad364e35_6.1.7601.17514_none_c70b231167ed6fc3Microsoft.Windows.ServerManager.dll所在的目录。这个库可以在visual中引用,但是它的名称空间不包含.Commands名称空间(?)类(?)
在C#中,$RES将使用PSObject类型作为Invoke()命令的结果
发布于 2014-08-06 14:57:17
我发现了。为了能够使用Add-WindowsFeature,您必须首先导入名为ServerManager的服务器模块。此服务器模块位于
C:\Windows\System32\WindowsPowerShell\v1.0\Modules\ServerManager目录。在同一个目录中,有包含行的ServerManager.psd1文件
NestedModules = 'Microsoft.Windows.ServerManager.PowerShell'如果您搜索Microsoft.Windows.ServerManager.PowerShell.dll库。您应该找到它的一个实例,这个实例包含我在问题中提到的.Commands命名空间。
发布于 2018-05-29 18:52:47
相关DLL的名称和位置是实现的详细信息,并且可能在不同版本之间更改。我发现在PowerShell结果中处理结构化对象的最简单方法是使用C#的“动态”特性。例如:
public bool FeatureIsInstalled(string name)
{
bool result = false;
using (Pipeline pipeline = runspace.CreatePipeline(
string.Format("Get-WindowsFeature '{0}'", name)))
{
Collection<PSObject> output = pipeline.Invoke();
if (output.Count > 0)
{
dynamic o1 = output[0];
result = (bool)o1.Installed;
}
}
return result;
}https://stackoverflow.com/questions/25163073
复制相似问题