|
ここでは、PowerShell If -Or Conditional Operator のサンプルを紹介します。
- If ($Service.Startmode -eq "Manual") { .. }
次の事例を実行してみよう。
#!/usr/bin/pwsh
# http://www.computerperformance.co.uk/powershell/powershell_if_or.htm
# PowerShell Script to change Startup to Automatic
# Clear-Host
$NameSrv = 'Spooler'
$Service = Get-WmiObject win32_service -filter "NAME = '$NameSrv'"
If ($Service.Startmode -eq "Manual") {
Set-Service $NameSrv -startuptype Automatic
}
$Service = Get-WmiObject win32_service -filter "NAME = '$NameSrv' "
$Service | Ft Name, Startmode -AutoSize
6 〜 7 行で、'Spooler' のサービスを取得して、$Service に格納します。
8 〜 10 行で、$Service.Startmode が "Manual" のときに、9 行目で、$Service.Startmode を Auto に設定します。
11 行目では、再び 'Spooler' のサービスを取得して、$Service に格納します。
12 行目では、$Service を Name, Startmode のテーブルに整形して、表示します。
それでは、実行してみましょう。下記のような結果が得られます。
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_05/041_PS_if-Or_ConditionalOperator_01.ps1:7 char:12
+ $Service = Get-WmiObject win32_service -filter "NAME = '$NameSrv'"
+ ~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Get-WmiObject:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
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_05/041_PS_if-Or_ConditionalOperator_01.ps1:11 char:12
+ $Service = Get-WmiObject win32_service -filter "NAME = '$NameSrv' "
+ ~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Get-WmiObject:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Linux版では、Get-WmiObject コマンドがサポートされていない。
[ 目次 ]
- If ($Service.Startmode -eq "Manual" -Or $Service.Startmode -eq "Disabled") { .. }
次の事例を実行してみよう。
#!/usr/bin/pwsh
# Clear-Host
$NameSrv = 'Spooler'
$Service = Get-WmiObject win32_service -filter "NAME = '$NameSrv' "
If ($Service.Startmode -eq "Manual" -Or $Service.Startmode -eq "Disabled") {
Set-Service $NameSrv -startuptype Automatic
}
$Service = Get-WmiObject win32_service -filter "NAME = '$NameSrv' "
$Service | Ft Name, Startmode -AutoSize
4 〜 5 行で、'Spooler' のサービスを取得して、$Service に格納します。
6 〜 8 行で、$Service.Startmode が "Manual" または "Disabled" のときに、5 行目で、$Service.Startmode を Auto に設定します。
9 行目では、再び 'Spooler' のサービスを取得して、$Service に格納します。
10 行目では、$Service を Name, Startmode のテーブルに整形して、表示します。
それでは、実行してみましょう。下記のような結果が得られます。
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_05/041_PS_if-Or_ConditionalOperator_02.ps1:9 char:12
+ $Service = Get-WmiObject win32_service -filter "NAME = '$NameSrv' "
+ ~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Get-WmiObject:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Linux版では、Get-WmiObject コマンドがサポートされていない。
[ 目次 ]
- if ($Service.Startmode -eq "Manual" -Or $Service.Startmode -eq "Disabled") { 実行 }
次の事例を実行してみよう。
#!/usr/bin/pwsh
# Clear-Host
$NameSrv = 'Spooler'
Set-Service $NameSrv -startupType manual
$Service = Get-WmiObject win32_service -filter "NAME = '$NameSrv'"
$Service | Ft Name, Startmode
if ($Service.Startmode -eq "Manual" -Or $Service.Startmode -eq "Disabled") {
Set-Service $NameSrv -startuptype Automatic
}
$Service = Get-WmiObject win32_service -filter "NAME = '$NameSrv' "
$Service | Ft Name, Startmode
4 〜5 行で、'Spooler' のサービスの Startmode を manual に設定しています。
6 行目で、'Spooler' のサービスを取得して、$Service に格納します。
7 行目では、$Service を Name, Startmode のテーブルに整形して、表示します。
8 〜 10 行で、$Service.Startmode が "Manual" または "Disabled" のときに、9 行目で、$Service.Startmode を Auto に設定します。
11 行目では、再び 'Spooler' のサービスを取得して、$Service に格納します。
12 行目では、$Service を Name, Startmode のテーブルに整形して、表示します。
それでは、実行してみましょう。下記のような結果が得られます。
Set-Service : The term 'Set-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_05/041_PS_if-Or_ConditionalOperator_03.ps1:6 char:1
+ Set-Service $NameSrv -startupType manual
+ ~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Set-Service:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
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_05/041_PS_if-Or_ConditionalOperator_03.ps1:7 char:12
+ $Service = Get-WmiObject win32_service -filter "NAME = '$NameSrv'"
+ ~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Get-WmiObject:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
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_05/041_PS_if-Or_ConditionalOperator_03.ps1:12 char:12
+ $Service = Get-WmiObject win32_service -filter "NAME = '$NameSrv' "
+ ~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Get-WmiObject:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Linux版では、Get-WmiObject と Set-Service コマンドがサポートされていない。
[ 目次 ]
- Start-Service 'Spooler'
次の事例を実行してみよう。
#!/usr/bin/pwsh
$Service = Get-WmiObject win32_service -filter "NAME = 'Spooler'"
if ($Service.Startmode -eq "Manual" -Or $Service.Startmode -eq "Disabled") {
Set-Service 'Spooler' -startuptype Automatic
}
Start-Service 'Spooler'
3 行目で、'Spooler' のサービスを取得して、$Service に格納します。
4 〜 6 行で、$Service.Startmode が "Manual" または "Disabled" のときに、5 行目で、$Service.Startmode を Auto に設定します。
7 行目で、'Spooler' のサービスを起動しています。
それでは、実行してみましょう。下記のような結果が得られます。
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_05/041_PS_if-Or_ConditionalOperator_04.ps1:4 char:12
+ $Service = Get-WmiObject win32_service -filter "NAME = 'Spooler'"
+ ~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Get-WmiObject:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Start-Service : The term 'Start-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_05/041_PS_if-Or_ConditionalOperator_04.ps1:8 char:1
+ Start-Service 'Spooler'
+ ~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Start-Service:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Linux版では、Get-WmiObject と Start-Service コマンドがサポートされていない。
[ 目次 ]
- If ($Calendar.day -eq '24' -And $Calendar.Month -eq '12') { .. }; ElseIf { .. }; Else { .. }
次の事例を実行してみよう。
#!/usr/bin/pwsh
# Clear-Host
$Calendar = Get-Date
If ($Calendar.day -eq '24' -And $Calendar.Month -eq '12') {
"Christmas Day"
}
ElseIf ($Calendar.day -eq '4' -And $Calendar.Month -eq '7') {
"4th of July"
}
Else {
"Today is not Christmas or the 4th of July"
}
4 行目で、今日の日時を取得して、$Calendar に格納します。
5 〜 7 行で、$Calendar.day が '24' かつ $Calendar.Month が '12' のときに、4行目を実行します。
7 〜9 行で、$Calendar.day が '4' かつ $Calendar.Month が '7' のときに、4行目を実行します。
何れでもない時には、9行目を実行します。
それでは、実行してみましょう。下記のような結果が得られます。
Today is not Christmas or the 4th of July
[ 目次 ]
- if ($Service.Startmode -eq "Manual") { .. }; ElseIf { .. }; Else { .. }
次の事例を実行してみよう。
#!/usr/bin/pwsh
# Clear-Host
$NameSrv = 'Spooler'
$Service = Get-WmiObject win32_service -filter "NAME = '$NameSrv'"
if ($Service.Startmode -eq "Manual") {
Set-Service $NameSrv -startuptype Automatic
}
ElseIf ($Service.Startmode -eq "Disabled") {
Set-Service $NameSrv -startuptype Automatic
}
$Service = Get-WmiObject win32_service -filter "NAME = '$NameSrv' "
$Service | Ft Name, Startmode -auto
4 〜 5 行で、'Spooler' のサービスを取得して、$Service に格納します。
6 〜 8 行で、$Service.Startmode が "Manual" のときに、7 行目で、$Service.Startmode を Auto に設定します。
9 〜 11 行で、$Service.Startmode が "Disabled" のときに、10 行目で、$Service.Startmode を Auto に設定します。
12 行目では、再び 'Spooler' のサービスを取得して、$Service に格納します。
13 行目では、$Service を Name, Startmode のテーブルに整形して、表示します。
それでは、実行してみましょう。下記のような結果が得られます。
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_05/041_PS_if-Or_ConditionalOperator_06.ps1:6 char:12
+ $Service = Get-WmiObject win32_service -filter "NAME = '$NameSrv'"
+ ~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Get-WmiObject:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
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_05/041_PS_if-Or_ConditionalOperator_06.ps1:13 char:12
+ $Service = Get-WmiObject win32_service -filter "NAME = '$NameSrv' "
+ ~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Get-WmiObject:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Linux版では、Get-WmiObject コマンドがサポートされていない。
|