|
ここでは、Windows PowerShell's If Statement のサンプルを紹介します。
- If ($test) { "Execute when true" }
次の事例を実行してみよう。
#!/usr/bin/pwsh
# http://www.computerperformance.co.uk/powershell/powershell_if_statement.htm
# If (condition) {Do stuff}
# Another explanation would be
$test = "true"
If ($test) {
"Execute when true"
}
Else {
"Stop when false"
}
6 行目で、$test が True のときに、次の { } 内を実行します。False のときに、Else の { } 内を実行します。
それでは、実行してみましょう。下記のような結果が得られます。
[ 目次 ]
- If ($Number -gt 0) { "Bigger than zero" }
次の事例を実行してみよう。
#!/usr/bin/pwsh
# PowerShell If Statement Simple Example
$Number = 10
If ($Number -gt 0) {
"Bigger than zero"
}
3行目で、$Number が 0 より大きいときに { } 内を実行します。
それでは、実行してみましょう。下記のような結果が得られます。
[ 目次 ]
- $FileExists = Test-Path $ChkFile
次の事例を実行してみよう。
#!/usr/bin/pwsh
# Windows PowerShell example to check 'If File Exists'
$ChkFile = "./038_PS_if_Statement_03.ps1"
$FileExists = Test-Path $ChkFile
If ($FileExists -eq $True) {
Write-Host "./038_PS_if_Statement_03.ps1 exists"
}
4 〜 5 行で、"./038_PS_if_Statement_03.ps1" が存在するかを、$FileExists に格納します。
6 行目で、$FileExists が $True(存在する)の時は、{ } 内を実行します。
それでは、実行してみましょう。下記のような結果が得られます。
./038_PS_if_Statement_03.ps1 exists
[ 目次 ]
- If (-Not $Service) { }
次の事例を実行してみよう。
#!/usr/bin/pwsh
# PowerShell script to check whether a service is installed
# Clear-Host
$Name = "Alerter"
$Service = Get-Service -display $Name -ErrorAction SilentlyContinue
If (-Not $Service) {
$Name + " is not installed on this computer."
}
5 〜 6 行で、"Alerter" サービスを取得して、$Service に格納します。
7 行目では、-Not $Service が $True のときに { } 内を実行します。
-Not は、論理値を反転させる命令です。この事例では、$Service が $False または NULL のときに反転させて、$True になります。
それでは、実行してみましょう。下記のような結果が得られます。
Get-Service : The term 'Get-Service' 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_04/038_PS_if_Statement_04.ps1:6 char:12
+ $Service = Get-Service -display $Name -ErrorAction SilentlyContinue
+ ~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Get-Service:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Linux版では、Get-Service コマンドがサポートされていない。
[ 目次 ]
- If (-Not $Service) { } Else { }
次の事例を実行してみよう。
#!/usr/bin/pwsh
# PowerShell If statement to check whether a service is installed
# Clear-Host
$Name = "Print Spooler"
$Service = Get-Service -display $Name -ErrorAction SilentlyContinue
If (-Not $Service) {
$Name + " is not installed on this computer."
}
Else {
$Name + " is installed."
$Name + "'s status is: " + $service.Status
}
5 〜 6 行で、"Print Spooler" サービスを取得して、$Service に格納します。
7 行目では、-Not $Service が $True のときに { } 内を実行します。$False のときには、8〜11行の Else { } 内が実行されます。
-Not は、論理値を反転させる命令です。この事例では、$Service が $False または NULL のときに反転させて、$True になります。
それでは、実行してみましょう。下記のような結果が得られます。
Get-Service : The term 'Get-Service' 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_04/038_PS_if_Statement_05.ps1:6 char:12
+ $Service = Get-Service -display $Name -ErrorAction SilentlyContinue
+ ~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Get-Service:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Print Spooler is not installed on this computer.
Linux版では、Get-Service コマンドがサポートされていない。
[ 目次 ]
- $Pingy = Get-WmiObject Win32_PingStatus -f "Address='$Ip4th'"
次の事例を実行してみよう。
#!/usr/bin/pwsh
# PowerShell If Statement To Test Ip Addresses
$i = 1
$Ip = "192.168.1."
Write-Host "IP Address"
Write-Host "----------------------------------------"
Do {
$Ip4th = $Ip + $i
$Pingy = Get-WmiObject Win32_PingStatus -f "Address='$Ip4th'"
If ($Pingy.StatusCode -eq 0) {
"{0,-15} {1,6} {2,-12}" -f
$Pingy.Address, $Pingy.StatusCode," ON NETWORK"
}
else {
"{0,-15} {1,6} {2,-12}" -f
$Pingy.Address, $Pingy.StatusCode, " xxxxxxxxx"
}
$i++
} until ($i -eq 20)
IP Address を 192.168.1.1 から 192.168.1.19 まで、Win32_PingStatus を利用して、ネットワーク内で稼働しているか調べています。
2〜3行で、変数の初期化、4〜5行で、ヘッダの書き出しを行っています。
6〜19行で、$i を 1 から 19 まで、繰り返します。
7行目で、IP Address を整形し、8行目で、PingStatus を取得して、$Pingy に格納します。
9〜16行で、$Pingy.StatusCode を判別して、それぞれに対応した処理に分岐して、表示します。
それでは、実行してみましょう。下記のような結果が得られます。
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_04/038_PS_if_Statement_06.ps1:10 char:11
+ $Pingy = Get-WmiObject Win32_PingStatus -f "Address='$Ip4th'"
+ ~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Get-WmiObject:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
xxxxxxxxx
Linux版では、Get-WmiObject コマンドがサポートされていない。
[ 目次 ]
- If ($File -Match "The if Statement") { ... }
次の事例を実行してみよう。
#!/usr/bin/pwsh
# Help on PowerShell's if statements
# Clear-Host
$File = Get-Help about_if
If ($File -Match "The if Statement") {
"We have the correct help file"
}
5 行目では、about_if のヘルプを、$File に格納します。
6 〜 7 行で、$File に "The if Statement" が見つかれば、"We have the correct help file" と表示します。
それでは、実行してみましょう。下記のような結果が得られます。
Get-Help : Get-Help could not find about_if 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.
At /xxxx/PSUX-Support/PS_ref_04/038_PS_if_Statement_07.ps1:5 char:9
+ $File = Get-Help about_if
+ ~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceUnavailable: (:) [Get-Help], HelpNotFoundException
+ FullyQualifiedErrorId : HelpNotFound,Microsoft.PowerShell.Commands.GetHelpCommand
Linux版では、about_if が見つからないようだ!
[ 目次 ]
- If ($File -Match "The if Statement") { ... }; Else { ... }
次の事例を実行してみよう。
#!/usr/bin/pwsh
# Help on PowerShell's Else statements
$File = Get-Help about_if
If ($File -Match "The if Statement") {
"We have the correct help file"
}
Else {
"The string is wrong"
}
この事例は、前項と同じ結果が得られます。ただし、条件が成立しないときに処理する Else { ... } ブロックが6〜8行で定義されています。
それでは、実行してみましょう。下記のような結果が得られます。
Get-Help : Get-Help could not find about_if 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.
At /xxxx/PSUX-Support/PS_ref_04/038_PS_if_Statement_07.ps1:5 char:9
+ $File = Get-Help about_if
+ ~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceUnavailable: (:) [Get-Help], HelpNotFoundException
+ FullyQualifiedErrorId : HelpNotFound,Microsoft.PowerShell.Commands.GetHelpCommand
Linux版では、about_if が見つからないようだ!
[ 目次 ]
- If (-Not $Service) { ... }; Else { ... }
次の事例を実行してみよう。
#!/usr/bin/pwsh
# PowerShell script to check whether a service is installed
# Clear-Host
$Name = "Print Spooler"
$Service = Get-Service -display $Name -ErrorAction SilentlyContinue
If (-Not $Service) {
$Name + " is not installed on this computer."
}
Else {
$Name + " is installed."
$Name + "'s status is: " + $service.Status
}
5 〜 6 行で、"Print Spooler" のサービスを取得して、$Service に格納します。
7 〜 13 行で、サービスの有無で処理を判別しています。サービスが無いときの処理は、6行目で、定義されています。
サービスが存在するときの処理は、11 〜 12 行で、定義されています。
それでは、実行してみましょう。下記のような結果が得られます。
Get-Service : The term 'Get-Service' 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_04/038_PS_if_Statement_09.ps1:6 char:12
+ $Service = Get-Service -display $Name -ErrorAction SilentlyContinue
+ ~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Get-Service:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Print Spooler is not installed on this computer.
Linux版では、Get-Service コマンドがサポートされていない。
[ 目次 ]
- If (-Not $Service) { ... }; Else { ... }
次の事例を実行してみよう。
#!/usr/bin/pwsh
# PowerShell script to check if a service is -NOT installed
# Clear-Host
$Name = "Alerter"
$Service = Get-Service -display $Name -ErrorAction SilentlyContinue
If (-Not $Service) {
$Name + " is not installed on this computer."
}
Else {
"You probably have $Name, thus machine is Vista or W2K3"
}
5 〜 6 行で、"Alerter" のサービスを取得して、$Service に格納します。
7 〜 12 行で、サービスの有無で処理を判別しています。サービスが無いときの処理は、6行目で、定義されています。
サービスが存在するときの処理は、11 行目で、定義されています。
それでは、実行してみましょう。下記のような結果が得られます。
Get-Service : The term 'Get-Service' 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_04/038_PS_if_Statement_10.ps1:5 char:12
+ $Service = Get-Service -display $Name -ErrorAction SilentlyContinue
+ ~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Get-Service:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Alerter is not installed on this computer.
Linux版では、Get-Service コマンドがサポートされていない。
[ 目次 ]
- If($Fw -Match 'ok'){ .. }; ElseIf { .. }; Else { .. }
次の事例を実行してみよう。
#!/usr/bin/pwsh
# PowerShell ElseIf Example Using NetSh
# Clear-Host
Write-Host "Firewall configuration for $env:computername"
$Fw = NetSh advfirewall set currentprofile state on
$Fw
If($Fw -Match 'ok'){
Write-Host "$env:username's job is done"
}
ElseIf($Fw -Match 'requires elevation') {
Write-Host "Call for an administrator"
}
Else {
Write-Host "Nothing happened"
}
NetSh advfirewall set currentprofile state on
5 行目では、ヘッダを表示しています。
6 行目では、advfirewall の状態を取得して、$Fw に格納します。
$Fw に 'ok' があるときの処理は、9 行目で、定義されています。
$Fw に 'requires elevation' があるときの処理は、10行目で、定義されています。
どれでもないときの処理は、15 行目で、定義されています。
それでは、実行してみましょう。下記のような結果が得られます。
Firewall configuration for
NetSh : The term 'NetSh' 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_04/038_PS_if_Statement_11.ps1:6 char:7
+ $Fw = NetSh advfirewall set currentprofile state on
+ ~~~~~
+ CategoryInfo : ObjectNotFound: (NetSh:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Nothing happened
NetSh : The term 'NetSh' 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_04/038_PS_if_Statement_11.ps1:17 char:1
+ NetSh advfirewall set currentprofile state on
+ ~~~~~
+ CategoryInfo : ObjectNotFound: (NetSh:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Linux版では、NetSh コマンドがサポートされていない。
[ 目次 ]
- If ($File -Match "The if Statement") { ..}; ElseIf { .. }; Else { .. }
次の事例を実行してみよう。
#!/usr/bin/pwsh
# Help on PowerShell's ElseIf statements
# Clear-Host
$Item = "about_if"
$File = Get-Help $Item
If ($File -Match "The if Statement") {
"We have the correct help file, $Item"
}
ElseIf ($File.Length -lt 1) {
"Check file location"
}
Else {
"File exists, but does not contain text string"
}
3〜4行で、"about_if" のヘルプを、$File に格納します。
$File に "The if Statement" があるときの処理は、6行目で、定義されています。
$File.Length が 1 より小さいときの処理は、9行目で、定義されています。
どれでもないときの処理は、12行目で、定義されています。
それでは、実行してみましょう。下記のような結果が得られます。
Get-Help : Get-Help could not find about_if 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.
At /xxxx/PSUX-Support/PS_ref_04/038_PS_if_Statement_07.ps1:5 char:9
+ $File = Get-Help about_if
+ ~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceUnavailable: (:) [Get-Help], HelpNotFoundException
+ FullyQualifiedErrorId : HelpNotFound,Microsoft.PowerShell.Commands.GetHelpCommand
Linux版では、about_if が見つからないようだ!
[ 目次 ]
- Get-Help About_If -full
次の事例を実行してみよう。
#!/usr/bin/pwsh
Get-Help About_If -full
About_If のヘルプを表示します。
それでは、実行してみましょう。下記のような結果が得られます。
Get-Help : Get-Help could not find about_if 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.
At /xxxx/PSUX-Support/PS_ref_04/038_PS_if_Statement_07.ps1:5 char:9
+ $File = Get-Help about_if
+ ~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceUnavailable: (:) [Get-Help], HelpNotFoundException
+ FullyQualifiedErrorId : HelpNotFound,Microsoft.PowerShell.Commands.GetHelpCommand
Linux版では、about_if が見つからないようだ!
[ 目次 ]
|