【问题标题】:Running powershell through c# recently getting error Access is denied通过c#运行powershell最近得到错误访问被拒绝
【发布时间】:2022-08-16 02:43:03
【问题描述】:

该计划已成功运行 4 年多。就在最近(2022 年 8 月 4 日),该程序的 pscommand 版本出现故障。我们正试图弄清楚发生了什么变化。

我们收到错误“连接到远程服务器 outlook.office365.com 失败,并显示以下错误消息:访问被拒绝。有关详细信息,请参阅 about_Remote_Troubleshooting 帮助主题。\”仅在处理 pssession / PSCommands 时。

代码:

public Collection<PSObject> runPSCommand(PSCommand _command, string _commandName, PSCommand _secondCommand = null)
        {
            PSCredential credential = new PSCredential(this.emailLogin, this.emailPass);
            WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri(this.WSManConnectionURI), this.MSSchema, credential);
            connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;
            try
            {
                using (Runspace runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(connectionInfo))
                {
                    PowerShell powershell = PowerShell.Create();
                    PSCommand remoteSigned = new PSCommand();
                    runspace.Open();
                    powershell.Runspace = runspace; 

wsmanconnectionURI:https://outlook.office365.com/PowerShell-LiveID

MSSchema:http://schemas.microsoft.com/powershell/Microsoft.Exchange

在 runspace.Open() 处失败。

我们在这个程序上运行了多种不同类型的命令(Connect-ExchangeOnline、Connect-AzureAD、Connect-MSOLService),它们都在工作,只是运行失败的 PSCommands。

也尝试使用powershell,但也失败了:

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Authentication Basic -AllowRedirection -Credential Get-Credential

出现错误:\"New-PSSession:[outlook.office365.com] 连接到远程服务器 outlook.office365.com 失败,并显示以下错误消息:访问被拒绝。 有关详细信息,请参阅 about_Remote_Troubleshooting 帮助主题。\"

同样,这已经工作了多年,只是开始失败。我们检查了密码,检查了登录名,尝试了多个用户。

感谢您提供任何帮助。

  • 这个问题超出了我的技能和经验,但我觉得有趣的是,根据this,版本 2207 于 8 月 3 日发布,而您在 8 月 4 日发现了您的问题。不幸的是,我没有看到任何引起我注意的更改,除非它发生在“已解决的问题”部分中,其中修复一个错误可能会产生另一个错误。

标签: c# powershell office365


【解决方案1】:

所以,达林引导我找到正确的答案,这可能是弃用。显然 PSSession 已被弃用,我们不得不将命令移至在线交换。

对于那些可能有帮助的人,我们的新代码是:

        public Collection<PSObject> runExchangeCommand(Command _command, string _commandName)
        {
            InitialSessionState initialSession = InitialSessionState.CreateDefault();
            initialSession.ImportPSModule(new[] { "MSOnline" });
            //initialSession.ImportPSModule(new[] { "ExchangeOnlineManagement" });
            PSCredential credential = new PSCredential(this.emailLogin, this.emailPass);
            WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri("https://outlook.office365.com/PowerShell-LiveID"), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", credential);
            connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;
            connectionInfo.MaximumConnectionRedirectionCount = 10;
            Command connectCommand = new Command("Connect-ExchangeOnline");
            connectCommand.Parameters.Add(new CommandParameter("Credential", credential));

            try
            {
                using (Runspace runspace = RunspaceFactory.CreateRunspace(initialSession))
                {
                    runspace.Open();
                    Pipeline pipe = runspace.CreatePipeline();
                    pipe.Commands.Add(connectCommand);
                    var results = pipe.Invoke();
                    var error = pipe.Error.ReadToEnd();
                    if (error.Count > 0)
                    {
                        foreach (PSObject err in error)
                        {
                            //more logging not sharing that code
                        }
                    }
                    pipe = runspace.CreatePipeline();
                    pipe.Commands.Add(_command);
                    var results2 = pipe.Invoke();
                    var error2 = pipe.Error.ReadToEnd();
                    if (error2.Count > 0)
                    {
                        foreach (PSObject er in error2)
                        {
                            //more logging, not sharing that code
                        }
                    }
                    return results2;
                }
            }

我省略了记录/捕获代码,因为其中包括一些识别内容。

至于创建命令,归结为您要运行的命令。以下是 runExchangeCommand 的一个示例:

        public bool removeCalendarInvites(string email)
        {
            Command removeMeetings = new Command("Remove-CalendarEvents");
            removeMeetings.Parameters.Add("Identity", email);
            removeMeetings.Parameters.Add("CancelOrganizedMeetings");
            removeMeetings.Parameters.Add("QueryWindowInDays", 365);
            removeMeetings.Parameters.Add("Confirm", false);
            Collection<PSObject> results = runExchangeCommand(removeMeetings, "removeCalendarEvents");

命令名称仅用于记录/识别捕获的错误。

感谢您的关注

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-09
    • 2017-09-05
    • 1970-01-01
    • 1970-01-01
    • 2017-04-06
    • 2017-09-15
    • 1970-01-01
    相关资源
    最近更新 更多