Created
April 25, 2012 16:00
-
-
Save tkmtmkt/2490903 to your computer and use it in GitHub Desktop.
Jobを使って計算機のバージョン情報を収集するサンプル
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.SYNOPSIS | |
ソフトウェアのバージョン情報を収集する | |
#> | |
$ps1_file = &{$myInvocation.ScriptName} | |
$base_dir = Split-Path (Split-Path $ps1_file) | |
$log_dir = "$base_dir\log" | |
$log_file = "$log_dir\$((Split-Path -Leaf $ps1_file).Replace(".ps1",".log"))" | |
if (-not (Test-Path "$log_dir")) {New-Item "$log_dir" -Force -ItemType Directory} | |
<# | |
.SYNOPSIS | |
メイン処理 | |
#> | |
$tmp = $Host.UI.RawUI.WindowSize | |
$tmp.Width = 120 | |
$Host.UI.RawUI.WindowSize = $tmp | |
Start-Transcript $log_file -Append | |
$csv_file = "$base_dir\conf\computers.csv" | |
$computers = ConvertFrom-Csv (cat $csv_file) | |
$outdir = "version_$(Get-Date -f "yyyyMMdd")" | |
if (-not (Test-Path $outdir)) {New-Item $outdir -Force -ItemType Directory} | |
# ソフトウェアバージョン情報を取得するジョブを発行する | |
$jobs = $computers | %{ | |
$job = gwmi Win32_Product -AsJob -ComputerName $_.IPAddress | |
$job.Name = $_.COMPUTERNAME | |
$job | |
} | |
# ジョブの実行結果を受け取る | |
Wait-Job $jobs | %{ | |
$job = $_ | |
Write-Host "$($job.Name) : $($job.State)" | |
if ($_.HasMoreData) { | |
Receive-Job $job | | |
select IdentifyingNumber,Name,Vendor,Version,Caption | | |
sort Vender,Name | | |
Export-CSV "$outdir\$($job.Name).csv" -Encoding OEM -NoTypeInformation | |
} | |
} | |
Remove-Job $jobs | |
Stop-Transcript | |
# vim: set ft=ps1 ts=4 sw=4 et: |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment