我想通过C#使用Powershell执行Exchange 2010操作。我可以如下所示建立连接。我的问题:如何在不显式指定凭据的情况下创建连接?我不能通过当前的Windows用户身份来获得它吗?

SecureString password = new SecureString();
string str_password = "pass";
string username = "userr";
string liveIdconnectionUri = "http://exchange.cccc.com  /Powershell?serializationLevel=Full";

foreach (char x in str_password)
{
password.AppendChar(x);
}

PSCredential credential = new PSCredential(username, password);

// Set the connection Info
WSManConnectionInfo connectionInfo = new WSManConnectionInfo((new Uri(liveIdconnectionUri)), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", credential);

connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Default;

最佳答案

我不知道从安全 token 创建PSCredential对象的方法。我认为一般的工作流程是在这些情况下提示用户输入凭据,以避免与安全提升有关的漏洞。

您可能应该使用PSHostUserInterface.PromptForCredentials方法提示用户输入其凭据。

还有一些缓存凭据的示例:"PowerShell Script: Export-PSCredential and Import-PSCredential"。使用风险自负

10-08 02:31