0

I am using a script to send emails on my machine , the script is a sh script and i need to execute automatically that script via another batch script , the problem is when i copy past the code on my Powershell command directly the code works fine the email will be sent , but then when I try to use a script to run it it always fail : This is my code for email :

$EmailFrom = “whatever@gmail.com” 
'$EmailTo = “whatever@gmail.com”
'$Subject = “helooo2o”
$Body = “test test test”
$SMTPServer = “smtp.gmail.com”
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential(“whatever@gmail.com”, “pwd”);
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)

it works fine when i put it directly on CMD then Powershell it works but when i use this script it won't work :

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe  -windowstyle hidden -command 'C:\Users\user1\Desktop\test.sh'

also when I use PowerShell with the command : Bash test.sh it shows me this :

bash /test.sh: cannot execute binary file

Can you guys help me i need to run automatically this script with a batch script thank you in advance .

Ed Morton
  • 188,023
  • 17
  • 78
  • 185
aze
  • 39
  • 9
  • 2
    What you have above has nothing to do with BASH or SH. Is there any other reason you have included those two tags? The syntax between powershell and Linux bash/sh is completely different. Bash and sh work with ASCII text in at most UTF-8 encoding. I suspect your file is in UTF-16. – David C. Rankin Jun 13 '21 at 13:50
  • hello thank you for ur reply , but i just checked with my notepad ++ in the buttom it says UTF-8 – aze Jun 13 '21 at 13:59
  • 1
    The extension of your file should be `.ps1`. This doesn't have something to do with bash. – Santiago Squarzon Jun 13 '21 at 14:01
  • If UTF-8, that is UTF-8 with BOM (byte order mark). see [Byte order mark](https://en.wikipedia.org/wiki/Byte_order_mark) -- this will also prevent bash and sh from interpreting the file. If you plan on editing files that can be used with bash/sh, you need to check the editor save settings and disable the BOM. You will also want to change the line-endings from DOS (`'\r\n'`) to Linux (`'\n'`). Powershell can read Linux line-endings, but bash and sh can't read DOS line endings. On Linux, you can use the `dos2unix` utility to convert the line endings automatically for you. – David C. Rankin Jun 13 '21 at 14:01
  • i am confused can my code be .sh and .ps1 or what should it be at first ? it work fine when i copy past on powershell so it's .ps1 right should i change it ? – aze Jun 13 '21 at 14:14
  • 1
    @aze The code you gave above is not a shell script code regardless of the file format. Please try to research the basics. – konsolebox Jun 13 '21 at 14:26
  • 2
    The code you're showing in the question is PowerShell code not bash. The extension for a PowerShell script is `.ps1`. In addition you have 2 variables defined wrong `'$EmailTo` and `'$Subject`. Remove the `'` at the beginning of those variables. – Santiago Squarzon Jun 13 '21 at 14:29
  • Agree with Santiago, the *commenting* out of code is a *hashtag* (`#`) in powershell – Abraham Zinala Jun 13 '21 at 14:33
  • i am sorry it was my mistake when i pasted here the code , the two ' are not included in my code – aze Jun 13 '21 at 14:45
  • i changed the extension to .ps1 and executed the file with ./ test1.ps1 an error occured here At character C: \ Users \ IBrahimBoubaker \ Desktop \ test1.ps1: 8: 96 + ... t System.Net.NetworkCredential (â € œdatastage.pvgtc@gmail.comâ €, â € rgœqvwjp ... + ~ Missing argument in the parameter list. + CategoryInfo: ParserError: (:) [], ParseException + FullyQualifiedErrorId: MissingArgument – aze Jun 13 '21 at 14:56

1 Answers1

1
  • Save your PowerShell code in a file with extension .ps1, not .sh. This ensures that PowerShell knows that the file is a PowerShell script (see below).

  • When you save the file, use character encoding UTF-8 with a BOM to ensure that the Windows PowerShell CLI, powershell.exe, correctly interprets your script file with respect to non-ASCII-range characters in the file, notably the quotation marks used in your code, (LEFT DOUBLE QUOTATION MARK, U+201C)

    • Using such typographic quotation marks - as opposed to the usual, ASCII-range quotation marks (" (QUOTATION MARK, U+0022) and ' (APOSTROPHE, U+0027), used as a single quote) works fine in PowerShell, but only if the PowerShell engine recognizes the script file's actual character encoding; see this answer for more information.

Background information:

  • You're running on Windows, where it is purely a script file's file-name extension - such as .sh - that determines what interpreter will execute it.

    • Using extension .sh is a(n ill-advised) convention in the Unix world indicating that a given file is a shell script designed to be interpreted by a POSIX-compatible shell, such as /bin/sh; however, unless the file is marked as executable via its file permissions and specifies the actual interpreter to use via a shebang line, you cannot invoke such files directly: you need to pass them to the target interpreter's executable as an argument.

    • On Windows, it is Git Bash, for instance, that registers extension .sh as to be executed with bash, which is why your attempt to invoke a .sh file resulted in a bash error message.

  • Additionally, from inside PowerShell, .ps1 files are run in-process, analogous to how batch files (.cmd, .bat) are run in-process from cmd.exe.

mklement0
  • 382,024
  • 64
  • 607
  • 775