云端转发邮箱

云端转发邮箱,即office365方式转发,特别注意连接方法和脚本

            RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();
            Runspace runspace1 = RunspaceFactory.CreateRunspace(runspaceConfiguration);
            try
            {
              
                runspace1.Open();
        
                ParameterObjectCloud paramObject = new ParameterObjectCloud() { powerUser = "admin@onmicrosoft.com"};
                runspace1.SessionStateProxy.SetVariable("arg", paramObject);

                Pipeline pipeline = runspace1.CreatePipeline();
                Command command = null;

                command = new Command("$powerPass = '密码'", true);
                pipeline.Commands.Add(command);
                command = new Command("$arg.Password = ConvertTo-SecureString $powerPass -AsPlainText -Force", true);
                pipeline.Commands.Add(command);
                command = new Command("$arg.Credential = New-Object -TypeName System.Management.Automation.PSCredential($arg.powerUser,$arg.Password)", true);
                pipeline.Commands.Add(command);
                command = new Command("$s =New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri 'https://ps.outlook.com/powershell' -Credential $arg.Credential -Authentication Basic -AllowRedirection", true);
                pipeline.Commands.Add(command);
                command = new Command("Import-PSSession $s", true);
                pipeline.Commands.Add(command);
                command = new Command("Set-Mailbox -Identity '"+adUser.UserPrincipalName+"' -DeliverToMailboxAndForward $true -ForwardingSMTPAddress '"+adUser.ForwardingOneMailbox+"'", true);
                pipeline.Commands.Add(command);
                var results = pipeline.Invoke();
                result.ExecResult = ExecResult.Success;
                runspace1.Dispose();
                return result;
原文地址:https://www.cnblogs.com/shiningleo007/p/6743621.html