简体   繁体   中英

running powershell command from c# webservice

I am running a powershell command from c#, which I call through a webservice

This is the command Set-MailboxAutoReplyConfiguration -identity

Here is my code.

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

The user I pass through the 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. 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 . Then you call AddParameter on the command to add the parameter eg:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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