|
ここでは、PowerShell's Comparison Operators のサンプルを紹介します。
Powershell のオペレータについてのサンプルです。
- $List = $Dir | Where-Object {$_.extension -eq ".dll"}
次の事例を実行してみよう。
#!/usr/bin/pwsh
# http://www.computerperformance.co.uk/powershell/powershell_comparison_operators.htm
# PowerShell script to list DLLs under system32 folder
$Dir = Get-Childitem ./ -recurse -EA SilentlyContinue
$List = $Dir | Where-Object {$_.extension -eq ".ps1"}
$List | Format-Table Name, CreationTime -auto
5 行目では、./ から再帰的にアイテムを検索取得し、$Dir に格納します。
6 行目では、$Dir から拡張子が ".dll" であるものを $List に格納します。
7 行目では、$List から Name, CreationTime にテーブル成形します。
それでは、実行してみましょう。下記のような結果が得られます。
Name CreationTime
---- ------------
011_PS_BuiltInCmdlets_01.ps1 2019/09/04 16:40:55
011_PS_BuiltInCmdlets_02.ps1 2019/09/04 18:01:11
012_PS_CompareObject_01.ps1 2019/09/05 10:27:31
012_PS_CompareObject_02.ps1 2019/09/05 10:37:52
012_PS_CompareObject_03.ps1 2019/09/05 11:00:55
012_PS_CompareObject_04.ps1 2019/09/05 12:44:18
012_PS_CompareObject_05.ps1 2019/09/05 13:08:07
013_PS_ComparisonOperators_01.ps1 2019/09/05 13:39:43
013_PS_ComparisonOperators_02.ps1 2016/07/01 15:07:49
013_PS_ComparisonOperators_03.ps1 2016/07/01 15:15:33
013_PS_ComparisonOperators_04.ps1 2016/07/01 15:19:35
013_PS_ComparisonOperators_05.ps1 2016/07/01 15:21:05
< 省略 6gt;
[ 目次 ]
- $DNS = (Test-Connection www.google.com -quiet)
次の事例を実行してみよう。
#!/usr/bin/pwsh
# PowerShell Script to Test An Internet Connection
$DNS = (Test-Connection www.google.com -quiet)
If($DNS -eq "True") {Write-Host "The internet is available"}
ElseIf($DNS -ne "True") {Restart-Service dnscache}
4 行目では、Test-Connection で、www.google.com への接続を確認します。
5 〜 6 行で、接続状況に応じて、処理を振り分けています。
それでは、実行してみましょう。下記のような結果が得られます。
Pinging www.google.com [172.217.161.68] with 32 bytes of data:
Reply from 172.217.161.68: bytes=0 time=16ms TTL=
Reply from 172.217.161.68: bytes=0 time=12ms TTL=
Reply from 172.217.161.68: bytes=0 time=12ms TTL=
Reply from 172.217.161.68: bytes=0 time=18ms TTL=
Ping complete.
The internet is available
Test-Connection と言うからには、ソケット通信しているのかと思ったが、ping コマンドを起動しているのかよ。かなり、素人の集団だろうかな? 少しましな開発者なら、ping の出力を隠蔽できるでしょう。
[ 目次 ]
- $file.name + "`t " +$file.length + "`t " + $file.LastAccessTime
次の事例を実行してみよう。
#!/usr/bin/pwsh
# PowerShell Comparison Operator -eq in Foreach Loop
"File Name " + "`t Size" +"`t Last Accessed"
foreach ($file in Get-Childitem)
{If ($file.extension -eq ".txt")
{
$file.name + "`t " +$file.length + "`t " + $file.LastAccessTime
}
}
カレントディレクトリの拡張子が ".txt" であるものを抽出して、タブ区切りで表示します。
"`t" は、タブにエスケープします。
それでは、実行してみましょう。下記のような結果が得られます。
File Name Size Last Accessed
011_PS_BuiltInCmdlets_01.ps1 259 09/04/2019 16:41:36
011_PS_BuiltInCmdlets_02.ps1 87 09/04/2019 18:01:51
012_PS_CompareObject_01.ps1 317 09/05/2019 10:27:55
012_PS_CompareObject_02.ps1 281 09/05/2019 10:37:54
012_PS_CompareObject_03.ps1 325 09/05/2019 11:01:01
012_PS_CompareObject_04.ps1 320 09/05/2019 12:44:30
012_PS_CompareObject_05.ps1 80 09/05/2019 13:09:33
013_PS_ComparisonOperators_01.ps1 314 09/05/2019 13:40:37
013_PS_ComparisonOperators_02.ps1 229 09/05/2019 13:55:39
013_PS_ComparisonOperators_03.ps1 268 09/05/2019 14:38:25
013_PS_ComparisonOperators_04.ps1 156 08/31/2019 13:43:03
013_PS_ComparisonOperators_05.ps1 70 08/31/2019 13:43:03
013_PS_ComparisonOperators_06.ps1 189 08/31/2019 13:43:03
< 省略 >
[ 目次 ]
- $Block | Where-Object {$_.length -gt 2MB}
次の事例を実行してみよう。
#!/usr/bin/pwsh
# Clear-Host
$FileSource ="./"
$Block = Get-ChildItem -path $Filesource -Recurse
$Block | Where-Object {$_.length -gt 20000 }
"F:\pdf_buff\" から再帰的にアイテムを検索取得して、ファイルサイズが 2MB 越えのアイテムを表示します。
それでは、実行してみましょう。下記のような結果が得られます。
Directory: /xxxx/PSUX-Support/PS_ref_02
Mode LastWriteTime Length Name
---- ------------- ------ ----
------ 2019/09/05 14:44 148539 _index.html
------ 2016/07/01 17:43 23942 016_PS_Drives_03.txt
------ 2016/07/02 9:33 23942 017_PS_NewPSDrive_01.txt
------ 2016/07/02 10:20 219764 017_PS_NewPSDrive_04_1.txt
------ 2016/07/02 10:19 116046 017_PS_NewPSDrive_04.txt
------ 2016/07/02 14:01 38636 018_PS_GetPSDrive_07.txt
------ 2016/07/02 14:29 28688 019_PS_GetEventlog_07.txt
------ 2016/07/02 14:31 380608 019_PS_GetEventlog_08.txt
------ 2016/07/02 15:03 52722 020_PS_WinEvent_08.txt
------ 2016/07/28 17:13 76846 ww01902.txt
Directory: /xxxx/PSUX-Support/PS_ref_02/scripts
Mode LastWriteTime Length Name
---- ------------- ------ ----
------ 2016/07/02 10:19 116046 017_PS_NewPSDrive_04.txt
[ 目次 ]
- For ( $i = 12; $i -le 144; $i+=12 ) { $i }
次の事例を実行してみよう。
#!/usr/bin/pwsh
# ----< for loop >----
For ( $i = 12; $i -le 144; $i+=12 ) { $i }
For ループのサンプルです。
それでは、実行してみましょう。下記のような結果が得られます。
12
24
36
48
60
72
84
96
108
120
132
144
[ 目次 ]
- Get-WmiObject -List | Where-Object {$_.name -Like "*Adapter"}
次の事例を実行してみよう。
#!/usr/bin/pwsh
# PowerShell -Like Operator
# Author: H.Kamifuji
# Version Sep 2019 tested on PowerShell v 6.2.2
# Clear-Host
Get-WmiObject -List | Where-Object {$_.name -Like "*Adapter"}
WMI オブジェクトを取得して、最後尾が "*Adapter" であるものを表示します。
それでは、実行してみましょう。下記のような結果が得られます。
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_02/013_PS_ComparisonOperators_06.ps1:8 char:1
+ Get-WmiObject -List | Where-Object {$_.name -Like "*Adapter"}
+ ~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Get-WmiObject:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Get-WmiObject は、サポートされていない。
[ 目次 ]
- Get-WmiObject -List | Where-Object {$_.name -Like "*Adapter*"}
次の事例を実行してみよう。
#!/usr/bin/pwsh
# Clear-Host
Get-WmiObject -List | Where-Object {$_.name -Like "*Adapter*"}
WMI オブジェクトを取得して、途中でも "*Adapter*" であるものを表示します。
それでは、実行してみましょう。下記のような結果が得られます。
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_02/013_PS_ComparisonOperators_07.ps1:8 char:1
+ Get-WmiObject -List | Where-Object {$_.name -Like "*Adapter*"}
+ ~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Get-WmiObject:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Get-WmiObject は、サポートされていない。
[ 目次 ]
- Get-WmiObject -List | Where-Object {$_.name -Match "Adapter"}
次の事例を実行してみよう。
#!/usr/bin/pwsh
# PowerShell example to check with -Match
# Author: Guy Thomas
# Version September 2011 tested on PowerShell v 2.0
# Version July 2016 tested on PowerShell v 5.0
# Clear-Host
Get-WmiObject -List | Where-Object {$_.name -Match "Adapter"}
WMI オブジェクトを取得して、途中でも "*Adapter*" であるものを表示します。
結果は、前項と同じです。
それでは、実行してみましょう。下記のような結果が得られます。
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_02/013_PS_ComparisonOperators_08.ps1:8 char:1
+ Get-WmiObject -List | Where-Object {$_.name -Match "Adapter"}
+ ~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Get-WmiObject:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Get-WmiObject は、サポートされていない。
[ 目次 ]
- where-Object {$_.name -NotLike "CIM*" -And $_.name -NotLike "__*"}
次の事例を実行してみよう。
#!/usr/bin/pwsh
# PowerShell example to list demonstrate -NotLike with -And
# Author: Guy Thomas
# Version September tested on PowerShell v 2
# Version July 2016 tested on PowerShell v 5.0
# Clear-Host
$WMI = Get-WmiObject -List `
| where-Object {$_.name -NotLike "CIM*" -And $_.name -NotLike "__*"}
$WMI
Write-Host `n $WMI.count "WMI objects not contain CIM or __"
WMI オジェクトを取得して、先頭が CIM でも __ でもないものを表示します。
それでは、実行してみましょう。下記のような結果が得られます。
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_02/013_PS_ComparisonOperators_09.ps1:8 char:8
+ $WMI = Get-WmiObject -List `
+ ~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Get-WmiObject:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
0 WMI objects not contain CIM or __
Get-WmiObject は、サポートされていない。
[ 目次 ]
- $WMI = Get-WmiObject -List | Where-Object {$_.name -Match "network"}
次の事例を実行してみよう。
#!/usr/bin/pwsh
# 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."
WMI オブジェクトを取得して、Name に "network" が含まれるものを表示します。
6行目では、表示した個数を表示します。
それでは、実行してみましょう。下記のような結果が得られます。
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_02/013_PS_ComparisonOperators_10.ps1:6 char:8
+ $WMI = Get-WmiObject -List | Where-Object {$_.name -Match "network"}
+ ~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Get-WmiObject:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
0 WMI objects contain the word network.
Get-WmiObject は、サポートされていない。
[ 目次 ]
- $WMI = Get-WmiObject -List | Where-Object {$_.name -Like "*network"}
次の事例を実行してみよう。
#!/usr/bin/pwsh
# 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."
WMI オブジェクトを取得して、最後尾が "*network" であるものを表示します。
それでは、実行してみましょう。下記のような結果が得られます。
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_02/013_PS_ComparisonOperators_11.ps1:6 char:8
+ $WMI = Get-WmiObject -List | Where-Object {$_.name -Like "*network"}
+ ~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Get-WmiObject:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
0 WMI objects contain the word network.
Get-WmiObject は、サポートされていない。
[ 目次 ]
- $WMI = Get-WmiObject -List | Where-Object {$_.name -Like "*network*"}
次の事例を実行してみよう。
#!/usr/bin/pwsh
# 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."
WMI オブジェクトを取得して、途中でも "*Adapter*" であるものを表示します。
結果は、前々項と同じです。
それでは、実行してみましょう。下記のような結果が得られます。
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_02/013_PS_ComparisonOperators_11_1.ps1:6 char:8
+ $WMI = Get-WmiObject -List | Where-Object {$_.name -Like "*network*"}
+ ~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Get-WmiObject:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
0 WMI objects contain the word network.
Get-WmiObject は、サポートされていない。
[ 目次 ]
- If($_.name -Match "System") { Write-Host $($_.name) ; $i++ }
次の事例を実行してみよう。
#!/usr/bin/pwsh
# ----< -Match のサンプル >----
# Clear-Host
$i=0
Get-WmiObject -List | ForEach-Object {
If($_.name -Match "System") {
Write-Host $($_.name) ; $i++
}
}
"There are $i objects containing System"
WMI オブジェクトを取得しながら name に "System" が含まれるものを表示と個数をカウントします。
9行目では、オブジェクトの個数を表示しています。
それでは、実行してみましょう。下記のような結果が得られます。
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_02/013_PS_ComparisonOperators_12.ps1:6 char:1
+ Get-WmiObject -List | ForEach-Object {
+ ~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Get-WmiObject:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
There are 0 objects containing System
Get-WmiObject は、サポートされていない。
[ 目次 ]
- Where-Object {$_.name -NotMatch "CIM" -And $_.name -NotMatch "__"}
次の事例を実行してみよう。
#!/usr/bin/pwsh
# 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 __"
WMI オブジェクトを取得して、Name に CIM も __ も含まれないものを表示します。
7行目では、表示したオブジェクトの個数を表示しています。
それでは、実行してみましょう。下記のような結果が得られます。
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_02/013_PS_ComparisonOperators_13.ps1:6 char:8
+ $WMI = Get-WmiObject -List |`
+ ~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Get-WmiObject:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
0 objects containing WMI, but not CIM or __
Get-WmiObject は、サポートされていない。
[ 目次 ]
- $Guy -Match "\s"
次の事例を実行してみよう。
#!/usr/bin/pwsh
# -Match Character Class Example
$Guy ="Babe Ruth"
$Guy -Match "\s"
$Guy 変数にスペース文字が含まれるかを調べ結果を返します。
含まれている時には、True が、含まれていない時には、False が返します。
それでは、実行してみましょう。下記のような結果が得られます。
[ 目次 ]
- [char[]]"rd2cp30" -Match "\d"
次の事例を実行してみよう。
#!/usr/bin/pwsh
# PowerShell example to extract numbers from a string
[char[]]"rd2cp30" -Match "\d"
文字列の内、数字(0-9) かを調べて、1文字毎に表示します。
それでは、実行してみましょう。下記のような結果が得られます。
[ 目次 ]
- $ArraySimple -Contains "Flat"
次の事例を実行してみよう。
#!/usr/bin/pwsh
# PowerShell -Contains Operator
# Clear-Host
$ArraySimple =@("House","Flat","Bungalow")
$ArraySimple -Contains "Flat"
文字列配列中に、"Flat" と一致する要素があるかを調べます。
含まれている時には、True が、含まれていない時には、False が返します。
それでは、実行してみましょう。下記のような結果が得られます。
[ 目次 ]
- $ArraySimple -Contains "Flat"( 2 / 2 )
次の事例を実行してみよう。
#!/usr/bin/pwsh
# PowerShell -Contains Operator
# Clear-Host
$ArraySimple =@("House","Flats","Bungalow")
$ArraySimple -Contains "Flat"
文字列配列中に、"Flat" と一致する要素があるかを調べます。
含まれている時には、True が、含まれていない時には、False が返します。
この事例では、"Flats" と "Flat" と s の有無で異なるので False が返されます。
それでは、実行してみましょう。下記のような結果が得られます。
[ 目次 ]
- $Collection -Contains "Paula"( 不一致 )
次の事例を実行してみよう。
#!/usr/bin/pwsh
# PowerShell -Contains
$Collection = "Peter Smith", "Paula Harris", "Joan Evans"
$Collection -Contains "Paula"
文字列配列中に、"Paula" と一致する要素があるかを調べます。
含まれている時には、True が、含まれていない時には、False が返します。
この事例では、"Paula" と "Paula Harris" と s の有無で異なるので False が返されます。
それでは、実行してみましょう。下記のような結果が得られます。
[ 目次 ]
- $Check = (Import-Csv $File)."name"; $Check -Contains "BFE"
次の事例を実行してみよう。
#!/usr/bin/pwsh
# PowerShell -Contains Operator
# Clear-Host
$File = ".\serviceslist2.csv"
$Check = (Import-Csv $File)."name"
$Check -Contains "BFE"
".\serviceslist2.csv" ファイルから "name" 列を取得して、"BFE" と一致するデータがあるかを調べます。
それでは、実行してみましょう。下記のような結果が得られます。
[ 目次 ]
- $Check = (Import-Csv $File)."name"; $Check -NotContains "BFE"
次の事例を実行してみよう。
#!/usr/bin/pwsh
# PowerShell -Contains Operator
# Clear-Host
$File = ".\serviceslist2.csv"
$Check = (Import-Csv $File)."name"
$Check -NotContains "BFE"
".\serviceslist2.csv" ファイルから "name" 列を取得して、"BFE" と一致しないデータがあるかを調べます。
それでは、実行してみましょう。下記のような結果が得られます。
|