2017-04-03 394 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

我相信我补充说,我复制了你上面陈述的内容。 –

相关问题