Powershell Get-Content到Messagebox保留换行符

时间:2018-04-20 07:57:35

标签: powershell newline messagebox

我正在使用powershell向用户显示消息框。 消息框的标题和文本是从文本文件中提取的:

Add-Type -AssemblyName PresentationCore,PresentationFramework
$ButtonType = [System.Windows.MessageBoxButton]::OK
$MessageIcon = [System.Windows.MessageBoxImage]::Information
$MessageBody = Get-Content -Path "$PSScriptRoot\text_body.txt"
$MessageTitle = Get-Content -Path "$PSScriptRoot\text_title.txt"
$MessageBody
$Result = [System.Windows.MessageBox]::Show($MessageBody,$MessageTitle,$ButtonType,$MessageIcon)

text_body.txt中的文本包含CariageReturns / LineFeeds。当我将变量$ MessageBody输出到控制台时,我看到了CR / LF。当我查看消息框时,所有CR / LF都消失了。所有文字都在一条大行中。

如果我将所有文本声明到powershell脚本中的变量并将该变量解析为消息框,则保留我的CR / LF ......

当我使用get-content命令并将文本解析为消息框时,如何保留CR / LF?

1 个答案:

答案 0 :(得分:0)

我设法通过将文本文件解析为" Out-String"来保留新行/ CR / LF。命令。我的脚本现在看起来像这样:

Add-Type -AssemblyName PresentationCore,PresentationFramework
$ButtonType = [System.Windows.MessageBoxButton]::OK
$MessageIcon = [System.Windows.MessageBoxImage]::Information
$MessageBody = Get-Content -Path "$PSScriptRoot\text_body.txt" | Out-String
$MessageTitle = Get-Content -Path "$PSScriptRoot\text_title.txt"
$Result = [System.Windows.MessageBox]::Show($MessageBody,$MessageTitle,$ButtonType,$MessageIcon)