aze
aze

Reputation: 39

Running sh script with a bat script

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 = “[email protected]” 
'$EmailTo = “[email protected]”
'$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(“[email protected]”, “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 .

Upvotes: 0

Views: 1227

Answers (1)

mklement0
mklement0

Reputation: 437638

  • 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.

Upvotes: 1

Related Questions