使用PowerShell cmdlet使用加密密码上的密钥连接到C#中的Exchange Online

时间:2019-08-07 13:28:31

标签: c# powershell

我想从C#在线访问Exchange,例如Web应用程序,以提供管理灵活性。通常,我使用下面的powershell脚本。

//the key and password was changed

IF ($session.state -ne 'Opened') {
    # encryption key
    $key = (3,4,12,3,56,34,211,22,1,1,22,23,42,54,33,233,81,34,2,27,116,5,35,43)

    $adminUser = "admin@domain.onmicrosoft.com" 
    $adminPwd = "76492d1974683f0423413b12570a5345MgB8AGgAZgB0AE4AeABlAG4AbgB5AHYATABwAE4AbwB5AGgAUABtAHoAbwBYAEEAPQA9AHhn208edA8nE973VsDji2wAMwA3AGMAYQBkADgAMgA2ADYAZABkADIAMQA1AGEAMQBiAGQAOQAQBiADEANAAyAGEANAA=" | ConvertTo-SecureString -Key $Key
    $psCred = New-Object System.Management.Automation.PsCredential $adminUser,$adminPwd 

    $O365Url = "https://outlook.office365.com/powershell-liveid/" 

    $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri $O365Url -Credential $psCred -Authentication basic -AllowRedirection 

    Import-PSSession $Session -AllowClobber
}
Else {
    Write-Host 'Opened session'

}

// do something

Remove-PSSession $Session -AllowClobber

现在,我尝试在C#中执行相同的操作。密码为明文形式时,我可以连接。我不知道如何包括我的加密密钥。这里的代码:

byte[] key = {3,4,12,3,56,34,211,22,1,1,22,23,42,54,33,233,81,34,2,27,116,5,35,43};

string adminUser = "admin@domain.onmicrosoft.com" 
string adminPwd = "76492d1974683f0423413b12570a5345MgB8AGgAZgB0AE4AeABlAG4AbgB5AHYATABwAE4AbwB5AGgAUABtAHoAbwBYAEEAPQA9AHhn208edA8nE973VsDji2wAMwA3AGMAYQBkADgAMgA2ADYAZABkADIAMQA1AGEAMQBiAGQAOQAQBiADEANAAyAGEANAA=";
string O365Url = "https://outlook.office365.com/powershell-liveid/";

SecureString adminPwdSecure = new SecureString();
PSObject SessionHolder = null;

foreach (char c in adminPwd.ToCharArray())
    adminPwdSecure.AppendChar(c);

adminPwdSecure.MakeReadOnly();

PSCredential credential = new PSCredential(adminUser, adminPwdSecure);

Runspace runspace = RunspaceFactory.CreateRunspace();
PowerShell powershell = PowerShell.Create();

PSCommand command = new PSCommand();
command.AddCommand("New-PSSession");
command.AddParameter("ConfigurationName", "Microsoft.Exchange");
command.AddParameter("ConnectionUri", new Uri(O365Url));
command.AddParameter("Credential", credential);
command.AddParameter("Authentication", "Basic");
powershell.Commands = command;

runspace.Open();
powershell.Runspace = runspace;
Collection<PSObject> result = powershell.Invoke();
if (powershell.Streams.Error.Count > 0 || result.Count != 1)
{
    throw new Exception("Fail to establish the connection");
}
else
{
    //Success to establish the connection
    SessionHolder = result[0];
}

PSCommand ImportSession = new PSCommand();
ImportSession.AddCommand("Import-PSSession");
ImportSession.AddParameter("Session", SessionHolder);
powershell.Commands = ImportSession;
powershell.Invoke();

// do something

PSCommand RemoveSession = new PSCommand();
RemoveSession.AddCommand("Remove-PSSession");
RemoveSession.AddParameter("Session", SessionHolder);
powershell.Commands = RemoveSession;
powershell.Invoke();

1 个答案:

答案 0 :(得分:0)

您可以尝试如下创建加密密钥:

    byte[] key = { 3, 4, 12, 3, 56, 34, 211, 22, 1, 1, 22, 23, 42, 54, 33, 233, 81, 34, 2, 27, 116, 5, 35, 43 };

    string adminUser = "admin@domain.onmicrosoft.com";
    string adminPwd = "76492d1974683f0423413b12570a5345MgB8AGgAZgB0AE4AeABlAG4AbgB5AHYATABwAE4AbwB5AGgAUABtAHoAbwBYAEEAPQA9AHhn208edA8nE973VsDji2wAMwA3AGMAYQBkADgAMgA2ADYAZABkADIAMQA1AGEAMQBiAGQAOQAQBiADEANAAyAGEANAA=";
    string O365Url = "https://outlook.office365.com/powershell-liveid/";

    PSObject SessionHolder = null;

    Runspace runspace = RunspaceFactory.CreateRunspace();
    PowerShell powershell = PowerShell.Create();

    runspace.Open();
    powershell.Runspace = runspace;

    // >>> Create $adminPwdSecure encrypted variable using PowerShell:
    powershell.AddCommand("ConvertTo-SecureString")
        .AddParameter("String", adminPwd)
        .AddParameter("Key", key)
        .AddCommand("New-Variable")
        .AddParameter("Name", "adminPwdSecure")
        .Invoke();

    // >>> Read $adminPwdSecure variable from session state:
PSCredential credential = new PSCredential(adminUser, (SecureString)((PSObject)pwsh.Runspace.SessionStateProxy.PSVariable.GetValue("adminPwdSecure")).BaseObject);

    PSCommand command = new PSCommand();
    command.AddCommand("New-PSSession");
    command.AddParameter("ConfigurationName", "Microsoft.Exchange");
    command.AddParameter("ConnectionUri", new Uri(O365Url));
    command.AddParameter("Credential", credential);
    command.AddParameter("Authentication", "Basic");
    powershell.Commands = command;

    Collection<PSObject> result = powershell.Invoke();
    if (powershell.Streams.Error.Count > 0 || result.Count != 1)
    {
        throw new Exception("Fail to establish the connection");
    }
    else
    {
        //Success to establish the connection
        SessionHolder = result[0];
    }

    PSCommand ImportSession = new PSCommand();
    ImportSession.AddCommand("Import-PSSession");
    ImportSession.AddParameter("Session", SessionHolder);
    powershell.Commands = ImportSession;
    powershell.Invoke();

    // do something

    PSCommand RemoveSession = new PSCommand();
    RemoveSession.AddCommand("Remove-PSSession");
    RemoveSession.AddParameter("Session", SessionHolder);
    powershell.Commands = RemoveSession;
    powershell.Invoke();