2017-04-03 395 views
1

我試圖在PHP中使用PowerShell.exe -command $command,其中該命令是爲新用戶創建的。從CMD啓動Powershell返回「無法將'System.Object []'轉換爲類型'System.String'」

我注意到它沒有運行,並試圖直接從CMD啓動它(我相信這就是exec();)。

的代碼我試圖運行:

powershell.exe -command New-ADUser -SAMAccountName "jajaap" -Instance "Get_ADUser -Identity UserTemplate" -Name "Jan Jaap" -DisplayName "Jan" -Path "OU=Users-NL,OU=Users,OU=Domain-Test,DC=Domain-controller,DC=com" -GivenName "Jan" -Surname "Jaap" -userPrincipalName "[email protected]" 

我得到的錯誤是:

New-ADUser : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Path'. Specified method is not supported.

我的猜測是,我要逃跑的路徑字符串的某些字符,但我不知道如何,或不知道。或者可能是CMD中有一個converttostring類型的函數?我無法在Google上找到它。在這裏,我看到了一些關於如何修復郵件功能的例子,但我無法弄清楚如何在我的代碼中使用這些解決方案。所以請原諒,如果這個問題已經得到解答。

回答

0

我想你可能需要包裝用花括號和引號整個命令,使其一個字符串和一個腳本塊,並用符號前綴的腳本塊如下:

powershell.exe -command "& {New-ADUser -SAMAccountName 'jajaap' -Instance 'Get_ADUser -Identity UserTemplate' -Name 'Jan Jaap' -DisplayName 'Jan' -Path 'OU=Users-NL,OU=Users,OU=Domain-Test,DC=Domain-controller,DC=com' -GivenName 'Jan' -Surname 'Jaap' -userPrincipalName '[email protected]'}" 

每MSDN:

The value of Command can be "-", a string. or a script block. If the value of Command is "-", the command text is read from standard input.

Script blocks must be enclosed in braces ({}). You can specify a script block only when running PowerShell.exe in Windows PowerShell. The results of the script are returned to the parent shell as deserialized XML objects, not live objects.

If the value of Command is a string, Command must be the last parameter in the command , because any characters typed after the command are interpreted as the command arguments.

Source: https://msdn.microsoft.com/en-us/powershell/scripting/core-powershell/console/powershell.exe-command-line-help

+0

嗨,馬克,感謝您的快速幫助。我正在嘗試你上面陳述的方式,並得到這個錯誤:字符串缺少結束符:'。 + CategoryInfo:ParserError:(:) [],ParentContainsErrorRecordException + FullyQualifiedErrorId:TerminatorExpectedAtEndOfString –

+0

您確定在字符串末尾有單引號嗎? –

+0

我相信我補充說,我複製了你上面陳述的內容。 –

相關問題