labunix's blog

labunixのラボUnix

PowerShellでBIOS、OS情報をCSV出力してみる

■多くを期待してはいけないWindows。
 それは、PowerShellにも当てはまる。
 例えば、MSの下記のサイトをもう少しカスタマイズしてみる。

 コンピューターに関する情報の収集
 http://technet.microsoft.com/ja-jp/library/dd315240.aspx

■大量のプロパティの中から自分が欲しい名前にマッチしたプロパティを検索

PS> Get-WmiObject -Class Win32_BIOS | Get-Member -MemberType property | Where-Object {$_.Name -Like "*Version*"}


   TypeName: System.Management.ManagementObject#root\cimv2\Win32_BIOS

Name               MemberType Definition
----               ---------- ----------
BIOSVersion        Property   System.String[] BIOSVersion {get;set;}
SMBIOSBIOSVersion  Property   System.String SMBIOSBIOSVersion {get;set;}
SMBIOSMajorVersion Property   System.UInt16 SMBIOSMajorVersion {get;set;}
SMBIOSMinorVersion Property   System.UInt16 SMBIOSMinorVersion {get;set;}
Version            Property   System.String Version {get;set;}

■BIOS情報をCSVで出力

PS> Get-WmiObject -Class Win32_BIOS  | Select-Object Name,SMBIOSBIOSVersion | ConvertTo-Csv -NoTypeInfor
mation
"Name","SMBIOSBIOSVersion"
"PhoenixBIOS 4.0 Release 6.0     ","6.00"

■OS情報をCSVで出力

PS> Get-WmiObject -Class Win32_OperatingSystem | Select-Object Version,BuildNumber | ConvertTo-Csv -NoTypeInfomation
"Version","BuildNumber"
"6.1.7600","7600"

PS> [Environment]::OSVersion | ConvertTo-Csv -NoTypeInformation
"Platform","ServicePack","Version","VersionString"
"Win32NT","","6.1.7600.0","Microsoft Windows NT 6.1.7600.0"

PS> Get-WmiObject Win32_OperatingSystem | Select-Object Caption, Version, WindowsDirectory | ConvertTo-C
sv -NoTypeInformation
"Caption","Version","WindowsDirectory"
"Microsoft Windows 7 Professional ","6.1.7600","C:\Windows"

■プリンタ、FAXデバイスのCSV出力。
 「Write-Output」でうまくCSVに収まるように回避出来る例。

PS> Get-WmiObject Win32_Printer | Select-Object Name,Location,ShareName | Write-Output | ConvertTo-Csv -
NoTypeInformation
"Name","Location","ShareName"
"Microsoft XPS Document Writer",,
"Fax",,

■ネットワークアダプタの情報取得
 「Write-Output」でもうまくいかない例。

PS> Get-WmiObject Win32_NetworkAdapterConfiguration | Where-Object {$_.IPAddress -like "*.*"} | Select-Object IPAddress,DefaultIPGateway,DHCPEnabled,DNSDomain,Description | Write-Output | ConvertTo-Csv -NoTypeInformation

"IPAddress","DefaultIPGateway","Description"
"System.String[]","System.String[]","Intel(R) 82574L Gigabit Network Connection"
"System.String[]",,"VMware Virtual Ethernet Adapter for VMnet1"
"System.String[]",,"VMware Virtual Ethernet Adapter for VMnet8"

■「Format-List」でごまかす。

PS> Get-WmiObject Win32_NetworkAdapterConfiguration | Where-Object {$_.IPAddress -like "*.*"} | Select-O
bject IPA*,*IPGateway,Description | format-list


IPAddress        : {192.168.6.101}
DefaultIPGateway : {192.168.6.2}
Description      : Intel(R) 82574L Gigabit Network Connection

IPAddress        : {169.254.140.75, fe80::546b:7e6c:5219:8c4b}
DefaultIPGateway :
Description      : VMware Virtual Ethernet Adapter for VMnet1

IPAddress        : {192.168.118.1, fe80::740e:3cc6:d799:b8b6}
DefaultIPGateway :
Description      : VMware Virtual Ethernet Adapter for VMnet8

■3列程度なら、ひとつひとつか、ループで。。。
 というかもうPowerShellでなくても。。。

PS> Get-WmiObject Win32_NetworkAdapterConfiguration | Where-Object {$_.IPAddress -like "*.*"} | Select-O
bject IPA* | Format-Table -HideTableHeaders

{192.168.6.101}
{169.254.140.75, fe80::546b:7e6c:5219:8c4b}
{192.168.118.1, fe80::740e:3cc6:d799:b8b6}

■ということで、PowerShellで「何もかも」は止めた方が良い。