Error on trying to connect to MSOL service with PHP and Powershell

0

I'm trying to connect to Msol Service with powershell

$username = "username@domain.com"
$password = "password"

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

Import-Module MSOnline
Connect-MsolService -Credential $credencial

Get-MSolGroup -GroupType DistributionList -SearchString "groupname" | Select DisplayName, EmailAddress, ObjectId | Out-String

And PHP:

$command = 'powershell -File "'.dirname(__DIR__).'\\ps\\run.ps1"';
exec($command, $output);
print_r($output);

From local PC powershell script runs without any errors, but with PHP it throws an error:

Array
(
    [0] => Exception of type 'Microsoft.Online.Administration.Automation.MicrosoftOnlineEx
    [1] => ception' was thrown.
    [2] =>     + CategoryInfo          : OperationStopped: (:) [Connect-MsolService], Mic
    [3] =>    rosoftOnlineException
    [4] =>     + FullyQualifiedErrorId : 0x80090345,Microsoft.Online.Administration.Autom
    [5] =>    ation.ConnectMsolService
    [6] =>     + PSComputerName        : s021
    [7] => 
    [8] => You must call the Connect-MsolService cmdlet before calling any other cmdlets.
    [9] =>     + CategoryInfo          : OperationStopped: (:) [Get-MsolGroup], Microsoft
    [10] =>    OnlineException
    [11] =>     + FullyQualifiedErrorId : Microsoft.Online.Administration.Automation.Micro
    [12] =>    softOnlineException,Microsoft.Online.Administration.Automation.GetGroup
    [13] =>     + PSComputerName        : s021
    [14] => 
    [15] => 
)
php
powershell
office365
asked on Stack Overflow Feb 18, 2014 by VaidasV

2 Answers

1

As the exception suggests: You must call the Connect-MsolService cmdlet before calling any other cmdlets.

answered on Stack Overflow Feb 18, 2014 by Matt
1

First of all, storing a password in a script in plain text is a terrible idea. If your goal is to provide a web interface to certain operations in Azure Active Directory, your best bet is to use the Azure Active Directory Graph API, which was designed for this. If you still feel you have a valid scenario for this, you have analyzed and weighed the risks of what you're doing, and are absolutely sure you want this, then continue reading....

What you have should work, I was able to use the same script you have and successfully see the details of the distribution group in the browser. A few things that you can try:

  • I doubt this is even remotely related to your problem, but if you're using single quotes, you shouldn't be escaping your path:

    $command = 'powershell -File "' . dirname(__DIR__) . '\ps\run.ps1"';
    
  • Make sure you're executing the correct version of PowerShell (32-bit vs. 64-bit). As a sanity check, try running:

    <?php echo shell_exec('powershell -Command "[intptr]::size"'); ?>
    

    If you're on a 64-bit system and see an 8 then you're probably good. If you see a 4, then you might have issues where your server (e.g. Apache or IIS) or PHP aren't the 64-bit versions. (This is what got me. Once I installed a server stack that was running on 64 bits, everything worked beautifully. Before that, I got errors when trying to load the MSOnline library.)

  • Triple-check that the script you have is the script that you're executing (i.e. make sure you aren't working on a copy that is in a different location). One option to debug this is to print out the script itself. Try adding this line to your PHP file:

    echo '<pre>'
        . htmlentities(file_get_contents(dirname(__DIR__) . '\ps\run.ps1')) 
        . '</pre>';
    
answered on Stack Overflow Feb 26, 2014 by Philippe Signoret

User contributions licensed under CC BY-SA 3.0