2017-03-08 35 views
1

我是PowerShell的新手。我創建其中包含一些數據的變量,我再傳給到電子郵件生成:PowerShell - 管道 - 硬編碼程序vs變量

$strEmailFile = "C:\Testing\SomeTextFile.txt" 

cat $strEmailFile | E:\sendmail.exe -t 

我的問題是我不想硬編碼的電子郵件生成軟件的驅動器。所以,我想的代碼是這樣的:

$strEmailFile = "C:\Testing\SomeTextFile.txt" 

$Dir = "E:" 

cat $strEmailFile | $Dir + "\sendmail.exe" -t 

但我不斷收到以下錯誤:

"Expressions are only allowed as the first element of a pipeline."

有我使用的管道,但沒有硬辦法碼?

+0

當你有'Send-MailMessage'時,你不需要'sendmail.exe'與PowerShell。 –

回答

1

你應該看看Send-MailMessage,但你的嘗試之間的區別是,第一個是表達式,第二個是一個字符串,你不能管道到一個字符串。

但是如果你使用的call operator &你可以管到的是:

cat $strEmailFile | & ($Dir + "\sendmail.exe") -t 

請注意,您需要括號先運行地連接了變量和字符串)的表達。