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