问题
I'm trying to pop up a simple message box using Powershell's -EncodedCommand flag, but it keeps failing. I've tried Googling for the last few hours, but can't seem to get this working. It almost looks like an encoding error, but I'm using regular UTF-8 with standard ASCII backwards-compatible characters.
The command that keeps failing:
Powershell.exe -EncodedCommand QWRkLVR5cGUgLUFzc2VtYmx5TmFtZSBQcmVzZW50YXRpb25Db3JlLFByZXNlbnRhdGlvbkZyYW1ld29yaztbU3lzdGVtLldpbmRvd3MuTWVzc2FnZUJveF06OlNob3coJ3dvcmtpbmcnKTs=
The b64 decoded command is:
Add-Type -AssemblyName PresentationCore,PresentationFramework;[System.Windows.MessageBox]::Show('working');
What am I missing? Thanks for helping with my noob question
回答1:
The Base64-encoded string passed to -EncodedCommand
must encode the byte representation of a UTF-16LE ("Unicode") string - UTF-8 is not supported:
$cmd = 'Add-Type -AssemblyName PresentationCore,PresentationFramework;[System.Windows.MessageBox]::Show(''working'')'
$encodedCmd = [Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes($cmd))
powershell.exe -EncodedCommand $encodedCmd
来源:https://stackoverflow.com/questions/50225489/powershell-encodedcommand-failing