【发布时间】:2019-01-02 08:14:14
【问题描述】:
我一直在尝试使用 C# 从我的 .Net 应用程序创建与 PowerShell 的连接。当我尝试创建会话时连接完成后,它返回空集合。
string shellUri = "http://schemas.microsoft.com/powershell/Microsoft.PowerShell";
PSCredential remoteCredential = new PSCredential("userID", StringToSecureString("Password"));
WSManConnectionInfo connectionInfo = new WSManConnectionInfo(false, "Ip Address of server", 5985, "/wsman", shellUri, remoteCredential, 1 * 60 * 1000);
runspace = RunspaceFactory.CreateRunspace(connectionInfo);
connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;
runspace.Open();
using (PowerShell ps = PowerShell.Create())
{
ps.Runspace = runspace;
ps.AddScript(@"$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://servername/poweshell -Credential " + remoteCredential);
//result count returned is 0
var result = ps.Invoke();
ps.Commands.Clear();
ps.AddCommand("Import-PSSession $Session");
ps.Invoke();
}
【问题讨论】:
-
您希望添加
string和PSCredential会产生什么结果?
标签: c# powershell