For my tests I am using ‘Start > Run’ dialog (not the cmd.exe).
This works fine, and I get 9 in log.txt
powershell -Command 4+5 > c:\log.txt
But this does not work:
powershell -EncodedCommand IAA1ACsANwAgAA== > c:\log.txt
So how can I redirect output in this case?
Experimental code:
function Test { $cmd = { 5+7 } $encodedCommand = EncodeCommand $cmd StartProcess 'powershell -Command $cmd > c:\log.txt' StartProcess 'powershell -EncodedCommand $encodedCommand > c:\log2.txt' } function StartProcess($commandLine) { Invoke-WMIMethod -path win32_process -name create -argumentList $commandLine } function EncodeCommand($expression) { $commandBytes = [System.Text.Encoding]::Unicode.GetBytes($expression) [Convert]::ToBase64String($commandBytes) }
The ‘Run’ dialog doesn’t seem like it provides redirection at all. According to this usenet post you only get redirection if you have a console. I wonder if the redirection parameter is being parsed by powershell.exe, which is choosing to redirect if it’s not receiving encoded input? Sounds like a question for Raymond Chen.
Anyway, this works, at the expense of spawning an otherwise useless console: