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.
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.
$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
}
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