How to run PowerShell script with verbose output?

I am wondering if there is a way to run a PowerShell script so that both the commands and the output of each line of the script are printed. For example, in Bash, you should write bash -x myscript or put set -x at the top of your script. In Batch, you would @echo off , traditionally left at the top of your script. Does PowerShell have the equivalent of these constructs?

Things I tried: Running powershell -? | sls verbose powershell -? | sls verbose powershell -? | sls verbose that didn't find anything.

+5
source share
4 answers

It just shows @JamesKo that if you ask the wrong question, you will get the wrong answer :-( Several people give honest answers here based on (a) the lack of exposure to Linux and (b) your use. In the future I will tell you how Linux relates to PowerShell on this topic, but feel free to go to the answer at the end if you are in a hurry. :-)

Background

In PowerShell, verbose has a very specific meaning, which the PowerShell man page is pretty vague:

Displays detailed information about the operation performed by the command. This information resembles information in a trace or in the transaction log. This option only works when the command generates a verbose message.

It even looks like what you want ... but compare this with the Linux documentation for set -x , which, depending on your taste of Linux, might be like this (from a person, a page project ) ...

The shell should write a standard trace error for each command after it expands the command and before it is executed.

or this (from gnu ) ...

Print a trace of simple commands, commands, case commands, select commands and arithmetic for commands and their arguments or related word lists after they are expanded and before they are executed.

The very first line of your question is clearly and concisely consistent with them. But verbosity in PowerShell is different. In a nutshell, turning on verbose mode (whether using the -Verbose command line or the $VerbosePreference variable) simply outputs the output from the verbose stream to the console. (Just as Linux offers two streams: stdout and stderr, PowerShell offers several streams: output stream, error stream, warning stream, detail stream and debug stream. You work with these streams according to Linux, you can even use, for example , commands 4>&1 , for example, to merge a material stream into stdout (you can learn more about several PowerShell output streams in the PowerShell One-Liners Master Records section : Accessing, Processing, and Writing Data and Good Reference - Complete Guide to PowerShell punctuation .)

Answer

The Set-PSDebug command will give you a bash equivalent trace. You can even customize tracking details with the -Trace . Firstly, here is the control before using Set-PSDebug :

 PS> Get-PSDepth 0 

With a value of 1, you get each line of code as it is executed, for example:

 PS> Set-PSDebug -Trace 1 PS> Get-PSDepth DEBUG: 1+ >>>> Get-PSDepth DEBUG: 141+ >>>> { DEBUG: 142+ >>>> $nest = -1 DEBUG: 143+ >>>> $thisId = $pid DEBUG: 144+ while ( >>>> (ps -id $thisId).Name -eq 'powershell') { DEBUG: 145+ >>>> $thisId = (gwmi win32_process -Filter "processid='$thisId'").ParentProcessId DEBUG: 146+ >>>> $nest++ DEBUG: 144+ while ( >>>> (ps -id $thisId).Name -eq 'powershell') { DEBUG: 148+ >>>> $nest 0 DEBUG: 149+ >>>> } 

With a value of 2, you also get variable assignments and code codes:

 PS> Set-PSDebug -Trace 2 PS> Get-PSDepth DEBUG: 1+ >>>> Get-PSDepth DEBUG: ! CALL function '<ScriptBlock>' DEBUG: 141+ >>>> { DEBUG: ! CALL function 'Get-PSDepth' (defined in file 'C:\Users\msorens\Documents\WindowsPowerShell\profile.ps1') DEBUG: 142+ >>>> $nest = -1 DEBUG: ! SET $nest = '-1'. DEBUG: 143+ >>>> $thisId = $pid DEBUG: ! SET $thisId = '9872'. DEBUG: 144+ while ( >>>> (ps -id $thisId).Name -eq 'powershell') { DEBUG: 145+ >>>> $thisId = (gwmi win32_process -Filter "processid='$thisId'").ParentProcessId DEBUG: ! SET $thisId = '10548'. DEBUG: 146+ >>>> $nest++ DEBUG: ! SET $nest = '0'. DEBUG: 144+ while ( >>>> (ps -id $thisId).Name -eq 'powershell') { DEBUG: 148+ >>>> $nest 0 DEBUG: 149+ >>>> } 

These are traces of a simple cmdlet that I wrote called Get-PSDepth . It prints commands, appointments, etc. With the prefix DEBUG mixed with the actual output, which in this case is the only line containing only 0 .

+12
source

You can always use below in a script.

$ VerbosePreference = "Continue"

Note. . You must open the shell in elevated mode .

Below is a screenshot.

$ VerbosePreference

Hope this helps.

+5
source

If you use multi-page recording in your scripts, this will happen automatically,

However, if you need to manually write detailed output from your functions, you need to manually verify that each function is called using the verbose flags. This can be done by checking $ PSCmdlet.MyInvocation.BoundParameters ["Verbose"] from within your function.

0
source

It is really very simple, every PowerShell CMDLET has a built-in Verbose tag. All you need to do, for example:

Test-Connection -ComputerName www.google.com -Verbose

That's all. Hope this helps

0
source

Source: https://habr.com/ru/post/1261884/


All Articles