|
ここでは、PowerShell -Query Parameter のサンプルを紹介します。
- Get-WmiObject -query "Select * from win32_DiskPartition" | Ft -auto
次の事例を実行してみよう。
#!/usr/bin/pwsh
# 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 オブジェクトを取得して、テーブルに整形して、表示します。
それでは、実行してみましょう。下記のような結果が得られます。
Get-WmiObject : The term 'Get-WmiObject' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At /xxxx/PSUX-Support/PS_ref_10/091_PS_WMI-Qyery_01.ps1:5 char:1
+ Get-WmiObject -query "Select * from win32_DiskPartition" | Ft -auto
+ ~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Get-WmiObject:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Linux版では、Get-WmiObject コマンドがサポートされていない。
[ 目次 ]
- Get-WmiObject -Query "Select * from win32_DiskPartition where PrimaryPartition ='True'" | Ft -auto
次の事例を実行してみよう。
#!/usr/bin/pwsh
# ---- 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 オブジェクトを取得して、テーブルに整形して、表示します。
それでは、実行してみましょう。下記のような結果が得られます。
Get-WmiObject : The term 'Get-WmiObject' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At /xxxx/PSUX-Support/PS_ref_10/091_PS_WMI-Qyery_02.ps1:4 char:1
+ Get-WmiObject -Query "Select * from win32_DiskPartition `
+ ~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Get-WmiObject:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Linux版では、Get-WmiObject コマンドがサポートされていない。
[ 目次 ]
- Get-WmiObject -query "Select * from win32_logicaldisk" |Format-Table $item -auto
次の事例を実行してみよう。
#!/usr/bin/pwsh
# 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
4 行目で、@("DeviceId", "MediaType", "Size", "FreeSpace") を $Item に格納します。
6 〜7 行で、-query "Select * from win32_logicaldisk" で、WMI オブジェクトを取得して、$item のテーブルに整形して、表示します。
それでは、実行してみましょう。下記のような結果が得られます。
Get-WmiObject : The term 'Get-WmiObject' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At /xxxx/PSUX-Support/PS_ref_10/091_PS_WMI-Qyery_03.ps1:6 char:1
+ Get-WmiObject -query "Select * from win32_logicaldisk" `
+ ~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Get-WmiObject:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Linux版では、Get-WmiObject コマンドがサポートされていない。
[ 目次 ]
- Get-WmiObject win32_logicaldisk | Format-Table DeviceId, MediaType, Size, FreeSpace -auto
次の事例を実行してみよう。
#!/usr/bin/pwsh
Get-WmiObject win32_logicaldisk `
| Format-Table DeviceId, MediaType, Size, FreeSpace -auto
クラスが win32_logicaldisk の WMI オブジェクトを取得して、DeviceId, MediaType, Size, FreeSpace のテーブルに整形して、表示します。
それでは、実行してみましょう。下記のような結果が得られます。
Get-WmiObject : The term 'Get-WmiObject' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At /xxxx/PSUX-Support/PS_ref_10/091_PS_WMI-Qyery_04.ps1:4 char:1
+ Get-WmiObject win32_logicaldisk `
+ ~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Get-WmiObject:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Linux版では、Get-WmiObject コマンドがサポートされていない。
[ 目次 ]
- "Select $([string]::Join(',',$Item)) from win32_logicaldisk where MediaType=12"
次の事例を実行してみよう。
#!/usr/bin/pwsh
# 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 のテーブルに整形して、表示します。
5 行目で、@("DeviceId", "MediaType", "Size", "FreeSpace") を $Item に格納します。
7 〜 10 行で、-computer kamxxxxx-pc7-14 で、-query "Select $([string]::Join(',',$Item)) from win32_logicaldisk where MediaType=12" の WMI オブジェクトを取得して、MediaType, DeviceID で、並び変えて、$item のテーブルに整形して、表示します。
それでは、実行してみましょう。下記のような結果が得られます。
Get-WmiObject : The term 'Get-WmiObject' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At /xxxx/PSUX-Support/PS_ref_10/091_PS_WMI-Qyery_05.ps1:8 char:1
+ Get-WmiObject -computer kamifuji-pc7-14 -query `
+ ~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Get-WmiObject:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Linux版では、Get-WmiObject コマンドがサポートされていない。
|