使用php exec运行powershell脚本时出现问题


Problems running powershell script with php exec

我有问题运行有效的powershell脚本(从cmd测试)。我想要的脚本,以测试用户是否存在于office 365,但身份验证只是不会通过php,而它在cmd执行正常。

服务器是windows 2008 R2, IIS 7.5, PHP 5.4 NTS.

我是一个普通的linux用户,我在让MS工作的时候遇到了麻烦,所以任何建议都是非常感谢的。

这是我的powershell脚本:
$username = "adminuser@my_domain"
$password = "password"
$secstr = New-Object -TypeName System.Security.SecureString
$password.ToCharArray() | ForEach-Object {$secstr.AppendChar($_)}
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $secstr
Import-Module MSOnline -force
Connect-Msolservice -Credential $cred
Get-MsolUser -UserPrincipalName student@my_domain

和PHP部分:

exec("powershell -Command C:/inetpub/wwwroot/office365/test.ps1 < NUL", $output);
echo '<pre>';
print_r ($output);
echo '</pre>';

输出结果:

Array
(
    [0] => Connect-MsolService : Exception of type 'Microsoft.Online.Administration.Automa
    [1] => tion.MicrosoftOnlineException' was thrown.
    [2] => At C:'inetpub'wwwroot'office365'test.ps1:8 char:20
    [3] => + Connect-Msolservice <<<<  -Credential $cred
    [4] =>     + CategoryInfo          : OperationStopped: (:) [Connect-MsolService], Mic
    [5] =>    rosoftOnlineException
    [6] =>     + FullyQualifiedErrorId : 0x80070002,Microsoft.Online.Administration.Autom
    [7] =>    ation.ConnectMsolService
    [8] => 
)

我找到了修复方法。

试题:

从源文件

复制名为MSOnlineMSOnline Extended的文件夹

C: ' Windows ' System32系统模块' WindowsPowerShell ' v1.0 ' '

到文件夹

C: ' Windows ' SysWOW64 ' WindowsPowerShell ' v1.0 '模块'

然后在PS中运行Import-Module MSOnline,它会自动获得模块:D