PowerShellでシステム情報を取得:Get-SystemInfoコマンドの5つの具体的な利用例に対する実務上の答えは「Windowsとhardwareの概要はGet-ComputerInfo、必要な項目だけならGet-CimInstance、互換確認にはsysteminfo.exeを使います。Get-SystemInfoは標準cmdletではなく、定義元不明のfunctionを実行しません。」です。ここではGet-SystemInfoという標準cmdletは存在しないため、旧記事の自作function名を標準commandと誤認しない前提を明示し、初回確認、sample実行、本番判定、元へ戻す条件をそれぞれ独立させます。
Get-ComputerInfo・CIM・systeminfoの重なりを整理する
端末情報を集める際は、対象コンピューターを一台に固定し、OSのCaption、Version、BuildNumber、OSArchitectureと機種情報をCIMから取得します。systeminfoの表示文字列は言語設定で変わるため、機械処理の主値には使わず照合用にします。遠隔取得では認証情報を出力へ残さず、接続失敗を空データと区別します。
- Get-Command Get-ComputerInfoとGet-Command Get-SystemInfoで実体を比較する
- $PSVersionTableとWindows edition/buildを記録する
- 取得項目にserial、domain、hotfix等の機密情報が含まれないか確認する
- remote取得はCIM sessionと権限を組織policyに合わせる
OS・computer system・BIOSを別objectで読む
- Get-SystemInfoを標準cmdletと説明する
- 全propertyを外部共有する
- memory単位を誤る
- systeminfoのlocal language列名を固定する
- CIM値だけでsupport可否を断定する
localized表示名とproperty名を混同しない
OS概要を取得
Get-ComputerInfo -Property WindowsProductName,WindowsVersion,OsBuildNumber,OsArchitecture,CsManufacturer,CsModel
Get-ComputerInfoは多くのpropertyを返すため-Propertyで限定します。WindowsProductName等の表示は管理判断の唯一の根拠にせずbuild番号も記録します。
OS CIMを限定取得
Get-CimInstance -ClassName Win32_OperatingSystem |
Select-Object Caption,Version,BuildNumber,OSArchitecture,LastBootUpTime
必要classだけをqueryします。LastBootUpTimeからuptimeを推定できますが、fast startup等の運用条件も確認します。
computer systemを確認
Get-CimInstance -ClassName Win32_ComputerSystem |
Select-Object Manufacturer,Model,SystemType,TotalPhysicalMemory,Domain
TotalPhysicalMemoryはbyteです。Domain情報の外部共有は避け、inventory用途の必要列だけ保存します。
firmware情報を読む
Get-CimInstance -ClassName Win32_BIOS |
Select-Object Manufacturer,SMBIOSBIOSVersion,ReleaseDate
BIOS設定値の全項目を返すcommandではありません。変更はvendor固有toolと承認済み手順に分けます。
systeminfo互換表示
systeminfo.exe /FO CSV | ConvertFrom-Csv |
Select-Object 'OS Name','OS Version','System Type','Total Physical Memory'
列名はOS表示言語で変わり得ます。自動化ではCIM propertyの方が安定し、systeminfoは人間向け確認に使います。
remote CIMの対象hostを明示する
Get-ComputerInfoは複数sourceから情報を集めるため、全property取得は時間がかかります。CIM classには現在値、firmware報告値、管理database値が混在し、physical realityと完全一致しない場合があります。memoryはbyteと表示単位を分け、build、edition、architectureを一つの文字列へ潰さず列として保持します。
domain・serial・modelの共有範囲を絞る
system情報の読取は変更を伴いませんが、serial number、domain、user、hotfix一覧は攻撃に使えるinventory情報です。出力列と共有先を限定し、credentialをscriptへ書きません。remote CIMのためにfirewallやauthenticationを弱めず、既存の管理channelを使います。自作Get-SystemInfoが存在する場合はDefinitionと署名を読み、信頼できなければ実行せずno-profile sessionで標準commandを使います。
同時刻snapshotでsource間の差を比較する
SettingsのAbout、winver、firmware inventoryと主要項目を照合し、同じhostと取得時刻を記録します。Get-CommandのSourceとCommandTypeを保存し、自作function混入がないことを確認します。CSVへ出す場合はExport-Csv後に読み戻し、必要列以外が含まれないことをreviewします。
build・architecture・modelを相互照合する
OS build、architecture、manufacturer、BIOSは一つのcommandに依存せず、Get-ComputerInfoとCIMの定義済みpropertyを突き合わせます。systeminfo.exeの表示文字列はlocale依存なのでCSV化しても列名が変わり、自動化の主sourceにはCIMを使います。
OSとmachine情報を一つのobjectへまとめる
$os=Get-CimInstance Win32_OperatingSystem -ErrorAction Stop
$cs=Get-CimInstance Win32_ComputerSystem -ErrorAction Stop
[pscustomobject]@{Computer=$env:COMPUTERNAME;Caption=$os.Caption;Build=$os.BuildNumber;Architecture=$os.OSArchitecture;Manufacturer=$cs.Manufacturer;Model=$cs.Model}
CIM取得の欠損をsource別に記録する
- system情報を照合:OS caption/build/architectureとmachine modelがCIMで一件ずつ取得され、別手段と一致する
- 一部property欠損:任意propertyがnullでもclass取得成功と区別し、unknownとして残す
- CIM・remote error:CIM service、remote auth、class不存在、timeoutは例外と所要時間を記録する
WindowsVersionのmarketing表記とOsBuildNumberを同じversion番号とみなしません。SerialNumber等を公開reportへ無加工で載せず、複数computerの結果にはPSComputerNameと取得時刻を必ず含めます。
systeminfo表示とCIM値を比較する
Get-ComputerInfo -Property WindowsProductName,WindowsVersion,OsBuildNumber,OsArchitecture | Format-List
systeminfo.exe /FO CSV | ConvertFrom-Csv | Select-Object -First 1
desktop/server、32/64bit表記、null firmware値、remote timeoutをtestします。class/property名、host、build、取得時刻、command versionを記録し、locale表示は補助列にします。
三つのcommandは同じ情報を完全には返しません。OS buildはWin32_OperatingSystem、modelはWin32_ComputerSystemのようにauthorityを決め、取得時刻と対象hostをそろえて比較します。

コメント