简体   繁体   English

从c#webservice运行powershell命令

[英]running powershell command from c# webservice

I am running a powershell command from c#, which I call through a webservice 我正在运行来自c#的powershell命令,我通过web服务调用它

This is the command Set-MailboxAutoReplyConfiguration -identity 这是命令Set-MailboxAutoReplyConfiguration -identity

Here is my code. 这是我的代码。

string command = "Set-MailboxAutoReplyConfiguration -identity " + user;

The user I pass through the webservice. 我通过webservice的用户。

However when I run it I get this error. 但是,当我运行它时,我收到此错误。

System.Management.Automation.RemoteException: The term 'Set-MailboxAutoReplyConfiguration -identity babbey' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

I'm not sure where the &#39 is coming from before and after my command. 我不知道&#39来自我命令之前和之后的位置。 However, if I take out the +user part it works fine. 但是,如果我取出+用户部分,它可以正常工作。 So my problem is the variable that is in the command variable. 所以我的问题是命令变量中的变量。

If you use PowerShell.AddCommand , you only specify the command name eg Set-MailboxAutoReplyConfiguration . 如果使用PowerShell.AddCommand ,则只需指定命令名称,例如Set-MailboxAutoReplyConfiguration Then you call AddParameter on the command to add the parameter eg: 然后在命令上调用AddParameter来添加参数,例如:

var ps = PowerShell.Create();
ps.AddCommand("Set-MailboxAutoReplyConfiguration");
ps.AddParameter("Identity", user);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM