Mad*_*Boy 9 .net c# powershell exchange-server
我正在尝试从C#.NET WinForms应用程序连接到远程PowerShell.我的目标是创建自己的Microsoft PowerShell ISE版本.所以我需要一种从远程计算机上的应用程序执行PowerShell脚本的方法.我已经创建了几种方法,并在我的应用程序中在本地计算机上进行了测试.如果我不使用WSManConnectionInfo和使用使用(运行空间remoteRunspace = RunspaceFactory.CreateRunspace()) ,我可以执行脚本本地,如果它是真实的PowerShell(小脚本变量使用,使用输出数据英尺,FL,做了很多的其他的事情我通常使用PowerShell.问题开始时我添加WSManConnectionInfo并指向我的Exchange服务器,而不是使用本地连接的.现在看来,这是能够执行像基本的东西"获得的邮箱",但只要我尝试管的事,使用一些脚本功能,如$ variables它打破说它不受支持.
同样,我必须禁用powershell.AddCommand("out-string"); 什么时候不在本地使用它.
System.Management.Automation.dll中发生了未处理的"System.Management.Automation.RemoteException"类型异常.
附加信息:术语"Out-String"不被识别为cmdlet,函数,脚本文件或可操作程序的名称.检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试.
如果我不强制远程连接但只是在本地执行,则不会出现相同的错误.似乎SchemaUri只对执行基本命令非常严格.我看到了其他使用非常直接信息的人的例子:
powershell.AddCommand("Get-Users");
powershell.AddParameter("ResultSize", count);
Run Code Online (Sandbox Code Playgroud)
但是通过这种方法,我将不得不定义许多可能的选项,我不想通过定义参数和其他东西.我只是想加载"脚本"并像在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();
}
Run Code Online (Sandbox Code Playgroud)
关于为什么会发生这种情况的任何建议,并解决如何让它以相同的方式运行?我见过很多像这样的博客,但定义每个简单的命令对我来说都没有意义.我还看到了创建本地连接然后在其中执行远程连接的选项,但这必须是最后的手段,因为它依赖于多个其他因素.
Exchange 2010通过PowerShell提供的管理体验已从Local更改为Remote.[...]只有交换cmdlet才能在此远程处理方案中运行,您将无法运行大多数PowerShell cmdlet.[...]是的,这确实意味着您将无法在远程运行空间中运行Where-Object和.PS1脚本等cmdlet.
这是一个限制吗?我不这么认为.我们可以通过创建一个新的Session并导入它来轻松解决它.
所以你需要做这样的事情:
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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5905 次 |
| 最近记录: |