CMD执行PowerShell加域命令提示参数缺失,PS直接执行正常求排查
Hey there! Let's break down why you're hitting that error when running your PowerShell command through CMD, and how to fix it.
The Root Cause
When you run a PowerShell command directly in its own prompt, it parses the syntax natively. But CMD has totally different rules for handling quotes, parentheses, and special characters—your original command doesn't account for these differences, so CMD is splitting up the parameter list incorrectly before passing it to PowerShell. Also, your original snippet was truncated: you need the -Force parameter for ConvertTo-SecureString when using -AsPlainText (PowerShell requires this to confirm you're aware of the security risk of plaintext passwords).
The Corrected Commands
Here are two working versions tailored for CMD:
Option 1: Use Single Quotes Inside the PowerShell Command
This avoids quote escaping headaches, since CMD treats single quotes as literal characters:
powershell.exe -Command "Add-Computer -DomainName mydomain -Credential (New-Object System.Management.Automation.PSCredential('mydomain\admin', (ConvertTo-SecureString 'W/GdGax+1CebYQ74' -AsPlainText -Force)))"
Option 2: Escape Double Quotes for CMD
If you prefer sticking with double quotes in your PowerShell command, escape them with `" (backtick + quote) so CMD doesn't interpret them as the end of the command string:
powershell.exe -Command "Add-Computer -DomainName mydomain -Credential (New-Object System.Management.Automation.PSCredential(`"mydomain\admin`", (ConvertTo-SecureString `"W/GdGax+1CebYQ74`" -AsPlainText -Force)))"
Why This Works
By wrapping the entire PowerShell command in double quotes, you tell CMD to pass everything inside as a single, intact argument to powershell.exe -Command. Fixing the quote handling (either switching to single quotes or escaping doubles) ensures PowerShell receives the full, correctly formatted command instead of a broken, truncated version.
内容的提问来源于stack exchange,提问作者Tom McMeekin

