適用于PowerShell3.0或者更高本版
有時,你可能會偶爾發(fā)現(xiàn)下面的代碼:
復制代碼 代碼如下:
$FilePath = "$env:SystemRoot\WindowsUpdate.log"
$ContentsWithLinebreaks = (Get-Content $FilePath) -join "`r`n"
猜猜它,想干啥子奧,Get-Content 默認將文本文件以單行讀取,并且返回一個多行數(shù)組,而-join操作符可以將它們轉換成一個單獨的字符串。而伴隨著PowerShell3.0 的低調問世,有這么一個參數(shù): -Raw,它可以非常高效的得到上面代碼同樣的結果:
復制代碼 代碼如下:
$FilePath = "$env:SystemRoot\WindowsUpdate.log"
$ContentsWithLinebreaks = (Get-Content $FilePath) -join "`r`n"
$ContentsWithLinebreaks2 = Get-Content $FilePath -Raw
$ContentsWithLinebreaks -eq $ContentsWithLinebreaks2
試著運行上面的代碼, $ContentWithLinebreaks 和$ContentWithLinebreaks2可能比較的結果略微有所不同,其不同可能只是換行符而已。
那我們繼續(xù)辨別真?zhèn)危黄淙唬?br />
復制代碼 代碼如下:
PS> $ContentsWithLinebreaks -eq $ContentsWithLinebreaks2.TrimEnd("`r`n")
True