php - 连接到Office365时访问被拒绝错误

标签 php powershell office365

我正在尝试使用Powershell连接到Office365。

$username = "username@domain.com"
$password = "password"
$secure_password = $password | ConvertTo-SecureString -AsPlainText -Force
$credencial = New-Object System.Management.Automation.PSCredential ($username, $secure_password)

$session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "https://ps.outlook.com/powershell/" -Credential $credencial -Authentication Basic -AllowRedirection -ErrorAction Stop -WarningAction SilentlyContinue
Import-PSSession $session -AllowClobber | Out-Null

问题是,在本地PC和服务器上的Powershell控制台上,它运行正常,没有任何错误,但是当我尝试使用PHP运行它时:
<?php
    $command = 'powershell -File "'.dirname(__DIR__).'\\ps\\test.ps1"';

    exec($command, $output);
    print_r($output);
?>

我收到错误消息,提示“访问被拒绝”:
Array
(
    [0] => New-PSSession : [pod51047psh.outlook.com] Connecting to remote server pod51047p
    [1] => sh.outlook.com failed with the following error message : Access is denied. For
    [2] => more information, see the about_Remote_Troubleshooting Help topic.
    [3] => At C:\inetpub\xxxxxx\ps\test.ps1:6 char:12
    [4] => + $session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri
    [5] =>  "h ...
    [6] => + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    [7] => ~~~
    [8] =>     + CategoryInfo          : OpenError: (System.Manageme....RemoteRunspace:Re
    [9] =>    moteRunspace) [New-PSSession], PSRemotingTransportException
    [10] =>     + FullyQualifiedErrorId : AccessDenied,PSSessionOpenFailed
)

有什么建议我做错了吗?还有其他方法可以用PHP运行此Powershell脚本吗?

最佳答案

我只是在与O365的另一种脚本语言(PowerShell)类似的情况下遇到了这种情况。我假设您正在IIS下的应用程序池中运行PHP。如果不是,请替换您的Web服务器按以下方式运行的用户。

如果可以从控制台运行PHP脚本,但不能通过IIS-> PHP(具有相同的凭据)运行PHP脚本,则可能是因为通过IIS运行该脚本不被视为“交互式”,并且您使用的帐户未得到明确授权。

在Windows Server 2012R2上,用于启动PSSession的默认权限配置需要BUILTIN \ Administrators,BUILTIN \ Remote Management Users或NT AUTHORITY \ INTERACTIVE中的成员身份。

从具有本地管理员权限的PowerShell实例中运行Get-PSSessionConfiguration,以查看已被授权的用户。

要向IIS应用程序池使用的用户添加执行权限,请运行以下命令(也从具有本地管理员特权的PowerShell实例中):Set-PSSessionConfiguration -Name Microsoft.PowerShell -showSecurityDescriptorUI
当安全描述符UI弹出时,为运行IIS App Pool的用户添加“执行”权限,然后单击“确定”。

有关更多信息,请参见以下PowerShell团队博客“您不必成为管理员即可运行远程PowerShell命令”:https://blogs.msdn.microsoft.com/powershell/2009/11/22/you-dont-have-to-be-an-administrator-to-run-remote-powershell-commands/

关于php - 连接到Office365时访问被拒绝错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21607362/

相关文章:

php - PDO MySQL备份功能

powershell - Powershell管道中的错误处理

Excel公式: If duplicate found in column B,在D列中添加 "Y"值

与过滤器连接的 Sharepoint 日历 ListView Web 部件

php - Blade 模板和单选按钮 - 在 foreach 循环中选择第一个

php - 如何用数据库中的数据填充 session 数组?

powershell - 无法从Powershell控制台运行Powershell 'runspace'

excel - Office 365 中 Excel 中 Power Pivot 的计算列中正确的 DAX GROUPBY 语法是什么

php - 如何对这段代码的结果进行排序?

Powershell:无法从远程计算机获取网络打印机列表并将这些打印机安装在新计算机上