なんとなく検索していたら面白いもの見つけたのでメモメモ。
Write-Host コマンドレットの使用
Write-Hostで表示されるメッセージに色つけれるみたい!
Write-Host "コメント" -foregroundcolor red -backgroundcolor yellow
ということで、各サーバのCドライブの利用状況を色別けして表示させてみるスクリプトとか作ってみた。
function MyFunction
{
$serverlist = @("192.168.1.1"; "192.168.1.2"; "192.168.1.3")
#サーバリストの分だけループ
foreach ($ipaddress in $serverlist)
{
Write-Host "`n"
Write-Host "★☆★-----------------【" $ipaddress "】-----------------★☆★" -foregroundcolor red -backgroundcolor yellow
#実行
#Cドライブの使用状況を表示する
CheckDiskSize
Read-Host "実行結果を確認して、エンターを押してください!"
}
}
function CheckDiskSize
{
Write-Host "■□■ Cドライブの使用状況を表示します。 ■□■"
#Session作成
$Session = New-PSSession -ComputerName $ipaddress
#コマンド実行
Invoke-Command -Session $Session -Scriptblock{
$drive = $args[0]
#ドライブの容量チエック
$used = (Get-PSDrive $drive).Used / 1GB
$free = (Get-PSDrive $drive).Free / 1GB
#使用率(四捨五入)
$rate = [math]::Truncate((($used / ($free + $used)) * 100)+.5)
#メッセージ作成
$Message = $scriptName + "`n" + "`n" +
$drive + "ドライブの使用量:" + ([math]::Truncate($used)+.5) + "GB" + "`n" +
$drive + "ドライブの空き容量:" + ([math]::Truncate($free)+.5) + "GB" + "`n" +
$drive + "ドライブの使用率:" + $rate + "%"
#使用率が70%以上で80%未満だったら黄色で表示
if ($rate -gt "70" -and $rate -le "80")
{
Write-Host $Message -foregroundcolor Yellow
}
#使用率が80%以上だったら赤字で表示
elseif ($rate -gt "80")
{
Write-Host $Message -foregroundcolor Red
}
#それ以外はセーフ
else
{
Write-Host $Message -foregroundcolor Green
}
} -ArgumentList "C"
#Session削除
Remove-PSSession $Session
}
}
#実行
MyFunction
WinRMで$serverlistの端末に対してドライブの容量チェックを実行していきます。
使用率に応じてコンソールに表示される色を変えています。
手動でサッと確認したいときに便利かと。

0 件のコメント:
コメントを投稿