Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/353.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#应用程序运行powershell命令_C#_Powershell - Fatal编程技术网

从C#应用程序运行powershell命令

从C#应用程序运行powershell命令,c#,powershell,C#,Powershell,我想从我的C#应用程序运行以下powershell命令: Enter-PSSession –ComputerName fedsrv01.domain.local Start-ADSyncSyncCycle -PolicyType Delta 我找到了一些关于Powershell类的信息,但由于缺乏经验,我很难实现我想要的目标 这就是我到目前为止所做的: 我已经添加了程序集并引用了system.management.automation using (var powershell = Pow

我想从我的C#应用程序运行以下powershell命令:

Enter-PSSession –ComputerName fedsrv01.domain.local

Start-ADSyncSyncCycle -PolicyType Delta
我找到了一些关于Powershell类的信息,但由于缺乏经验,我很难实现我想要的目标

这就是我到目前为止所做的:

我已经添加了程序集并引用了system.management.automation

using (var powershell = PowerShell.Create())
{

     //powershell.AddCommand("get-process");
     powershell.AddCommand("Enter-PSSession -ComputerName fedsrv01.domain.local");


     powershell.Invoke();
}
我收到一条错误消息,
“Enter PSSession-ComputerName fedsrv01.domain.local”一词不能识别为cmdlet、函数、脚本文件或可操作程序的名称。

如果我使用:
powershell.AddCommand(“获取进程”)
它执行得很好

如果我在同一台PC上启动Powershell并输入,
输入PSSession-ComputerName fedsrv01.domain.local
它工作正常

任何帮助都将不胜感激

干杯


Jono

尝试将应用程序编译为x64。如果它编译为x86平台,那么它将使用虚拟化System32目录,因此您需要的功能可能不存在


好的,在对PowerShell类进行更多研究之后,我现在意识到必须使用.addparameter方法单独添加参数。 .addcommand仅适用于PowerShell命令。现在可以理解为什么我得到了一个错误,说找不到命令。它假设整个字符串是一个命令。 问题解决了


乔诺

这回答了你的问题吗?