首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PowerShell服务-输出读取不正确

PowerShell服务-输出读取不正确
EN

Stack Overflow用户
提问于 2016-01-13 17:27:24
回答 2查看 1.4K关注 0票数 0

试图编写一个执行以下操作的PowerShell脚本:

  1. 寻找服务存在
  2. 如果找到,请检查服务状态。
  3. 报告服务状态
  4. 如果服务状态未运行,启动服务
  5. 报告服务状态

问题:--如果我只运行一次脚本,最终的服务状态将是运行,但之前打印的消息是它无法启动服务。如果我再次运行这个脚本,它会抛出触发器,并说服务已经启动,但是最终状态已经停止。

当前代码:

代码语言:javascript
复制
# 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:

代码语言:javascript
复制
=========================================================================
    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:

代码语言:javascript
复制
=========================================================================
    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
=========================================================================

==========================================

这里是更正的代码:

代码语言:javascript
复制
# 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

这里是经过更正的代码的输出:

代码语言:javascript
复制
=========================================================================
    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
=========================================================================
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-01-13 18:08:14

好好看看你刚开始做什么:

代码语言:javascript
复制
$arrService = Get-Service -Name $ServiceName

现在,在您开始执行任何操作之前,Stopped都会反映服务的状态--,让我们想象一下,状态是。

然后,在后面的脚本中:

代码语言:javascript
复制
if ($arrService.Status -ne "Running"){
    Start-Service $ServiceName | Out-File $LogFile -append
}

这个部分如预期的那样工作-当您启动脚本时,服务没有运行,所以它现在就要启动了,太棒了!

接下来是脚本中有问题的部分:

代码语言:javascript
复制
if ($arrService.Status -eq "Running"){
       Write-Output "$date - '$ServiceName' started..." | Out-File $LogFile -append
       Write-Output " " | Out-File $LogFile -append
       FinalServiceStatus
}
else{
    Write-Output "Error: '$ServiceName' service could not be started..." | Out-File $LogFile -append
    Write-Output " " | Out-File $LogFile -append
    FinalServiceStatus
}

由于$arrService.Status仍然具有与以前完全相同的值(即使服务本身现在可能已将其状态更改为Running),因此将执行else块,而不管服务是否已成功启动。

您需要再次调用Get-Service来获取服务的新值,或者(我个人最喜欢的)使用Start-Service -PassThru“更新”$arrService变量:

代码语言:javascript
复制
if($arrService.Status -ne 'Running')
{
    $arrService = Start-Service $ServiceName -PassThru
}

if($arrService.Status -eq 'Running')
{
    "Service is running" # although we don't know whether it was just started or already had been 
}
else
{
    "Service not running, starting must have failed"
}
票数 2
EN

Stack Overflow用户

发布于 2016-01-13 18:38:13

对于任何感兴趣的人,根据Mathias的建议,下面是我的代码看起来的样子(就逻辑而言):

代码语言:javascript
复制
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
           FinalServiceStatus
}

    if ($arrService.Status -ne "Running"){
        CurrentServiceStatus
        $arrService = Start-Service $ServiceName -PassThru
        if ($arrService.Status -eq "Running"){
           Write-Output "$date - '$ServiceName' started..." | Out-File $LogFile -append
           Write-Output " " | Out-File $LogFile -append
           FinalServiceStatus
        }
        elseif ($arrService.Status -ne "Running"){
           Write-Output "Error: '$ServiceName' service could not be started..." | Out-File $LogFile -append
           Write-Output " " | Out-File $LogFile -append
           FinalServiceStatus
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34773169

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档