gpt4 book ai didi

c# - 在 C# 中连接到 Microsoft Exchange PowerShell

转载 作者:太空狗 更新时间:2023-10-29 17:45:49 25 4
gpt4 key购买 nike

我正在尝试从 C# .NET WinForms 应用程序连接到远程 powershell。我的目标是创建我自己的 Microsoft PowerShell ISE 版本。所以我需要一种方法来从我的应用程序在远程机器上执行 PowerShell 脚本。我已经创建了几种方法并通过我的应用程序在本地机器上对其进行了测试。如果我不使用 WSManConnectionInfo 并使用 using (Runspace remoteRunspace = RunspaceFactory.CreateRunspace()) 我可以在本地执行脚本,就好像它是真正的 powershell(小脚本,用法变量,使用 ftfl 输出数据,做很多我通常用 powershell 做的其他事情。当我添加 WSManConnectionInfo 和将它指向我的 Exchange 服务器,而不是使用本地连接。它似乎能够执行基本的东西,比如“get-mailbox”,但是一旦我尝试通过管道传输东西,使用一些脚本功能,比如 $variables,它就会中断,说它不受支持。

同样,当不在本地使用时,我必须禁用 powershell.AddCommand("out-string");

An unhandled exception of type 'System.Management.Automation.RemoteException' occurred in System.Management.Automation.dll.

Additional information: The term 'Out-String' 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.

如果我不强制远程连接而只是在本地进行,则不会出现完全相同的错误。似乎 SchemaUri 使得只执行基本命令变得非常严格。我看到其他示例,其中人们使用非常直接的信息,例如我们:

powershell.AddCommand("Get-Users");
powershell.AddParameter("ResultSize", count);

但是如果采用这种方法,我将不得不定义很多可能的选项,而且我不想通过定义参数和其他内容来完成。我只是想加载“脚本”并像在 PowerShell 窗口中一样执行它。这是我现在使用的示例。

    public static WSManConnectionInfo PowerShellConnectionInformation(string serverUrl, PSCredential psCredentials)
{
var connectionInfo = new WSManConnectionInfo(new Uri(serverUrl), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", psCredentials);
//var connectionInfo = new WSManConnectionInfo(new Uri(serverUrl), "http://schemas.microsoft.com/powershell", psCredentials);
connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;
connectionInfo.SkipCACheck = true;
connectionInfo.SkipCNCheck = true;
connectionInfo.SkipRevocationCheck = true;
connectionInfo.MaximumConnectionRedirectionCount = 5;
connectionInfo.OperationTimeout = 150000;
return connectionInfo;
}
public static PSCredential SecurePassword(string login, string password)
{
SecureString ssLoginPassword = new SecureString();
foreach (char x in password) { ssLoginPassword.AppendChar(x); }
return new PSCredential(login, ssLoginPassword);
}
public static string RunScriptPs(WSManConnectionInfo connectionInfo, string scriptText)
{
StringBuilder stringBuilder = new StringBuilder();
// Create a remote runspace using the connection information.
//using (Runspace remoteRunspace = RunspaceFactory.CreateRunspace())
using (Runspace remoteRunspace = RunspaceFactory.CreateRunspace(connectionInfo))
{
// Establish the connection by calling the Open() method to open the runspace.
// The OpenTimeout value set previously will be applied while establishing
// the connection. Establishing a remote connection involves sending and
// receiving some data, so the OperationTimeout will also play a role in this process.
remoteRunspace.Open();
// Create a PowerShell object to run commands in the remote runspace.
using (PowerShell powershell = PowerShell.Create())
{
powershell.Runspace = remoteRunspace;
powershell.AddScript(scriptText);
//powershell.AddCommand("out-string");
powershell.Commands.Commands[0].MergeMyResults(PipelineResultTypes.Error, PipelineResultTypes.Output);
Collection<PSObject> results = powershell.Invoke();

foreach (PSObject result in results) {
stringBuilder.AppendLine(result.ToString());
}

}
// Close the connection. Call the Close() method to close the remote
// runspace. The Dispose() method (called by using primitive) will call
// the Close() method if it is not already called.
remoteRunspace.Close();
}

// convert the script result into a single string
return stringBuilder.ToString();
}

关于为什么会发生这种情况的任何建议以及如何使其以相同方式运行的解决方法?我看过很多像 this 这样的博客。但是定义每个简单的命令对我来说没有意义。我还看到了创建本地连接然后执行 remote connection within that 的选项。但这必须是最后的手段,因为它依赖于多种其他因素。

最佳答案

检查 https://blogs.msdn.microsoft.com/akashb/2010/03/25/how-to-migrating-exchange-2007-powershell-managed-code-to-work-with-exchange-2010/ :

The management experience given by Exchange 2010 through PowerShellhas been moved all the way from Local to Remote. [...] Only exchange cmdlets will work in this remoting scenario, you will not be able to run most of the powershell cmdlets. [...] Yes, this does mean that you will not be able to run cmdlets like Where-Object and .PS1 scripts in the Remote Runspace.

Is that a limitation? I don’t think so. We can very easily get around it by create a new Session and Importing it.


所以你需要做像这样的事情:

PSCredential creds = new PSCredential(userName, securePassword);
System.Uri uri = new Uri("http://Exchange-Server/powershell?serializationLevel=Full");

Runspace runspace = RunspaceFactory.CreateRunspace();

PowerShell powershell = PowerShell.Create();
PSCommand command = new PSCommand();
command.AddCommand("New-PSSession");
command.AddParameter("ConfigurationName", "Microsoft.Exchange");
command.AddParameter("ConnectionUri", uri);
command.AddParameter("Credential", creds);
command.AddParameter("Authentication", "Default");
powershell.Commands = command;
runspace.Open(); powershell.Runspace = runspace;
Collection<PSSession> result = powershell.Invoke<PSSession>();

powershell = PowerShell.Create();
command = new PSCommand();
command.AddCommand("Set-Variable");
command.AddParameter("Name", "ra");
command.AddParameter("Value", result[0]);
powershell.Commands = command;
powershell.Runspace = runspace;
powershell.Invoke();

powershell = PowerShell.Create();
command = new PSCommand();
command.AddScript("Import-PSSession -Session $ra");
powershell.Commands = command;
powershell.Runspace = runspace;
powershell.Invoke();

# now you can use remote PS like it's local one

关于c# - 在 C# 中连接到 Microsoft Exchange PowerShell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36236897/

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