windows - Script to enable services one after the other, waiting for the previous one to start fully -
i'm trying write code start list of services 1 after other, next service cannot start until first 1 has had chance start.
# 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 append log file function logwrite { param ([string]$logstring) add-content $logfile -value $logstring } #write current date log file logwrite ("starting esb services") $startlogdate = get-date -format "mm-dd-yyyy - hh:mm:ss" logwrite ($startlogdate) logwrite ("-----------------") #runs through each entry in csv file defined $tslist foreach ($_.ts in $tslist) { $svc=get-service $_.ts logwrite ("starting service -", $_.ts) start-service $svc.name #if 2nd column set true need wait until starts if ($_.wait -eq "true") { logwrite "waiting for",$_.ts,"to started" #waits service status of running or time taken reach 5 minutes $svc.waitforstatus('running','00:05:00') #check service running if ($svc.status -ne "running") { logwrite $_.ts, "failed start service within 4 minutes" } else { logwrite $_.ts, "service started successfully" } } else { #wait 5 seconds between each service startup start-sleep -s 5 #check service running if ($svc.status -ne "running") { logwrite $_.ts, "failed start service" } else { logwrite $_.ts, "service started successfully" } } logwrite "-----------------" } #write end date log file logwrite "script finished" $stoplogdate = get-date -format "mm-dd-yyyy - hh:mm:ss" logwrite ($stoplogdate)
the problem have on starting service according code set running, if query service status using powershell script this
$svc= get-service "service name" write-host $svc.status
then i'm returned value of startpending, i've tried inserting
$svc=get-service $_.ts
immediately after command start service service status still thinks it's @ state of running.
any idea why happening?
i've made slight amendments, if provide headers included in csv file.
# 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 append log file function logwrite { param ([string]$logstring) add-content $logfile -value $logstring } #write current date log file logwrite ("starting esb services") $startlogdate = get-date -format "mm-dd-yyyy - hh:mm:ss" logwrite ($startlogdate) logwrite ("-----------------") #runs through each entry in csv file defined $tslist foreach ($ts in $tslist) { $svc = get-service $ts.ts logwrite ("starting service -", $ts.ts) start-service $svc.name #if 2nd column set true need wait until starts if ($ts.wait -eq "true") { logwrite "waiting for", $ts.ts, "to started" #waits service status of running or time taken reach 5 minutes $svc.waitforstatus('running', '00:05:00') #check service running if ($svc.status -ne "running") { logwrite $ts.ts, "failed 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 service running if ($svc.status -ne "running") { logwrite $ts.ts, "failed start service" } else { logwrite $ts.ts, "service started successfully" } } logwrite "-----------------" } #write end date log file logwrite "script finished" $stoplogdate = get-date -format "mm-dd-yyyy - hh:mm:ss" logwrite ($stoplogdate)
Comments
Post a Comment