Monad + VBScript(IE)のサンプル
Monad + VBScript(IE) - PowerShell Memoで紹介した「MonadとVBScript(IE)の連携デモ」のスクリプトは、
「MSH Script」と「HTML(withVBScript)」が1つのファイルに詰め込まれているので、メンテナンス性がよくありません。
そこで、少し改修してみました。
- 変更点
- Viewである「HTML(withVBScript)」を別ファイルに切り出し
- エラーチェックを強化
サンプル
chart.msh
param($name, $value, $title = "Chart created by Monad")
#パイプでオブジェクトが渡されたかチェック
$test = $input.clone()
if($test.MoveNext() -eq $False){return}
#パイプで渡されたのが「プロセスオブジェクトの配列」かどうかチェック
$processs = @($input)
if(($processs[0] -is [System.Diagnostics.Process]) -eq $False){return}
#スクリプトのパスを取得
$scriptDir = parse-path $MyInvocation.MyCommand.Path -parent
#テンプレートスクリプトの実行(関数のインクルード)
$templateScriptPath = combine-path $scriptDir "chartHTML.msh"
. $templateScriptPath
#プロセスの名前を取得
$Names = '"' + [string]::Join('","', ($processs | foreach {$_.$name})) + '"'
#プロセスのプロパティを取得
$Values = '"' + [string]::Join('","', ($processs | foreach {$_.$value})) + '"'
#HTML出力
outChart $title $value $Names $Values
chartHTML.msh
function outChart()
{
param([String]$title, [String]$value, [String]$Names, [String]$Values)
@"
<html>
<head>
<title>$title</title>
</head>
<BODY>
<object id=ChartSpace1 classid="CLSID:0002E55D-0000-0000-C000-000000000046">
</object>
<script language="VBScript">
option explicit
Sub window_onload()
Dim oChart
Dim oSeries1
Dim oConst
ChartSpace1.Clear
Set oConst = ChartSpace1.Constants
Set oChart = ChartSpace1.Charts.Add
Set oSeries1= oChart.SeriesCollection.Add
with oSeries1
.Caption = "$value"
.SetData oConst.chDimCategories, oConst.chDataLiteral, Array($Names)
.SetData oConst.chDimValues, oConst.chDataLiteral, Array($Values)
.DataLabelsCollection.Add
.Type = 0
End with
oChart.HasLegend = True
oChart.HasTitle = True
oChart.Title.Caption = "$title"
End Sub
</script>
</body>
</html>
"@
}
実行方法
実行例
ps | .\chart name handlecount >result.html
ps | .\chart name PagedMemorySize >result.html