This powershell code works fine:
powershell -NoProfile -Command {Start-Process -FilePath wscript.exe -Verb RunAs -ArgumentList '"C:\Users\TestAccount\Desktop\cartella con spazi\test.vbs" "/CurrentDirectory:C:\Users\TestAccount\Desktop\Cartella con spazi" "/AppData:C:\Users\TestAccount\AppData\Roaming"'}
But when I enter the complete command, powershell leaves only one space between the words but in my path there are two consecutive ones:
Start-Process -FilePath powershell.exe -WorkingDirectory "$env:ALLUSERSPROFILE" -Credential $credential -WindowStyle Hidden -ArgumentList "-NoProfile -Command & {Start-Process -FilePath wscript.exe -Verb RunAs -ArgumentList '\`"C:\Users\TestAccount\Desktop\cartella con spazi\test.vbs\`" \`"/CurrentDirectory:C:\Users\TestAccount\Desktop\Cartella con spazi\`" \`"/AppData:C:\Users\TestAccount\AppData\Roaming\`"'}"
Same error with this:
Start-Process -FilePath powershell.exe -WorkingDirectory "$env:ALLUSERSPROFILE" -Credential $credential -WindowStyle Hidden -ArgumentList "-NoProfile -Command Start-Process -FilePath wscript.exe -Verb RunAs -ArgumentList '\`"C:\Users\TestAccount\Desktop\cartella con spazi\test.vbs\`" \`"/CurrentDirectory:C:\Users\TestAccount\Desktop\Cartella con spazi\`" \`"/AppData:C:\Users\TestAccount\AppData\Roaming\`"'"
This is the error message:
As you can see, in the error message the path contains only one space between words, while in the code I have correctly entered two consecutive ones.
This is the code to enter the credentials (just before the line that gives an error):
$username = 'Username'
$password = 'Password'
$securePassword = ConvertTo-SecureString $password -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential $username, $securePassword
I've been getting a single line of code to work properly for days but without success.
Those example paths I entered are actually variables (with an indefinite number of consecutive spaces) and therefore by enclosing -ArgumentList in single quotes, the variables are not expanded.
You can adopt the strategy you think is most appropriate to help me.
Windows 10 Pro 64-bit
Powershell Version: 5.1.19041.1237 (Integrated in Windows 10).
UPDATE:
@mklement0 There is the problem that in reality the line in question contains an expandable variable:
Start-Process -FilePath powershell.exe -WorkingDirectory "$env:ALLUSERSPROFILE" -Credential $credential -WindowStyle Hidden -ArgumentList @"
-NoProfile -Command "Start-Process -FilePath wscript.exe -Verb RunAs -ArgumentList '\"$PWD\test.vbs\" \"/CurrentDirectory:$PWD\" \"/AppData:$env:APPDATA\"'"
"@
so if the path contains single quotes, the line goes in error.
UPDATE 2:
@mklement0 Your solution has stopped working since I put it in a script, which is what I need, with the following error message: