sltechadmin
sltechadmin

Reputation: 41

Restart-Service output to file

Using Task Scheduler I am running a PS script to restart selected Windows Services using Restart-Service. For troubleshooting issues I'd like to write the output to a log file so we can make sure the service did restart. For the life of me I can't get the output file to write anything just creates the file in date format, but no contents. THank you

Edit: OG Script

Restart-Service Printer Spooler -Force | Out-File c:\scripts\test3.txt 

If I add -PassThru I get an output but the output is pretty bare bones. Would like to log steps of the Service Controller.

Restart-Service Printer Spooler -Force -PassThru | Out-File c:\scripts\test3.txt

Upvotes: 0

Views: 7304

Answers (2)

BobFrankly
BobFrankly

Reputation: 49

Provided the service you're restarting talks to the eventlogs, I'd grab the data from there and log it. Or leave it in there and grab it as needed. If you want to output it, this is one approach:

$date = (get-date).AddMinutes(-5)
$serviceData = Get-Service wersvc 
restart-service $serviceData
$eventData = Get-Winevent -FilterHashtable @{ LogName = 'System'; StartTime = $date; ID = 7036} | ? {$_.message -match $serviceData.DisplayName}
$eventData | Out-File C:\logs\filename.txt

Upvotes: 0

Eric
Eric

Reputation: 239

$logFile = "C:\Windows\Temp\out.txt"
$serviceName = "serviceName"

Restart-Service $serviceName -Verbose *> $logFile

The -Verbose switch gives you detailed start/stop attempt information

*> Redirects all command output to the log file.

Upvotes: 3

Related Questions