2014-02-18 46 views
0
連接到MSOL服務

我試圖使用PowerShell錯誤的嘗試與PHP和PowerShell

$username = "[email protected]" 
$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 

和PHP連接到MSOL服務:

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

從本地PC PowerShell腳本運行不任何錯誤,但與PHP它會拋出一個錯誤:

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] => 
) 

回答

1

作爲例外說明:您必須調用th e調用任何其他cmdlet之前,Connect-MsolService cmdlet。

+0

但在PowerShell腳本存在命令連接,MsolService。此異常由另一個命令「Get-MsolGroup」引發。 「Connect-MsolService」只拋出未知的異常,沒有任何詳細信息,出現了什麼問題:引發了Microsoft.Online.Administration.Automation.MicrosoftOnlineException類型的異常。 – VaidasV

+0

@VaidasV是在同一個powershell會話中執行的這兩個命令嗎? – Matt

+0

這些命令在相同的powershell腳本中執行,所以我認爲在同一個會話中。 – VaidasV

1

首先,以純文本形式在腳本中存儲密碼是一個糟糕的主意。如果您的目標是爲Azure Active Directory中的某些操作提供Web界面,則最好的方法是使用專爲此設計的Azure Active Directory Graph API。如果你仍然覺得你有這個有效的場景,你已經分析和權衡了你正在做的事情的風險,並且絕對確定你想要這個,然後繼續閱讀......

你有什麼應該工作,我能夠使用您已有的相同腳本,並在瀏覽器中成功查看通訊組的詳細信息。有幾件事情,你可以嘗試:

  • 我懷疑這甚至遠程關係到你的問題,但如果你使用單引號,你不應該逃避你的路徑:

    $command = 'powershell -File "' . dirname(__DIR__) . '\ps\run.ps1"'; 
    
  • 確保您正在執行正確版本的PowerShell(32位與64位)。作爲一個全面的檢查,嘗試運行:

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

    如果你是一個64位系統上,看到一個8那麼你很可能好。如果您看到4,那麼您的服務器(例如Apache或IIS)或PHP不是64位版本時可能會出現問題。 (這是我得到的,一旦我安裝了一個運行在64位上的服務器堆棧,一切都運行良好。在此之前,我嘗試加載MSOnline庫時出現錯誤。)

  • 三重檢查腳本你有你正在執行的腳本(即確保你沒有在不同位置的副本上工作)。一個用於調試的選項是打印出腳本本身。嘗試添加此行到PHP文件:

    echo '<pre>' 
        . htmlentities(file_get_contents(dirname(__DIR__) . '\ps\run.ps1')) 
        . '</pre>';