I am trying to implement a powershell script and really need some assistance as I don't really know where to go with this.
Basically I have a file that contains the below:
Service1 , 0
Service2 , 150
Service3 , 0
Service4 , 210
Service1/2/3/4 indicates the service name, and the number represents the number of seconds delay after the service startup.
So service1 gets started, then a 0 second delay. Then service2 gets started, then a 150 second delay, then service3 gets started, then a 0 second delay. And so on.
Has anyone created a powershell script like this before or know of a way to pull this information into a Start-Service script via powershell?
Any help is appreciated.
Thanks, Dan
CodePudding user response:
A simple way of doing is:
$services = get-content C:\temp\sample.txt
foreach ($line in $services) {
$serviceName = $line.Split(",")[0]
$timeWait = $line.Split(",")[1]
Start-Service -Name $serviceName
Start-sleep $timeWait
$status = (Get-Service -Name $serviceName).Status
Write-Host "The current status of $($serviceName) is $status"
}
CodePudding user response:
Assuming the file format of the csv file is like you provided, you can use Import-Csv
, add in some headers with the -Header
parameter, and use ForEach-Object
to loop through the values and pipe those to commands accordingly to do what you prefer.
PowerShell
Import-csv -Path "C:\Folder\Services.csv" -Header "service","seconds" | ForEach-Object {
Start-Service -Name "$($_.service.trim())";
Start-Sleep -Seconds $_.seconds;
};