get-command | where-object { $_.commandtype -eq "cmdlet" } | sort-object -property name | select-object -property name | where-object { $_.name -match "^get" } | out-file "getcommands.txt"
$content = get-content "getcommands.txt"
$content | Foreach-Object { $_.TrimEnd() } | where { $_ -match "\w" } | Out-File "getcommands.txt" -encoding Ascii
compare-object -referenceobject $(Get-Content "oldcommands.txt") -differenceobject $(Get-Content "getcommands.txt") -includeequal
該代碼檢索所有以「get」開頭的cmdlet,並將它們與文本文件中的列表進行比較。它也消除了超額回報和空白,所以比較實際的作品。如何優化所有這些管道?
一切正常,但很難閱讀。我剛學習如何編寫PowerShell腳本,因此我不確定如何用更優雅的代碼完成相同的任務。
我敢打賭,有一種方法可以做到這一點,沒有所有的管道。我也無法從第一行代碼輸出到一個文本文件沒有一大堆額外的空間和返回。
我有一個優化添加:)使用名詞參數: - 獲取 – 2013-04-11 19:16:53
@ShayLevy是'-verb get';)但我認爲這也返回函數和動詞的別名得到 – 2013-04-11 19:27:45
不應該是'-Verb Get'? – alroc 2013-04-11 19:31:04