|
ここでは、Windows PowerShell -Match and -Like のサンプルを紹介します。
- $WMI = Get-WmiObject -List | Where-Object {$_.name -Match "network"}
次の事例を実行してみよう。
# http://www.computerperformance.co.uk/powershell/powershell_match.htm
# PowerShell example to list demonstrate -Match
# Author: Guy Thomas
# Clear-Host
$WMI = Get-WmiObject -List | Where-Object {$_.name -Match "network"}
$WMI
Write-Host `n $WMI.count "WMI objects contain the word network."
5行目では、WMI オブジェクトを取得して、.name に "network" があるのを、$WMI に格納します。
6行目で、$WMI を表示します。
7行目で、取得した WMI オブジェクトの個数を表示します。
それでは、実行してみましょう。下記のような結果が得られます。
NameSpace: ROOT\CIMV2
Name Methods Properties
---- ------- ----------
CIM_NetworkAdapter {SetPowerState, R... {AutoSense, Availability, Caption, ConfigMa...
Win32_NetworkAdapter {SetPowerState, R... {AdapterType, AdapterTypeId, AutoSense, Ava...
Win32_NetworkConnection {} {AccessMask, Caption, Comment, ConnectionSt...
Win32_NetworkProtocol {} {Caption, ConnectionlessService, Descriptio...
Win32_NetworkClient {} {Caption, Description, InstallDate, Manufac...
Win32_NetworkLoginProfile {} {AccountExpires, AuthorizationFlags, BadPas...
Win32_NetworkAdapterConfiguration {EnableDHCP, Rene... {ArpAlwaysSourceRoute, ArpUseEtherSNAP, Cap...
Win32_NetworkAdapterSetting {} {Element, Setting}
Win32_SystemNetworkConnections {} {GroupComponent, PartComponent}
Win32_PerfFormattedData_Counters... {} {Bytestransmitted, BytestransmittedPersec, ...
Win32_PerfRawData_Counters_Netwo... {} {Bytestransmitted, BytestransmittedPersec, ...
Win32_PerfFormattedData_Counters... {} {BuildScatterGatherCyclesPersec, Caption, D...
Win32_PerfRawData_Counters_PerPr... {} {BuildScatterGatherCyclesPersec, Caption, D...
Win32_PerfFormattedData_Counters... {} {BuildScatterGatherListCallsPersec, Caption...
Win32_PerfRawData_Counters_PerPr... {} {BuildScatterGatherListCallsPersec, Caption...
Win32_PerfFormattedData_Counters... {} {Caption, Description, DevicePowerState, Fr...
Win32_PerfRawData_Counters_Physi... {} {Caption, Description, DevicePowerState, Fr...
Win32_PerfFormattedData_Counters... {} {BaseTCPRTT, BaseUDPRTT, Caption, CurrentTC...
Win32_PerfRawData_Counters_Remot... {} {BaseTCPRTT, BaseUDPRTT, Caption, CurrentTC...
Win32_PerfFormattedData_NETCLRNe... {} {BytesReceived, BytesSent, Caption, Connect...
Win32_PerfRawData_NETCLRNetworki... {} {BytesReceived, BytesSent, Caption, Connect...
Win32_PerfFormattedData_NETCLRNe... {} {BytesReceived, BytesSent, Caption, Connect...
Win32_PerfRawData_NETCLRNetworki... {} {BytesReceived, BytesSent, Caption, Connect...
Win32_PerfFormattedData_Tcpip_Ne... {} {BytesReceivedPersec, BytesSentPersec, Byte...
Win32_PerfRawData_Tcpip_NetworkA... {} {BytesReceivedPersec, BytesSentPersec, Byte...
Win32_PerfFormattedData_Tcpip_Ne... {} {BytesReceivedPersec, BytesSentPersec, Byte...
Win32_PerfRawData_Tcpip_NetworkI... {} {BytesReceivedPersec, BytesSentPersec, Byte...
27 WMI objects contain the word network.
上記を実行すると
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 でも、同様です。
[ 目次 ]
- $WMI = Get-WmiObject -List | Where-Object {$_.name -Like "*network"}
次の事例を実行してみよう。
# PowerShell example to demonstrate -Like
# Author: Guy Thomas
# Clear-Host
$WMI = Get-WmiObject -List | Where-Object {$_.name -Like "*network"}
$WMI
Write-Host `n $WMI.count "WMI objects contain the word network."
この事例は、前項と比べ、4行の {$_.name -Match "network"} が {$_.name -Like "*network"} に変更されたものです。
{$_.name -Match "network"} は、何れかに network があるものを取得するのに対して、{$_.name -Like "*network"} は、最後尾が network であるものを取得します。
それでは、実行してみましょう。下記のような結果が得られます。
NameSpace: root\cimv2
Name Methods Properties
---- ------- ----------
Win32_PerfFormattedData_Counters... {} {BaseTCPRTT, BaseUDPRTT, Caption, CurrentTC...
Win32_PerfRawData_Counters_Remot... {} {BaseTCPRTT, BaseUDPRTT, Caption, CurrentTC...
2 WMI objects contain the word network.
最後尾が一致するのは、2個だけでした。
上記を実行すると
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 でも、同様です。
[ 目次 ]
- If($_.name -Match "System") { .. }
次の事例を実行してみよう。
# Clear-Host
$i=0
Get-WmiObject -List | ForEach-Object {
If($_.name -Match "System") {
Write-Host $($_.name) ; $i++
}
}
"There are $i objects containing System"
3〜7行で、WMI オブジェクトリストを取得して、アイテム毎に、.name が "System" にマッチするものを表示します。また、その個数もカウントしています。
それでは、実行してみましょう。下記のような結果が得られます。
__SystemClass
Win32_SystemConfigurationChangeEvent
Win32_SystemTrace
Win32_ComputerSystemEvent
MSFT_NetBootSystemDriversFailed
__SystemEvent
__SystemSecurity
CIM_ManagedSystemElement
CIM_OperatingSystem
Win32_OperatingSystem
< 省略 >
Win32_PerfFormattedData_PerfOS_System
Win32_PerfRawData_PerfOS_System
Win32_PerfFormattedData_WindowsWorkflowFoundation4000_WFSystemWorkflow4000
Win32_PerfRawData_WindowsWorkflowFoundation4000_WFSystemWorkflow4000
There are 64 objects containing System
上記を実行すると
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 でも、同様です。
[ 目次 ]
- Where-Object {$_.name -NotMatch "CIM" -And $_.name -NotMatch "__"}
次の事例を実行してみよう。
# PowerShell example to list demonstrate -NotMatch and -And
# Author: Guy Thomas
# Clear-Host
$WMI = Get-WmiObject -List |`
Where-Object {$_.name -NotMatch "CIM" -And $_.name -NotMatch "__"}
$WMI
Write-Host `n $WMI.count "objects containing WMI, but not CIM or __"
4〜6行で、WMI オブジェクトリストを取得して、.name に "CIM" が無い かつ .name に "__" が無い のを、表示します。
7行目で、表示したオブジェクトの個数を表示します。
それでは、実行してみましょう。下記のような結果が得られます。
NameSpace: ROOT\cimv2
Name Methods Properties
---- ------- ----------
Win32_DeviceChangeEvent {} {EventType, SECURITY_DESCRIPTOR, TIME_C...
Win32_SystemConfigurationChangeE... {} {EventType, SECURITY_DESCRIPTOR, TIME_C...
Win32_VolumeChangeEvent {} {DriveName, EventType, SECURITY_DESCRIP...
MSFT_WMI_GenericNonCOMEvent {} {ProcessId, PropertyNames, PropertyValu...
MSFT_NCProvEvent {} {Namespace, ProviderName, Result, SECUR...
MSFT_NCProvCancelQuery {} {ID, Namespace, ProviderName, Result...}
MSFT_NCProvClientConnected {} {Inproc, Namespace, ProviderName, Resul...
MSFT_NCProvNewQuery {} {ID, Namespace, ProviderName, Query...}
MSFT_NCProvAccessCheck {} {Namespace, ProviderName, Query, QueryL...
Win32_SystemTrace {} {SECURITY_DESCRIPTOR, TIME_CREATED}
< 省略 >
Win32_PerfRawData_WindowsMediaPl... {} {AFTSExecutionTimems, ArtExtractionTime...
Win32_PerfFormattedData_WindowsW... {} {Caption, Description, Frequency_Object...
Win32_PerfRawData_WindowsWorkflo... {} {Caption, Description, Frequency_Object...
Win32_PerfFormattedData_WindowsW... {} {Caption, Description, Frequency_Object...
Win32_PerfRawData_WindowsWorkflo... {} {Caption, Description, Frequency_Object...
Win32_PerfFormattedData_Workflow... {} {AverageWorkflowLoadTime, AverageWorkfl...
Win32_PerfRawData_WorkflowServic... {} {AverageWorkflowLoadTime, AverageWorkfl...
Win32_PerfFormattedData_WSearchI... {} {ActiveConnections, Caption, CleanWidSe...
Win32_PerfRawData_WSearchIdxPi_S... {} {ActiveConnections, Caption, CleanWidSe...
947 objects containing WMI, but not CIM or __
上記を実行すると
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 でも、同様です。
[ 目次 ]
- $Guy -Match "\s"
次の事例を実行してみよう。
# -Match Character Class Example
$Guy ="Babe Ruth"
$Guy -Match "\s"
# Result PS> True
<#
\w is the equivalent of: -Match [a-zA-Z_0-9].
\d matches any digit character.
\s matches any white space character including tabs.
#>
2行目で、"Babe Ruth" を $Guy に格納します。
3孕めで、$Guy 内にスペース文字があるかを評価します。
それでは、実行してみましょう。下記のような結果が得られます。
True
上記を実行すると
PowerShell 7.15 でも、正常(同様)に、実行されます。
PowerShell 7.4.6 でも、同様です。
[ 目次 ]
- [char[]]"rd2cp30" -Match "\d"
次の事例を実行してみよう。
# PowerShell example to extract numbers from a string
[char[]]"rd2cp30" -Match "\d"
<#
\W (capital W) any non-word character
\S any non-white space.
\D any non digit.
#>
"rd2cp30" から 数字を取り出し、char 配列にキャストして、表示します。
それでは、実行してみましょう。下記のような結果が得られます。
2
3
0
上記を実行すると
PowerShell 7.15 でも、正常(同様)に、実行されます。
PowerShell 7.4.6 でも、同様です。
[ 目次 ]
- Get-Help about_Comparison_Operators
次の事例を実行してみよう。
Get-Help about_Comparison_Operators
about_Comparison_Operators のヘルプを表示します。
それでは、実行してみましょう。下記のような結果が得られます。
" > パイプ問題(?)" 関連で、下記のファイルを差し替えました。
表示内容は、050_PS_-Match_-Like_07.txt に保存しました。
上記を実行すると
PowerShell 7.15 では、エラーです。何故か about_Comparison_Operators が無い??
Get-Help: Get-Help could not find about_Comparison_Operators in a help file
in this session. To download updated help topics type: "Update-Help".
To get help online, search for the help topic in the TechNet library at
https://go.microsoft.com/fwlink/?LinkID=107116.
PowerShell 7.4.6 でも、同様です。
|