【发布时间】:2016-01-13 17:27:24
【问题描述】:
尝试编写执行以下操作的 PowerShell 脚本:
- 寻找服务存在
- 如果找到,检查服务状态
- 报告服务状态
- 如果服务状态不是运行启动服务
- 报告服务状态
问题:如果我运行一次脚本,最终服务状态会显示正在运行,但之前打印的消息是无法启动服务。如果我再次运行脚本,它会触发并说服务已启动,但最终状态已停止。
当前代码:
# Setting variables
$date = Get-Date # setting date
$LogFile = "$env:UserProfile\Desktop\Log.txt" # setting log file - change as needed
$ServiceName = "Spooler" # setting service name - change as needed
$arrService = Get-Service -Name $ServiceName
$arrServiceCheck = Get-Service -Name $ServiceName -ErrorAction SilentlyContinue -ErrorVariable NoService
<# =============== DO NOT CHANGE ANYTHING BELOW THIS POINT =============== #>
# Creating functions for re-use throughout script
function CurrentServiceStatus {
Write-Output "Status of '$ServiceName' service:" | Out-File $LogFile -append
Get-Service $ServiceName | Select Name,DisplayName,Status | Format-List | Out-File $LogFile -append
}
# Starting script operation
Write-Output "=========================================================================" | Out-File $LogFile
Write-Output " Starting '$ServiceName' Service Monitor Script on $date" | Out-File $LogFile -append
Write-Output "=========================================================================" | Out-File $LogFile -append
Write-Output " " | Out-File $LogFile -append
# Looking for service. If service was found, checking it's status. If status is not running, starting the service.
if ($arrServiceCheck){
Write-Output "'$ServiceName' service found on $env:ComputerName..." | Out-File $LogFile -append
Write-Output " " | Out-File $LogFile -append
ServiceStatus
if ($arrService.Status -ne "Running"){
Start-Service $ServiceName | Out-File $LogFile -append
}
if ($arrService.Status -eq "Running"){
Write-Output "$date - '$ServiceName' started..." | Out-File $LogFile -append
Write-Output " " | Out-File $LogFile -append
ServiceStatus
}
else{
Write-Output "Error: '$ServiceName' service could not be started..." | Out-File $LogFile -append
Write-Output " " | Out-File $LogFile -append
ServiceStatus
}
}
# If service was not found, making note of it to log file
if ($NoService){
Write-Output " " | Out-File $LogFile -append
Write-Output $NoService[0].exception.message | Out-File $LogFile -append
Write-Output " " | Out-File $LogFile -append
}
# Completing running of script
Write-Output "=========================================================================" | Out-File $LogFile -append
Write-Output " Finished '$ServiceName' Service Monitor Script on $date" | Out-File $LogFile -append
Write-Output "=========================================================================" | Out-File $LogFile -append
Write-Output " " | Out-File $LogFile -append
Write-Output " " | Out-File $LogFile -append
这是我的输出...
运行 1:
=========================================================================
Starting 'Spooler' Service Monitor Script on 01/13/2016 12:06:49
=========================================================================
'Spooler' service found on MW762OXI5K7M8D...
Current status of 'Spooler' service:
Name : Spooler
DisplayName : Print Spooler
Status : Stopped
01/13/2016 12:06:49 - 'Spooler' started...
Final status of 'Spooler' service:
Name : Spooler
DisplayName : Print Spooler
Status : Stopped
=========================================================================
Finished 'Spooler' Service Monitor Script on 01/13/2016 12:06:49
=========================================================================
运行 2:
=========================================================================
Starting 'Spooler' Service Monitor Script on 01/13/2016 12:15:58
=========================================================================
'Spooler' service found on MW762OXI5K7M8D...
Current status of 'Spooler' service:
Name : Spooler
DisplayName : Print Spooler
Status : Stopped
'Spooler' service could not be started...
Final status of 'Spooler' service:
Name : Spooler
DisplayName : Print Spooler
Status : Running
=========================================================================
Finished 'Spooler' Service Monitor Script on 01/13/2016 12:15:58
=========================================================================
===========================================强>
这是更正后的代码:
# Setting variables
$date = Get-Date # setting date
$LogFile = "$env:UserProfile\Desktop\NXLogMonitor\Logging\NXLogMonitor.txt" # setting log file - change as needed
$ServiceName = "Spooler" # setting service name - change as needed
$arrService = Get-Service -Name $ServiceName
$arrServiceCheck = Get-Service -Name $ServiceName -ErrorAction SilentlyContinue -ErrorVariable NoService
<# =============== DO NOT CHANGE ANYTHING BELOW THIS POINT =============== #>
# Creating functions for re-use throughout script
function ServiceStatus {
Write-Output "Status of '$ServiceName' service:" | Out-File $LogFile -append
Get-Service $ServiceName | Select Name,DisplayName,Status | Format-Table -AutoSize | Out-File $LogFile -append
}
# Starting script operation
Write-Output "=========================================================================" | Out-File $LogFile
Write-Output " Starting '$ServiceName' Service Monitor Script on $date" | Out-File $LogFile -append
Write-Output "=========================================================================" | Out-File $LogFile -append
Write-Output " " | Out-File $LogFile -append
# Looking for service. If service was found, checking it's status. If status is not running, starting the service.
if ($arrServiceCheck){
Write-Output "'$ServiceName' service found on $env:ComputerName..." | Out-File $LogFile -append
Write-Output " " | Out-File $LogFile -append
if ($arrService.Status -eq "Running"){
Write-Output "'$ServiceName' is already started..." | Out-File $LogFile -append
Write-Output " " | Out-File $LogFile -append
ServiceStatus
}
if ($arrService.Status -ne "Running"){
Write-Output "'$ServiceName' service is not started..." | Out-File $LogFile -append
Write-Output " " | Out-File $LogFile -append
ServiceStatus
$arrService = Start-Service $ServiceName -PassThru
if ($arrService.Status -eq "Running"){
Write-Output "$date - '$ServiceName' service started..." | Out-File $LogFile -append
Write-Output " " | Out-File $LogFile -append
ServiceStatus
}
elseif ($arrService.Status -ne "Running"){
Write-Output "Error: '$ServiceName' service could not be started..." | Out-File $LogFile -append
Write-Output " " | Out-File $LogFile -append
ServiceStatus
}
}
}
# If service was not found, making note of it to log file
if ($NoService){
Write-Output " " | Out-File $LogFile -append
Write-Output $NoService[0].exception.message | Out-File $LogFile -append
Write-Output " " | Out-File $LogFile -append
}
# Completing running of script
Write-Output "=========================================================================" | Out-File $LogFile -append
Write-Output " Finished '$ServiceName' Service Monitor Script on $date" | Out-File $LogFile -append
Write-Output "=========================================================================" | Out-File $LogFile -append
Write-Output " " | Out-File $LogFile -append
Write-Output " " | Out-File $LogFile -append
这是更正后代码的输出:
=========================================================================
Starting 'Spooler' Service Monitor Script on 01/13/2016 13:53:08
=========================================================================
'Spooler' service found on MW762OXI5K7M8D...
'Spooler' is already started...
Status of 'Spooler' service:
Name DisplayName Status
---- ----------- ------
Spooler Print Spooler Running
=========================================================================
Finished 'Spooler' Service Monitor Script on 01/13/2016 13:53:08
=========================================================================
【问题讨论】:
-
$arrService.Status在不同的 if/else 语句之间不会改变,它在整个脚本中保持不变。再次运行Get-Service,而不是检查和“旧”变量值
标签: powershell logging service output