gpt4 book ai didi

c# - 来自 C# 的 Exchange Managed Shell

转载 作者:行者123 更新时间:2023-11-30 18:30:57 24 4
gpt4 key购买 nike

我正在尝试从客户端计算机远程运行下一个脚本:

Get-Mailbox | foreach { Get-InboxRule -Mailbox $_.Name | Remove-InboxRule }

这是我的代码:

private const string SHELL_URI = "http://schemas.microsoft.com/powershell/Microsoft.Exchange";

static public void RunPowerShellScript(string scriptfile, List<CommandParameter> cmdList, string runasUsername, string runasPassword, string serverIP)
{
// Prepare the credentials that will be used when connecting
// to the server. More info on the user to use on the notes
// below this code snippet.

System.Uri serverUri = new Uri(String.Format("http://{0}/POWERSHELL?serializationLevel=Full", serverIP));
System.Security.SecureString securePassword = new System.Security.SecureString();

foreach (char c in runasPassword.ToCharArray())
{
securePassword.AppendChar(c);
}

System.Management.Automation.PSCredential creds = new System.Management.Automation.PSCredential(runasUsername, securePassword);

RunspaceConfiguration rc = RunspaceConfiguration.Create();
WSManConnectionInfo wsManInfo = new WSManConnectionInfo(serverUri, SHELL_URI, creds);
wsManInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;
RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();

Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);
runspace.Open();

RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace);

Pipeline pipeline = runspace.CreatePipeline();

//Here's how you add a new script with arguments
Command myCommand = new Command(scriptfile, true);

if(cmdList != null)
{
foreach (var cmd in cmdList)
myCommand.Parameters.Add(cmd);
}

pipeline.Commands.Add(myCommand);

// Execute PowerShell script
var results = pipeline.Invoke();
}

结果,最后一行抛出异常:

The term 'Get-Mailbox' 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.

我知道,我在那里使用的是 PowerShell,而不是 ExchangeManagedShell。我试过像这样添加管理单元:

RunspaceConfiguration rsConfig = RunspaceConfiguration.Create();
PSSnapInException snapInException = null;
PSSnapInInfo info = rsConfig.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.Admin", out snapInException);
Runspace myRunSpace = RunspaceFactory.CreateRunspace(rsConfig);
myRunSpace.Open(rsConfig);

但客户端计算机上没有 ExchangeManagedShell。

附言我正在尝试连接到 Exchange2013。

我做错了什么?

[第 1 版]

哦,我发现了,我做错了什么:

WSManConnectionInfo wsManInfo = new WSManConnectionInfo(serverUri, SHELL_URI, creds);
wsManInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;
RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();

Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);
runspace.Open();

我什至不连接到远程计算机。创建 wsManInfo,但不要在 RunspaceFactory.CreateRunspace(); 中使用它。

但是我还是不知道怎么解决这个问题...

最佳答案

您是否检查过您使用的凭据是否有权调用“Get-Mailbox”命令?如果不是,则必须授予帐户通过 RBAC 执行命令的权限。

关于c# - 来自 C# 的 Exchange Managed Shell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20835691/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com