Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pipe output from command in Git Bash to Powershell script on windows

I am on windows in a git bash hook script and want to pipe output from a git command to a powershell script, but can not get to work in bash what otherwise works in a windows command shell:

The command in question is the following:

dir | powershell.exe -NoProfile -NonInteractive -Command "$Input | send_mail.ps1"

Here is the content of send_mail.ps1:

[CmdletBinding()]
Param
(
[Parameter(ValueFromPipeline)]
[string[]]$Lines
)
BEGIN
{
  $AllLines = ""
}
PROCESS
{
  foreach ($line in $Lines)
  {
    $AllLines += $line
    $AllLines += "`r`n"
  }
}
END
{
  $EmailFrom = "[email protected]"
  $EmailTo = "[email protected]"

  $Subject = "Message from GIT"
  $Body = $AllLines

  $SMTPServer = "smtp.sample.com"
  $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
  $SMTPClient.EnableSsl = $true
  $SMTPClient.Credentials = New-Object System.Net.NetworkCredential("[email protected]", "asdf1234");
  $SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)
}

If the command is run from powershell or windows command line i successfully receive an email with the contents of the directory.

If the command is run from bash the following error is displayed:

In Zeile:1 Zeichen:2
+  | C:\Temp\Test\send_mail.ps1
+  ~
Ein leeres Pipeelement ist nicht zulässig.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : EmptyPipeElement

This translates roughly to "An empty pipe element is not allowed" and means $Input is empty here. Does anyone have a working example for bash?

like image 521
Daniel Birler Avatar asked Dec 07 '25 07:12

Daniel Birler


1 Answers

Bash is probably expanding the $Input in double quotes to an empty string, because Input probably isn't defined as a variable in Bash, so PowerShell gets the command | send_mail.ps1. One way to fix it is to use single quotes instead of double quotes:

dir | powershell.exe -NoProfile -NonInteractive -Command '$Input | send_mail.ps1'
like image 107
pjh Avatar answered Dec 08 '25 23:12

pjh