Php 连接到office365时出现拒绝访问错误

Php 连接到office365时出现拒绝访问错误,php,powershell,office365,Php,Powershell,Office365,我正在尝试使用Powershell连接到Office 365 $username = "username@domain.com" $password = "password" $secure_password = $password | ConvertTo-SecureString -AsPlainText -Force $credencial = New-Object System.Management.Automation.PSCredential ($username, $secure_p

我正在尝试使用Powershell连接到Office 365

$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);
?>

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

根据错误,Powershell脚本已经启动,因此PHP运行Powershell命令应该没有问题


必须为远程Shell启用用于连接Exchange Online的用户帐户。请查看此msdn以了解如何操作。

我同意。。。该特定用户帐户没有PSRemoting的权限

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

如果您可以从控制台运行PHP脚本,但不能通过IIS->PHP(使用相同的凭据)运行,这可能是因为通过IIS运行PHP脚本不被认为是“交互式的”,并且您使用的帐户没有显式授权

在Windows Server 2012R2上,启动PSSessions的默认权限配置要求成员身份为BUILTIN\Administrators、BUILTIN\Remote Management Users或NT AUTHORITY\INTERACTIVE

从具有本地管理员权限的PowerShell实例运行
Get-PSSessionConfiguration
,查看哪些用户已获得授权

要向IIS应用程序池使用的用户添加执行权限,请运行以下命令(也可以从具有本地管理员权限的PowerShell实例中运行):
Set PSSessionConfiguration-Name Microsoft.PowerShell-showSecurityDescriptorUI

当安全描述符UI弹出时,为运行IIS应用程序池的用户添加执行权限,单击确定


请参阅此PowerShell团队博客“您不必是管理员才能运行远程PowerShell命令”,以了解更多信息:

用户帐户具有权限。我说,它在控制台上工作,但在PHP上不工作。应该是别的。嗨,我知道这是一个旧线程,但我有同样的问题。PS脚本在控制台中运行良好,但在PHP中不起作用。始终拒绝访问。但应用程序池用户具有管理员权限,并在PSSessionConfiguration中设置。用户帐户具有权限。我说,它在控制台上工作,但在PHP上不工作。这应该是另外一种情况。@VaidasV,然后检查托管PHP的进程/服务的权限,如果它在本地系统帐户下,正如我所知,当使用CSOM API登录SharePoint online时,您将得到拒绝访问错误。我想重要的是,默认情况下不会加载用户配置文件,因此,即使您设置了执行权限,也无法识别。另外,转到应用程序池中的服务的
Advanced Settings
并将
load user profile
设置为
true
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
)