我創建了一個腳本來停止和運行我們在其中一臺服務器上運行的一些自定義服務。我想在下午 5 點 (17:00) 停止服務,然后在凌晨 2 點 (02:00) 啟動它們。但是在星期五我希望服務運行到下午 5 點之后,他們在星期六凌晨 1 點停止。
我寫了下面的腳本。我還是 PowerShell 腳本的新手。我使用 switch case 來執行這些功能,但我認為必須有更好的方法來實作這一點。
# PowerShell script to stop and start services.
# Variable assignement
$OnOffSwitch = Get-Date -Format 'dddd_HH:mm';
$ServiceNames = @('CAPS8','CAPS9'); # Array to store service names.
function serivcesOff {
# Checks the all the service in the above $serviceNames.
foreach ($SeviceName in $ServiceNames) {
# Check for service status if stopped this will start it and goes throug the loop again.
# until status matches running.
if ((Get-Service -Name $ServiceName).Status -eq "Running") {
do{
Start-Service $ServiceName
Start-Sleep -Seconds 20
} until ((Get-Service $ServiceName).Status -eq "Stopped")
} Return "$($ServiceName) has been shutdown."
}
}
function serviceOn{
# Checks the all the service in the above $serviceNames.
foreach ($SeviceName in $ServiceNames) {
# Check for service status if stopped this will start it and goes throug the loop again.
# until status matches running.
if ((Get-Service -Name $ServiceName).Status -eq "Stopped") {
do{
Start-Service $ServiceName
Start-Sleep -Seconds 20
} until ((Get-Service $ServiceName).Status -eq "Running")
} Return "$($ServiceName) has been turned on."
}
}
#s?ndag - Sunday,
#mandag - Monday,
#tirsdag - Tuesday
#onsdag - Wednesday,
#torsdag - Thursday,
#fredag - Friday,
#l?rdag - Saturday
Switch ($OnOffSwitch){
'mandag_02:00' {serviceOn}
'mandag_17:00' {serivcesOff}
'tirsdag_02:00' {serviceOn}
'tirsdag_17:00' {serviceOff}
'onsdag_02:00' {serviceOn}
'onsdag_17:00' {serviceOff}
'torsdag_02:00' {serviceOn}
'l?rdag_01:00' {serviceOff}
default {Write-Output "Nothing matched"}
}
這是最好的方法還是我應該使用if ... else if ... else陳述句?有沒有更好的辦法?
uj5u.com熱心網友回復:
基本上是stackprotector所說的。使用本機任務調度程式將是最簡單的方法。無論如何,這里有一些技巧可以改進您的代碼。我通常會將它們放入評論中,但它不適合:
在函式中,servicesOff您檢查狀態是否正在運行以及是否是您Start-Service。我想你想在Stop-Service這里使用。 在啟動或停止服務之后
并不是絕對必要的sleep。這些 cmdlet 不被稱為異步,因此在服務完全啟動之前甚至不會啟動睡眠。但是,如果它們沒有啟動但導致錯誤,它們最終可能會處于另一種狀態而不是Running或Stopped,這會使您的腳本陷入嘗試啟動損壞服務的無限回圈中。
我會盡量避免腳本中的作業日等文化習俗。有時,腳本是在服務器的原始文化設定(英語)中執行的,而不是在您登錄時可用的文化(在“德語”服務器上的艱難方式)。在腳本的開頭定義文化:
[System.Threading.Thread]::CurrentThread.CurrentCulture = "da-DK"
或者使其文化中立:
(get-date).DayOfWeek將始終回傳英文作業日 ( [DayOfWeek].GetEnumNames())。盡可能使用[DateTime]物件 ( Get-Date) 而不是它代表的字串。由于[DateTime]精確到 CPU 的滴答聲,您不能直接將其作為-eq或在開關中進行比較。使用-lt/-gt或$Date.Hour
在你的腳本中,我會這樣做:
# PowerShell script to stop and start services.
# Variable assignement
$Date = Get-Date
$ServiceNames = @('CAPS8','CAPS9'); # Array to store service names.
function serivcesOff {
# Checks the all the service in the above $serviceNames.
foreach ($SeviceName in $ServiceNames) {
# Check for service status if stopped this will start it and goes throug the loop again.
# until status matches running.
if ((Get-Service -Name $ServiceName).Status -ne "Stopped") { # changed this to not stopped instead of running
do{
Stop-Service $ServiceName
#Start-Sleep -Seconds 20
} until ((Get-Service $ServiceName).Status -ne "Running") # changed this to not running instead of stopped
} Return "$($ServiceName) has been shutdown."
}
}
function serviceOn{
# Checks the all the service in the above $serviceNames.
foreach ($SeviceName in $ServiceNames) {
# Check for service status if stopped this will start it and goes throug the loop again.
# until status matches running.
if ((Get-Service -Name $ServiceName).Status -eq "Stopped") {
do{
Start-Service $ServiceName
#Start-Sleep -Seconds 20
} until ((Get-Service $ServiceName).Status -eq "Running")
} Return "$($ServiceName) has been turned on."
}
}
if ($Date.Hour -eq 2){
serviceOn
}elseif ($Date.DayOfWeek -ne "Friday"){ # or -notin "Friday","Sunday"
if ($Date.Hour -eq 17){
serviceOff
}
if ($Date.DayOfWeek -eq "Saturday" -and $Date.Hour -eq 1 ){
serviceOff
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/372593.html
