1

I'm currently working on a script which will read a mail from a certain sender, who will be sending some commands for the scripts to run.

The Main Idea:

Sender sends a mail with a command for example: ipconfig /all > result.txt and the script running at the recipients side copies this command to a .bat file and running the .bat file to process the command.

The Code:

$junk = $routlook.GetDefaultFolder(23)
$MI = $junk.items

foreach($m in $MI)
{
    if($m.SenderEmailAddress -eq '<sender-address>')
    {
        Echo "@ECHO OFF" > com.bat
        Echo $MI.item(1).Body > com.bat
        .\com.bat
    }
    break
}

The Error:

enter image description here

Tom Kustermans
  • 521
  • 2
  • 8
  • 31
  • 1
    I've posted a solution, but I trust you are aware of the major security risk you are running?! Email addresses can be trivially spoofed (as can anything else you might use to authenticate), so I can't see any scenario where this would be an appropriate thing to do. What are you actually trying to achieve? – RB. Feb 24 '16 at 13:10

1 Answers1

5

Your problem is that you are not specifying the encoding of your output file.

The easiest way to fix this is to use the Out-File cmdlet and specify the encoding yourself. Note that the 2nd and subsequent calls to Out-File must specify the -Append parameter or you will overwrite your file.

$> "@ECHO OFF" | Out-File -FilePath cmd.bat -Encoding ascii
$> "Echo hi"   | Out-File -FilePath cmd.bat -Encoding ascii -Append
$> .\cmd.bat
RB.
  • 36,301
  • 12
  • 91
  • 131