Powershell: Logging assistance


I need to restart a service on a few hundred servers, and I want to be able to log success/failures when stopping/starting a service. The servers are 2008r2. I have the following code below, and I need help with logging results to a file, the success/failure of starting and stopping the service.  Thanks very much for your assistance.

$serverlist=gc ./servers.txt
foreach($server in $serverlist) {
$service = Get-WmiObject -ComputerName $server -Class Win32_Service -Filter "Name='frs'"
            Write-Host "  Stopping frs service."
               $service.StopService() | Out-Null
              $service.StartService() | Out-Null
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

The easiest thing to do is use the transcription function..

Something like this:
function restart-servicelist {
          [parameter(position = 0, ValueFromPipeLineByName = $true)]
          [string]$LogPath = "$env:temp\ServiceRestart.log",
          [parameter(position = 1, ValueFromPipeLineByName = $true)]
     start-transcript -path $LogPath -Append

     $ServerList = get-content -path $ServerList
     Write-Verbose "`$ServerList is $($ServerList -split '\r\n')"

     Write-Verbose 'Entering the loop to process `$ServerList'

     foreach ($server in $ServerList) {
          Write-verbose "Processing server ($server)"

          $Service = Get-WMIObject -ComputerName $server -Class Win32_Service -filter "Name='frs'"
          Write-verbose "Stopping the FRS service on $server"
          Write-Verbose "Starting the FRS service on $server"

restart-servicelist -LogFile "$env:temp\RestartService.log" -ServerList .\ServerList.txt

Open in new window

Although, I am curious why you didn't want to use the restart-service powershell function..


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Parity123Author Commented:
I would like to capture any errors during stop and start service
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.