how to log the powershellactivity

I am running a PS script to stop and restart Services using Restart-Service. For troubleshooting issues I’d like to write the output of every activity to a log file so we can make sure the services stopped and restart.

please find the code:

$mailprops= @{

From = 'operating.system@abc.com'

To = 'asdf@abc.com'

Subject = 'test'

Attachment = $logfileSmtpServer = '127.0.0.1'
}

'Stopping services' | Out-File $logfile

Stop-Service '3456','2354' 'Services stopped' | Out-File $logfile -append

Restart-computer QY34-Force 'Remote computer sent restart command' | Out-File $logfile -append

Send-MailMessage @mailprops

can any one suggest me how to fix this?

Could you please format your code as code using the code tag button “pre” on the edit bar of the post editor? Thanks

You could use the output of Get-Service to show the change in the status …

$mailprops = @{
From = ‘operating.system@abc.com’
To = ‘asdf@abc.com’
Subject = ‘test’
Attachment = $logfile
SmtpServer = ‘127.0.0.1’
}

‘Stopping services - status before’ | Out-File -FilePath $logfile
Get-Service -Name ‘3456’, ‘2354’ | Out-File -FilePath $logfile -Append
Stop-Service ‘3456’, ‘2354’
‘Stopping services - status after’ | Out-File -FilePath $logfile -Append
Get-Service -Name ‘3456’, ‘2354’ | Out-File -FilePath $logfile -Append
Restart-Computer -ComputerName QY34 –Force
‘Remote computer sent restart command’ | Out-File -FilePath $logfile -Append

Send–MailMessage @mailprops

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.host/start-transcript?view=powershell-6

I agree with Sam. Start-Transcript is a good way to log the PowerShell session when a script is running.

Start-Transcript -Path C:\temp\script.log
# Code goes here
Stop-Transcript

pwshliquori

[quote quote=149936]https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.host/start-transcript?view=powershell-6

[/quote]

Transcripts are useful but you end up getting much more info than what might be necessary.

 

As for the script, you may want to also look at building in some form of error handling as well. This script as it stands right now may produce errors that are not logged. I would suggest reading up on how to use ‘Try / Catch / Finally’ and make this a function that you can feed parameters to, such as service or computer name, and even the path to the log file you want to keep.

Well, if you don’t want to use start-transcript, you can use Write-Log
This is a function that I wrote years back and is now part of the AZSBTools PS module (install-module AZSBTools)
It writes output to the console/and optionally to a log file - it also writes to the console in color lol
Example:

Install-Module AZSBTools -Force -AllowClobber 

$logfile = "$env:TEMP\Restarting services report - $(Get-Date -Format 'ddMMMMyyyy_hh-mm-ss_tt').txt"
$ComputerName = 'QY34'

$mailprops = @{
    From       = 'operating.system@abc.com'
    To         = 'asdf@abc.com'
    Subject    = 'test'
    Attachment = $logfile
    SmtpServer = '127.0.0.1' 
}

Write-Log 'Stopping services - status before:' Green $logfile
Write-Log (Get-Service -Name '3456', '2354' | Out-String).Trim() Cyan $logfile

Stop-Service '3456', '2354'
Write-Log 'Stopping services - status after:' Green $logfile
Write-Log (Get-Service -Name '3456', '2354' | Out-String).Trim() Cyan $logfile

Restart-Computer -ComputerName $ComputerName –Force 
Write-Log 'Remote computer',$ComputerName,'sent restart command' Green,Cyan,Green $logfile

Send–MailMessage @mailprops

It seems like gval19 does not like the answers he’s got here … :wink: :smiley:

how to log the powershellactivity.

The -passthru option returns an ServiceController object for those commands. You don’t need to run stop-service first.

PS C:\Users\admin> stop-service apple* -passthru

Status   Name               DisplayName
------   ----               -----------
Stopped  Apple Mobile De... Apple Mobile Device Service


PS C:\Users\admin> restart-service apple* -passthru

Status   Name               DisplayName
------   ----               -----------
Running  Apple Mobile De... Apple Mobile Device Service