「マイクロソフト系技術情報 Wiki」は、「Open棟梁Project」,「OSSコンソーシアム .NET開発基盤部会」によって運営されています。
以前から、オブジェクトベースのプログラム、スクリプト(perl、WSH)などは存在したが、
PowerShellが唯一のオブジェクト・ベースのシェル(perlに似ている)である。
# OSレイヤから少々遠いためシェルというよりシェルスクリプトという意見もある。
皆、
違いに混乱する。
Like This
↓パイプ ↓ラムダ式的な
get-service | where-object {$_.Status -eq "Running"}
パイプしなくてもキャスト(暗黙)でつなげられる。
function dirinfo {
param(
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[object]$dir,
[boolean]$totalup = $false
)
# 空っぽの配列
$dirs = @()
# $dir へ プロパティ追加
$dir | Add-Member -MemberType NoteProperty -Name TotalFiles -Value 0
$dir | Add-Member -MemberType NoteProperty -Name TotalSize -Value 0
foreach ($f in (get-childitem $dir.FullName)) {
if ( $f.Attributes -band [System.IO.FileAttributes]::Directory ) {
# サブフォルダの情報
$ret = dirinfo $f $totalup
if ( $totalup ) {
# 末尾の情報を $dir へ加算
$dir.TotalFiles += $ret[-1].TotalFiles
$dir.TotalSize += $ret[-1].TotalSize
}
$dirs += $ret # サブフォルダ全体を覚える
} else {
$dir.TotalFiles += 1
$dir.TotalSize += $f.Length
$dirs += $f
}
}
$dirs += $dir # $dir自身を末尾に覚える
# 覚えたものをまとめて返す
write-output $dirs
}
dirinfo (get-item "c:\program files") | sort-object totalsize -Descending |
where { $_.TotalSize -gt 1 } | select-object -first 5 |
format-list -Property TotalFiles, TotalSize, Fullname
(行は折り返して表示しています。実行する際は、1行に入力してください。)Tags: :シェル, :インフラストラクチャ, :Windows