Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PowerShell服务-输出读数不正确_Powershell_Logging_Service_Output - Fatal编程技术网

PowerShell服务-输出读数不正确

PowerShell服务-输出读数不正确,powershell,logging,service,output,Powershell,Logging,Service,Output,正在尝试编写执行以下操作的PowerShell脚本: 寻找服务存在 如果找到,请检查服务状态 报告服务状态 如果服务状态未运行,则启动服务 报告服务状态 问题:如果我运行一次脚本,最终的服务状态将为running,但之前打印的消息是无法启动服务。如果我再次运行该脚本,它将跳转并显示服务已启动,但最终状态为stopped 当前代码: # Setting variables $date = Get-Date # setting date $LogFile = "$env:UserProfile\D

正在尝试编写执行以下操作的PowerShell脚本:

  • 寻找服务存在
  • 如果找到,请检查服务状态
  • 报告服务状态
  • 如果服务状态未运行,则启动服务
  • 报告服务状态
  • 问题:如果我运行一次脚本,最终的服务状态将为running,但之前打印的消息是无法启动服务。如果我再次运行该脚本,它将跳转并显示服务已启动,但最终状态为stopped

    当前代码:

    # 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
    
    # 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
    =========================================================================
    
    运行2:

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

    以下是更正的代码:

    # 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
    
    # 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 = Get-Service -Name $ServiceName
    

    $arrService.Status
    现在反映了开始执行任何操作之前服务的状态-让我们想象一下,该状态是
    已停止

    然后,在脚本的后面:

    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
           FinalServiceStatus
    }
    else{
        Write-Output "Error: '$ServiceName' service could not be started..." | Out-File $LogFile -append
        Write-Output " " | Out-File $LogFile -append
        FinalServiceStatus
    }
    
    这部分工作与预期的一样-启动脚本时服务没有运行,所以现在就可以启动了,太棒了

    然后是脚本中有问题的部分:

    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
           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
    变量:

    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"
    }
    

    仔细看看你一开始做了什么:

    $arrService = Get-Service -Name $ServiceName
    

    $arrService.Status
    现在反映了开始执行任何操作之前服务的状态-让我们想象一下,该状态是
    已停止

    然后,在脚本的后面:

    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
           FinalServiceStatus
    }
    else{
        Write-Output "Error: '$ServiceName' service could not be started..." | Out-File $LogFile -append
        Write-Output " " | Out-File $LogFile -append
        FinalServiceStatus
    }
    
    这部分工作与预期的一样-启动脚本时服务没有运行,所以现在就可以启动了,太棒了

    然后是脚本中有问题的部分:

    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
           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
    变量:

    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"
    }
    

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


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


    $arrService.Status
    不会在不同的if/else语句之间更改,它在整个脚本中保持不变。再次运行
    Get Service
    而不是检查,并且“旧”变量值
    $arrService.Status
    不会在不同的if/else语句之间更改,它在整个脚本中保持不变。再次运行
    Get Service
    ,而不是检查和“旧”变量值谢谢您,@Mathias。我在你的帮助下成功了。完全忽略了这一点,但绝对理解这可能是一个问题。-passthru开关的触感也很好:)。现在,如果我能得到正确的格式…这是一个不同的问题(请随意问一个新的)。提示:不要使用
    格式列表
    ,它会添加不需要的换行符。明白了。谢谢你在解决最大问题上的帮助@Mathias。谢谢你,Mathias。我在你的帮助下成功了。完全忽略了这一点,但绝对理解这可能是一个问题。-passthru开关的触感也很好:)。现在,如果我能得到正确的格式…这是一个不同的问题(请随意问一个新的)。提示:不要使用
    格式列表
    ,它会添加不需要的换行符。明白了。感谢您帮助解决最大的问题@Mathias。