【问题标题】:Script to enable services one after the other, waiting for the previous one to start fully脚本依次启用服务,等待前一个完全启动
【发布时间】:2015-08-19 17:20:26
【问题描述】:

我正在尝试编写代码来一个接一个地启动服务列表,直到第一个服务有机会启动后才能启动下一个服务。

# Define System Variables
$workingdirectory="c:\powershell\"
$currentDate = date -uformat "%d%m%y-%H%M"
$logFile = ($workingdirectory+"StartLog-$currentDate.txt")
$tslist=IMPORT-CSV($workingdirectory+"start.csv")

#Logwrite function to append to log file
Function logwrite
{
   Param ([string]$logstring)
   Add-content $Logfile -value $logstring
}

#Write current date to log file
logwrite ("Starting ESB Services")
$startlogdate = Get-Date -Format "MM-dd-yyyy - hh:mm:ss"
logwrite ($startlogdate)
logwrite ("-----------------")

#runs through each entry in the CSV file defined by $tslist
ForEach ($_.ts in $tslist)
{
    $svc=get-service $_.ts
    logwrite ("Starting service -", $_.ts)
    start-service $svc.name

    #if the 2nd column is set to true then we need to wait until it starts
    if ($_.wait -eq "true")
    {
        logwrite "waiting for",$_.ts,"to be started"

        #Waits for the service to get to status of Running or time taken to reach 5 minutes
        $svc.waitforstatus('Running','00:05:00')

        #Check that the service is running
        if ($svc.status -ne "Running")
        {   
            logwrite $_.ts, "Failed to start service within 4 Minutes"
        }
        else
        {
            logwrite $_.ts, "Service started successfully"
        }
    }
    else
    {

    #Wait 5 seconds between each service startup
    start-sleep -s 5

        #Check that the service is running
        if ($svc.status -ne "Running")
        {   
            logwrite $_.ts, "Failed to start service"
        }
        else
        {
            logwrite $_.ts, "Service started successfully"
        }
    }
logwrite "-----------------"
}
#Write end date to the log file
logwrite "Script finished"
$stoplogdate = Get-Date -Format "MM-dd-yyyy - hh:mm:ss"
logwrite ($stoplogdate)

我遇到的问题是,如果我使用另一个类似这样的 powershell 脚本查询服务状态,则根据此代码立即启动服务设置为 正在运行

$svc= get-service "service name"
write-host $svc.status

然后我返回值 Startpending,我尝试插入另一个

$svc=get-service $_.ts

在命令后立即启动服务但服务状态仍然认为它处于正在运行的状态。

知道为什么会这样吗?

【问题讨论】:

    标签: windows powershell service status


    【解决方案1】:

    我做了一些细微的修改,但如果您可以提供包含在 csv 文件中的标题。

    # Define System Variables
    $workingdirectory = "c:\powershell\"
    $currentDate = date -uformat "%d%m%y-%H%M"
    $logFile = ($workingdirectory + "StartLog-$currentDate.txt")
    $tslist = IMPORT-CSV($workingdirectory + "start.csv")
    
    #Logwrite function to append to log file
    Function logwrite
    {
        Param ([string]$logstring)
        Add-content $Logfile -value $logstring
    }
    
    #Write current date to log file
    logwrite ("Starting ESB Services")
    $startlogdate = Get-Date -Format "MM-dd-yyyy - hh:mm:ss"
    logwrite ($startlogdate)
    logwrite ("-----------------")
    
    #runs through each entry in the CSV file defined by $tslist
    ForEach ($ts in $tslist)
    {
        $svc = get-service $ts.ts
        logwrite ("Starting service -", $ts.ts)
        start-service $svc.name
    
        #if the 2nd column is set to true then we need to wait until it starts
        if ($ts.wait -eq "true")
        {
            logwrite "waiting for", $ts.ts, "to be started"
    
            #Waits for the service to get to status of Running or time taken to reach 5 minutes
            $svc.waitforstatus('Running', '00:05:00')
    
            #Check that the service is running
            if ($svc.status -ne "Running")
            {
                logwrite $ts.ts, "Failed to start service within 4 Minutes"
            }
            else
            {
                logwrite $ts.ts, "Service started successfully"
            }
        }
        else
        {
    
            #Wait 5 seconds between each service startup
            start-sleep -s 5
    
            #Check that the service is running
            if ($svc.status -ne "Running")
            {
                logwrite $ts.ts, "Failed to start service"
            }
            else
            {
                logwrite $ts.ts, "Service started successfully"
            }
        }
        logwrite "-----------------"
    }
    #Write end date to the log file
    logwrite "Script finished"
    $stoplogdate = Get-Date -Format "MM-dd-yyyy - hh:mm:ss"
    logwrite ($stoplogdate)
    

    【讨论】:

    • 我设法通过删除等待检查并使用 refresh() 命令刷新状态来解决此问题。感谢您的调查:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-13
    • 2021-09-27
    • 1970-01-01
    • 2017-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多