运行在C#中有错误的PowerShell命令,"不能调用这个函数,因为目前的宿主,并不能实现它与QUOT; [英] Running a powershell command in C# errors with, "Cannot invoke this function because the current host does not implement it"
问题描述
我有,我用它来运行Exchange的Powershell里面C#code命令,如下面的应用程序。这是相关的线I用于运行powershell命令的一个例子。
I have an application that I use to run Exchange Powershell commands inside C# code like below. This is an example of the relevant lines I use to run the powershell command.
RunspaceConfiguration rsConfig = RunspaceConfiguration.Create();
PSSnapInException snapInException = null;
//load Exchange shell
rsConfig.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.E2010", out snapInException);
Runspace runSpace = RunspaceFactory.CreateRunspace(rsConfig);
//open runspace
runSpace.Open();
//setup pipeline
Pipeline pipeLine = runSpace.CreatePipeline();
String sScript = "get-mailbox -identity 'rj'";
//add script to pipeline
pipeLine.Commands.AddScript(sScript);
//run the script
pipeLine.Invoke();
pipeLine.Dispose();
这code可以完美运行在所有的情况下,一直到现在。脚本我试图运行,而不是上面那个是设置的RetentionPolicy邮箱。我试图运行该脚本是这样的:
This code works perfect in all cases until now. the script I am trying to run instead of the one above is to set the RetentionPolicy for a mailbox. The script I am trying to run looks like this:
设置邮箱-IdentityRJ-RetentionPolicy'主校区保留策略
Set-Mailbox -Identity 'rj' -RetentionPolicy 'Main Campus Retention Policy'
当我运行这个在PowerShell中它本身完美的作品,但是当我尝试使用code以下我得到的错误来运行它,不能调用此函数,因为目前的主机没有实现它。
When I run this in powershell itself it works perfectly but when I try to run it using the code below I get the error, "Cannot invoke this function because the current host does not implement it."
从这个错误,它似乎像一个运行在C#中不能运行的RetentionPolicy命令的命令,但是这并没有多大意义。我用Google搜索这一点,并尝试了所有建议,但没有运气。
From this error, it almost seems like the command that runs in C# cannot run the RetentionPolicy command but that doesn't make much sense. I have Googled this and tried everything suggested but no luck.
如果有人知道为什么会这样,那将是非常有益的。
If anyone knows why this is happening, it would be very helpful.
推荐答案
如果该命令通常会提示进行确认,那么你将需要:
If that command would normally prompt for confirmation then you will need to either:
- 设置
-Confirm:$假
作为一个参数(也可能是-Force
为好) - 设置
$确认preference =无
调用设置邮箱(也可能是之前-Force
太) - 创建一个主机和落实确认功能; - )
- Set
-Confirm:$false
as a parameter (and possibly-Force
as well) - Set
$ConfirmPreference = "None"
before calling Set-Mailbox (and possibly-Force
too) - Create a Host and implement the Confirm functionality ;-)
这篇关于运行在C#中有错误的PowerShell命令,"不能调用这个函数,因为目前的宿主,并不能实现它与QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!