0
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" で始まるすべてのコマンドレットを取得し、それらをテキスト ファイル内のリストと比較します。また、余分なリターンと空白を削除するため、比較は実際に機能します。

すべてが機能しますが、読むのはかなり難しいです。PowerShell スクリプトの書き方を学んでいるところなので、より洗練されたコードで同じタスクを達成する方法がわかりません。

すべてのパイプなしでこれを行う方法があるに違いありません。また、コードの最初の行からテキスト ファイルに出力するための出力を、大量の余分なスペースと改行なしで取得することもできませんでした。

4

3 に答える 3

1

私はこれが同じことをすると思います:

get-command -CommandType "cmdlet" -name get*  | SELECT -expand name | 
out-file "getcommands.txt" -encoding Ascii

compare-object -referenceobject (Get-Content "oldcommands.txt") -differenceobject (Get-Content "getcommands.txt") -includeequal
于 2013-04-11T19:05:05.133 に答える
0

V3 をお持ちの場合、これは少し速いようです。

(get-command -CommandType "cmdlet" -name get*).name |
set-content getcommands.txt
于 2013-04-11T23:37:12.140 に答える
-1

を使用して、ソースでコマンドレットのリストをフィルター処理し-verbます。ベスト プラクティスは、パイプラインの左側 (データ ソースに最も近い) でできる限りフィルタリングすることです。

get-command -verb get |where-object{$_.CommandType -eq "Cmdlet"}|select-object -expandpropertyproperty name|out-file getcommands.txt -encoding ascii
compare-object -referenceobject $(Get-Content "oldcommands.txt") -differenceobject $(Get-Content "getcommands.txt") -includeequal

パラメータ for を使用して も削除する方法があるはずですが、ここで機能させることはできません。次のいずれかが機能することを期待していますが、どちらも機能しません。Where-Object-CommandTypeget-command

get-command -verb get -CommandType Cmdlet
get-command -verb get -CommandType [system.management.automation.commandtypes]::Cmdlet
于 2013-04-11T19:35:57.930 に答える