我有下面的多线程函数,当我是唯一一个使用它的人时,这对于显式凭据很好,但我被要求将它放入一个 powershell 脚本中,它将是纯文本以利用多线程能力。
是否可以将运行脚本的帐户的隐含凭据与 wsmanconnectioninfo 一起使用?如果没有,是否有其他方法可以在没有显式凭据的情况下创建 Exchange shell 连接?
private Collection<PSObject> runPowerShellScript(object server)
{
Collection<PSObject> psobjs = new Collection<PSObject>();
string result = "";
string serverName = server.ToString();
string loginPassword = "xxx";
System.Security.SecureString secpassword = new SecureString();
foreach (char c in loginPassword)
{
secpassword.AppendChar(c);
}
PSCredential credential = new PSCredential(@"domain/samaccount", secpassword);
WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri("https://xxxxx/powershell"), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", credential);
connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;
using (Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo))
{
PowerShell powershell = PowerShell.Create();
if (runspace.RunspaceStateInfo.State == RunspaceState.Opened)
{
// do nothing
}
else
{
runspace.Open();
powershell.Runspace = runspace;
}
try
{
PSCommand command = new PSCommand();
command.AddScript($@"get-mailboxdatabase -Server " + server + " -Status");
powershell.Commands = command;
psobjs = powershell.Invoke();
if (powershell.HadErrors == true)
{
result = "Failed - " + powershell.Streams.Error[0].ToString();
result = result.Replace("\"", "*");
}
}
catch (Exception ex)
{
string fail = ex.Message;
}
}
object serverNameO = server;
PSObject serverNameObj = new PSObject(serverNameO);
psobjs.Insert(0, serverNameObj);
return psobjs;
}
最佳答案
这是有效的
WSManConnectionInfo wmc = new WSManConnectionInfo(new Uri(`"http://xxx/powershell`"));
wmc.AuthenticationMechanism = AuthenticationMechanism.Kerberos;
wmc.ShellUri = `"http://schemas.microsoft.com/powershell/Microsoft.Exchange`";
using (Runspace runspace = RunspaceFactory.CreateRunspace(wmc))
{
}
关于c# 可以将登录凭据(非显式)与 wsmanconnectioninfo 一起使用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52066794/