|
ここでは、PowerShell -Query Parameter のサンプルを紹介します。
- Get-WmiObject -query "Select * from win32_DiskPartition" | Ft -auto
次の事例を実行してみよう。
# http://www.computerperformance.co.uk/powershell/powershell_wmi_query.htm
# PowerShell query select:
Get-WmiObject -query "Select * from win32_DiskPartition" | Ft -auto
-query "Select * from win32_DiskPartition" で、WMI オブジェクトを取得して、テーブルに整形して、表示します。
それでは、実行してみましょう。下記のような結果が得られます。
NumberOfBlocks BootPartition Name PrimaryPartition Size Index
-------------- ------------- ---- ---------------- ---- -----
3906764800 False ディスク #3, パーティション #0 True 2000263577600 0
976764928 False ディスク #1, パーティション #0 True 500103643136 0
976769024 False ディスク #2, パーティション #0 True 500105740288 0
3907029056 False ディスク #4, パーティション #0 True 2000398876672 0
487469056 True ディスク #0, パーティション #0 True 249584156672 0
921600 False ディスク #0, パーティション #1 True 471859200 1
上記を実行すると
PowerShell 7.15 では、エラーです。何故か Object が無い??
Get-WmiObject: The term 'Get-WmiObject' is not recognized as a name
of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify
that the path is correct and try again.
PowerShell 7.4.6 でも、同様です。
[ 目次 ]
- Get-WmiObject -Query "Select * from win32_DiskPartition where PrimaryPartition ='True'" | Ft -auto
次の事例を実行してみよう。
# ---- PowerShell Query, Select and Where ----
Get-WmiObject -Query "Select * from win32_DiskPartition where PrimaryPartition ='True'" | Ft -auto
-query "Select * from win32_DiskPartition where PrimaryPartition ='True'" で、WMI オブジェクトを取得して、テーブルに整形して、表示します。
それでは、実行してみましょう。下記のような結果が得られます。
NumberOfBlocks BootPartition Name PrimaryPartition Size Index
-------------- ------------- ---- ---------------- ---- -----
3906764800 False ディスク #3, パーティション #0 True 2000263577600 0
976764928 False ディスク #1, パーティション #0 True 500103643136 0
976769024 False ディスク #2, パーティション #0 True 500105740288 0
3907029056 False ディスク #4, パーティション #0 True 2000398876672 0
487469056 True ディスク #0, パーティション #0 True 249584156672 0
921600 False ディスク #0, パーティション #1 True 471859200 1
上記を実行すると
PowerShell 7.15 では、エラーです。何故か Object が無い??
Get-WmiObject: The term 'Get-WmiObject' is not recognized as a name
of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify
that the path is correct and try again.
PowerShell 7.4.6 でも、同様です。
[ 目次 ]
- Get-WmiObject -query "Select * from win32_logicaldisk" |Format-Table $item -auto
次の事例を実行してみよう。
# PowerShell cmdlet to display a disk's free space
$Item = @("DeviceId", "MediaType", "Size", "FreeSpace")
# Next follows one command split over two lines by a backtick `
Get-WmiObject -query "Select * from win32_logicaldisk" `
|Format-Table $item -auto
2行目で、@("DeviceId", "MediaType", "Size", "FreeSpace") を $Item に格納します。
4〜5行で、-query "Select * from win32_logicaldisk" で、WMI オブジェクトを取得して、$item のテーブルに整形して、表示します。
それでは、実行してみましょう。下記のような結果が得られます。
DeviceId MediaType Size FreeSpace
-------- --------- ---- ---------
A: 5
C: 12 249584152576 90892582912
D: 12 500103639040 308351717376
E: 11
F: 12 500105736192 114140094464
G: 12 2000398872576 538432401408
H: 12 2000263573504 1208513372160
上記を実行すると
PowerShell 7.15 では、エラーです。何故か Object が無い??
Get-WmiObject: The term 'Get-WmiObject' is not recognized as a name
of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify
that the path is correct and try again.
PowerShell 7.4.6 でも、同様です。
[ 目次 ]
- Get-WmiObject win32_logicaldisk | Format-Table DeviceId, MediaType, Size, FreeSpace -auto
次の事例を実行してみよう。
Get-WmiObject win32_logicaldisk `
| Format-Table DeviceId, MediaType, Size, FreeSpace -auto
クラスが win32_logicaldisk の WMI オブジェクトを取得して、DeviceId, MediaType, Size, FreeSpace のテーブルに整形して、表示します。
それでは、実行してみましょう。下記のような結果が得られます。
DeviceId MediaType Size FreeSpace
-------- --------- ---- ---------
A: 5
C: 12 249584152576 90892582912
D: 12 500103639040 308351717376
E: 11
F: 12 500105736192 114140094464
G: 12 2000398872576 538432401408
H: 12 2000263573504 1208513372160
上記を実行すると
PowerShell 7.15 では、エラーです。何故か Object が無い??
Get-WmiObject: The term 'Get-WmiObject' is not recognized as a name
of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify
that the path is correct and try again.
PowerShell 7.4.6 でも、同様です。
[ 目次 ]
- "Select $([string]::Join(',',$Item)) from win32_logicaldisk where MediaType=12"
次の事例を実行してみよう。
# PowerShell -Query example
$Item = @("DeviceId", "MediaType", "Size", "FreeSpace")
# Next follows one command split over four lines by backticks (`)
Get-WmiObject -computer kamxxxxx-pc7-14 -query `
"Select $([string]::Join(',',$Item)) from win32_logicaldisk `
where MediaType=12" | sort MediaType, DeviceID | `
Format-Table $item -auto
クラスが win32_logicaldisk の WMI オブジェクトを取得して、DeviceId, MediaType, Size, FreeSpace のテーブルに整形して、表示します。
3行目で、@("DeviceId", "MediaType", "Size", "FreeSpace") を $Item に格納します。
5〜8行で、-computer kamxxxxx-pc7-14 で、-query "Select $([string]::Join(',',$Item)) from win32_logicaldisk where MediaType=12" の WMI オブジェクトを取得して、MediaType, DeviceID で、並び変えて、$item のテーブルに整形して、表示します。
それでは、実行してみましょう。下記のような結果が得られます。
DeviceId MediaType Size FreeSpace
-------- --------- ---- ---------
C: 12 249584152576 90897178624
D: 12 500103639040 308351717376
F: 12 500105736192 114140094464
G: 12 2000398872576 538432401408
H: 12 2000263573504 1208513372160
上記を実行すると
PowerShell 7.15 では、エラーです。何故か Object が無い??
Get-WmiObject: The term 'Get-WmiObject' is not recognized as a name
of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify
that the path is correct and try again.
PowerShell 7.4.6 でも、同様です。
|