Showing posts with label Output. Show all posts
Showing posts with label Output. Show all posts

June 13, 2014

How to call a Powershell script from the command line and return an ERRORLEVEL

Sometimes you want to call a powershell script from the command line and get an ERRORLEVEL a the exit if an exception is throw in the script (ex: With a scheduled task).

In Powershell v2 you could do this :

powershell.exe -noprofile -command ". c:\test-exit.ps1 -erroraction stop;exit $LastExitCode"

Unfortunately this does not work anymore in Powershell v4 (always return ERRORLEVEL 0). It looks like the $LastExitCode is not define anymore for a script by Powershell (only for an external process).  But Powershell seems to now behave as expected and exit with an ERRORLEVEL=1 if any error occurs in your script. So now, the following code should works :

powershell.exe -noprofile -command ". c:\test-exit.ps1 -erroraction stop"

If you really want to check if your script was successful and return a special ERRORLEVEL, you should use the $? variable :

powershell.exe -noprofile -command ". c:\test-exit.ps1 -erroraction stop;if ($?){exit 0}else{exit 99}"

Don’t forget :

  • To use dote-sourcing, the small dot (.) in  front of the script name.
  • To use the –command parameter instead of the –file parameter. The two parameters behave differently.
  • To set the –erroraction stop. If you keep the default value of continue, your script will not stop and the ERRORLEVEL will not be set to 1.

October 29, 2013

How to export/pipe/copy results to Excel

Sometimes you just want to paste the result of a cmdlet into an Excel spreadsheet.

First you will probably try to copy the content from a Gridview :

get-process | out-gridview

But you will realise that the column’s headers are missing.

Why not try to pipe the result into the clip command and paste it in Excel :

get-process | clip

Well not so great, everything is in one big column (no delimiter).

Ok, let’s try to convertto-csv before doing the copy :

get-process | convertto-csv -Delimiter "`t" -NoTypeInformation | clip

Looks great … but if you happen to have any french characters, they will not appear in Excel.

So, my final solution is to re-encode the output in UTF8 by saving it to a file :

get-process | export-csv -Delimiter ";" -NoTypeInformation -force -path "$env:TEMP\export-csv.csv" -Encoding UTF8; start-process excel "$env:TEMP\export-csv.csv"

August 23, 2013

How to use write-verbose with an object

When you are debugging some times you want to use the write-verbose cmdlet to display an object or a collection.  It would have been really nice to be able to do :

write-verbose (Get-EventLog -LogName system -Newest 3 -EntryType Error)

unfortunately this will produce an error :

Write-Verbose : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Message'. Specified method is not supported.

The write-verbose cmdlet only takes a string as a paramter, objects are not allowed.
The trick is to use the out-string cmdlet like this :

write-verbose (Get-EventLog -LogName system -Newest 3 -EntryType Error | Out-String)

You can event make it look a little bit prettier :

write-verbose (Get-EventLog -LogName system -Newest 5 -EntryType Error| format-list -Property TimeGenerated,Entrytype,EventID,Source,Message | out-string)

February 25, 2013

Using Out-Gridview to select objects

If you use the –Passthru parameter with Out-GridView,you will be able to select one or more row, click OK and send the result to the pipeline.

For example, if you wish to restart one or more services, try :

get-service | out-gridview -PassThru | restart-service -Verbose

April 7, 2010

How to add some verbose to a script ?

In your script you can add the following command :

Write-verbose "Verbose message test"

Normally write-verbose output is not displayed, you can add the-verbose parameter when you call your script to active the display of verbose messages.

You can also add the following in your script to activate de display of verbose messages :

$VerbosePreference="Continue"

And the following to restore the normal behavior :

$VerbosePreference="SilentlyContinue"

Update : september 6, 2012

Yon can also add the follwing header to your script (event if you do not have parameters) :

[CmdletBinding()]
param()

This will allow your sctipt to support “common paramters “. You will be able to simply do the following instead of playing with the $VerbosePreference :

.\yourscript.ps1 -verbose

What’s the equivalent of MORE ?

| out-host -paging

How to read and display a text file ?

Get-content c:\boot.ini | foreach-object {$_}

How to do a simple Echo ?

write-output "Starting engine ..."
write-output "Starting engine from $pshome ..."