Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用C#32位和Powershell安装Windows服务器角色/功能_C#_Powershell - Fatal编程技术网

使用C#32位和Powershell安装Windows服务器角色/功能

使用C#32位和Powershell安装Windows服务器角色/功能,c#,powershell,C#,Powershell,我试图通过执行powershell命令,使用c#安装Windows服务器角色。这是我的密码: InitialSessionState iss = InitialSessionState.CreateDefault(); iss.ImportPSModule(new string[] { "ServerManager" }); Runspace PowerShellRunspace = RunspaceFactory.CreateRunspace(iss); PowerShellRunspace.

我试图通过执行powershell命令,使用c#安装Windows服务器角色。这是我的密码:

InitialSessionState iss = InitialSessionState.CreateDefault();
iss.ImportPSModule(new string[] { "ServerManager" });
Runspace PowerShellRunspace = RunspaceFactory.CreateRunspace(iss);
PowerShellRunspace.Open();
using (PowerShell powershell = PowerShell.Create())
{
 powershell.Runspace = PowerShellRunspace;
 powershell.AddCommand("Install-WindowsFeature");
 powershell.AddArgument("XPS-Viewer");
 powershell.Invoke();
 }
 PowerShellRunspace.Close();
但我得到一个错误,tearm“install windowsfeature”未被识别为cmdlet的名称,函数


请告诉我如何安装XPS查看器。我正在使用.NET 2.0 32位并在Windows Server 2016操作系统上执行它。

您有什么Powershell版本?另外,为什么不在服务器2016操作系统上默认安装.NET 4.5?Powershell 5。此代码需要在服务器2008 R2上执行,而.NET 4.5将不存在。在服务器2008 R2上,请使用
Add WindowsFeature
或使用
dism.exe
,该命令适用于所有位置。在Windows Server 2008 R2上,cmdlet的名称为old。尝试在代码中使用此选项。
安装WindowsFeature
在2012年R*和2016年运行良好,但在2008年R2中使用
添加WindowsFeature