【发布时间】: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