Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

start-Transcript not capturing all output to log file..?

Tags:

I have the below code that goes through and gets scheduled tasks info and puts the output that occurs on the screen to a log file.

However, what I have noticed is that all errors are logged EXCEPT for servers that have "Access is denied" - how can I log those errors in the log file as well.

Below is the code:

Start-Transcript -path $scheduledpath\logging.txt -append  foreach ($name in $names)  {     Write-Host "Running Against Server $name" -ForegroundColor Magenta     if ( Test-Connection -ComputerName $name -Count 1 -ErrorAction SilentlyContinue )          {             #$Command = "schtasks.exe /query /S $name /fo CSV /v >c:\tools\Scheduled\$name.csv"             $Command = "schtasks.exe /query /S $name /fo CSV /v >$scheduledpath\$name.csv"             Invoke-Expression $Command             Clear-Variable Command -ErrorAction SilentlyContinue         }      else{             Write-Host "$name is Down" -ForegroundColor Red         }  }  Stop-Transcript 

Here is the output on the screen:

> Running Against Server SV064909  > SV064909 is Down  > Running Against Server SV081372  > SV081372 is Down  > Running Against Server YBEF008690_vorher_SV064930  > YBEF008690_vorher_SV064930 is Down  > Running Against Server Alt_SV064921  > Alt_SV064921 is Down  > Running Against Server SV073632  > ERROR: Access is denied.  > Running Against Server SV073633  > ERROR: Access is denied. 

Here is the output in the LOG file....no ACCESS IS DENIED Shown...?

> Running Against Server SV064909  > SV064909 is Down  > Running Against Server SV081372  > SV081372 is Down  > Running Against Server YBEF008690_vorher_SV064930  > YBEF008690_vorher_SV064930 is Down  > Running Against Server Alt_SV064921  > Alt_SV064921 is Down  > Running Against Server SV073632  > Running Against Server SV073633 
like image 552
lara400 Avatar asked Oct 31 '12 14:10

lara400


People also ask

What is transcript logging?

Transcript Logging: As described by Microsoft, Transcript Logging provides a summary of what's happening in a PowerShell session as if you were looking over the shoulder of the person typing. It will provide the commands and the output of those commands.

How do I get the PowerShell transcript?

The Start-Transcript cmdlet creates a record of all or part of a PowerShell session to a text file. The transcript includes all command that the user types and all output that appears on the console.

How do I write a PowerShell log file?

PowerShell has a built-in transcript feature to save all commands and outputs shown in the PS console to a text log file. To log your current PowerShell session, the Start-Transcript cmdlet is used. The –Append option indicates that new sessions will be logged to the end of the file (without overwriting it).


2 Answers

This behavior with native command output not being recorded in Start-Transcript output but being output to the console is reported in connect bug 315857. See workarounds for possible solutions.

like image 103
dugas Avatar answered Nov 09 '22 16:11

dugas


The simplest workaround is to pipe the results of the native command to Out-Host

schtasks.exe /query /S $name /fo CSV /v >$scheduledpath\$name.csv | Out-Host 
like image 38
Stanley De Boer Avatar answered Nov 09 '22 17:11

Stanley De Boer