1

Powershell has Start-Process cmdlet (start a new process) which can be a new powershell and get it to run whatever e.g. via -Command & {scriptblock} .

Limitation on/after ‘-Command ‘ of 32,760 chars ( due to underlying UNICODE_STRING usage?) . However you can supply Start-Process with a -Credential ( to run under ), in which circumstance the limitation on/after the ‘-Command’ appears to drop to 1024.

Can anyone help me by explaining why this is ? If it should be and if it's documented anywhere ?

( And without using fixed other script, or passing of arguments to said code via a file, how best to overcome ? )

Code to repro:-


$username = "Domain\User" 
$password = "************"
$useCred = New-Object System.Management.Automation.PSCredential -ArgumentList @($username,(ConvertTo-SecureString -String $password -AsPlainText -Force))

$Nof = 32000
$spaces = (" "*$Nof)
write-host -NoNewLine "test with OUT Credential, command at least $Nof in length"
try { Start-Process Powershell -NoNewWindow -ArgumentList "-Command & $spaces{}"  ; write-host " - OK"} 
catch {write-host " - FAIL"; throw $_ }

$Nof = 800
$spaces = (" "*$Nof)
write-host -NoNewLine "test WITH Credential, command at least $Nof in length"
try { Start-Process Powershell -NoNewWindow -Credential $useCred -ArgumentList "-Command & $spaces{}" ; write-host " - OK"} 
catch {write-host " - FAIL"; throw $_ }

$Nof = 1000
$spaces = (" "*$Nof)
write-host -NoNewLine "test WITH Credential, command at least $Nof in length"
try { Start-Process Powershell -NoNewWindow -Credential $useCred -ArgumentList "-Command & $spaces{}" ; write-host -NoNewLine " - OK"} 
catch {write-host " - FAIL"; throw $_ }

Result:

test with OUT Credential, command at least 32000 in length - OK
test WITH Credential, command at least 800 in length - OK
test WITH Credential, command at least 1000 in length - FAIL
Start-Process : This command cannot be run due to the error: The parameter is incorrect.
At ......\test_F.ps1:22 char:7
+ try { Start-Process Powershell -NoNewWindow -Credential $useCred -Arg ...
+       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Start-Process], InvalidOperationException
    + FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand
2
  • The error is the parameter is incorrect. The length doesn't have anything to do with the error. Why are you using curly brackets after $spaces? Where is the argument list? Commented Jul 31, 2024 at 15:50
  • The code is just a repro in reality there'd be actual code within the {scriptblock} and possibly arguments/parameter provision for it after. The $spaces could probably be put later in the code but for demo purposes it shows the issue there. The 3 test cases showing the difference between having no -Credential and having some -Credential, then that it works with a longish (800) line but not when go over 1000 (1024 presumed). Commented Aug 1, 2024 at 19:01

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.