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?
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'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With