/\/\o\/\/ PowerShelled

This blog has moved to http://ThePowerShellGuy.com Greetings /\/\o\/\/
$AtomFeed = ("Atom.xml")
$PreviousItems = (" PowerShell and Active Directory Part 9 (nested gro... "," Powershell and hey Scripting guy ! "," PowerShell Orphan share remover Tool (Update) "," PowerShell and Active Directory Part 8 (ACL's) "," Signing PowerShell Scripts "," PowerShell and Active Directory Part 7 "," PowerShell and Active Directory Part 6 "," Windows PowerShell Video: Next Generation Command ... "," PowerShell and Active Directory Part 5 "," PowerShell and Active Directory Part 4 (TypeData) "," ")

Monday, August 28, 2006

 


PowerShell Adding -verbose switch to functions



*Remark* this is a post to test Live Writer so it will change a couple of times. (*Edit 5 *for now it looks good I can edit my posts without losing formatting, and offline)

*Edit 2*  there will be some double posts for testing, I will remove those later (this link will stay others will be removed)

In the last post you did see that I used the verbose-switch

here I will show an example on how to set this for the function itself, or to use the preference variables to set this:

*Edit* note that all the Write Messages do not get passed to the pipeline so only will display on the host.

*see this examples in normal text look if pipeline chars do stay

PoSH>$test = "pipeline" ; write-host "Host"
Host

PoSH>$test
pipeline

PoSH>1,2 |% {out-host "pipeline $_" ; write-host "Host $_"}
Host 1
Host 2

PoSH>1,2 |% {"pipeline $_" ; write-host "Host $_"}
pipeline 1
Host 1
pipeline 2
Host 2

PoSH>1,2 |% {"pipeline $_" ; write-host "Host $_"} |% {"second foreach : $_"}
second foreach : pipeline 1
Host 1
second foreach : pipeline 2
Host 2

PoSH>

*End Edit*

 

In the former examples you could see what is the difference between outputting to the host and to the pipeline, the last example shows that in the second foreach there is only the output from out-object (out-object is used by default if you just put a string on a line as I did in the example)  

an example how to add a switch to a funtion, so you can override it for a function like you can with a CMDlet :

 

# WriteExample.ps1
# add a verbose switch to a PowerShell Function

param ([switch]$Verbose)

# Check verbose Switch

if ($verbose.IsPresent) {
  $VerbosePreference = 'Continue'
  Write-Verbose "Verbose Mode Enabled"
}
Else {
  $VerbosePreference = 'SilentlyContinue'
}

# Rest of script

write-host "only Host output"
write-verbose "only Host output verbose mode"
write-warning "Warning only host"
write-error "error, will raise error"

now you can change the behavior by setting the preference in the $global scope or overrule it on a Function base this like this :

 *Edit 4 *Note that the -verbose switch will set it only for the script it leaves the global setting alone

# Examples working with the different preferences

PoSH>$VerbosePreference
SilentlyContinue
PoSH>$WarningPreference
Continue
PoSH>$ErrorActionPreference
Continue


PoSH>\WriteExample.ps1
only Host output
WARNING: Warning only host
C:\PowerShell\WriteExample.ps1 : error, will raise error
At line:1 char:19
+ .\WriteExample.ps1  <<<<

# verbose

PoSH>\WriteExample.ps1 -v
VERBOSE: Verbose Mode Enabled
only Host output
VERBOSE: only Host output verbose mode
WARNING: Warning only host
C:\PowerShell\WriteExample.ps1 : error, will raise error
At line:1 char:19
+ .\WriteExample.ps1  <<<< -v
PoSH>

# changing also warning and error preference

PoSH>$ErrorActionPreference = 'SilentlyContinue'
PoSH>$WarningPreference = 'SilentlyContinue'
PoSH>.\WriteExample.ps1
only Host output
PoSH>.\WriteExample.ps1 -v
VERBOSE: Verbose Mode Enabled
only Host output
VERBOSE: only Host output verbose mode
PoSH>

Enjoy,

Greetings, /\/\o\/\/
Tags : Monad msh PowerShell




Comments:
Anonymous Anonymous
Random question- With E2K7, exmerge goes away. We were trying to figure out a way to get items from the dumpster the same way we did with exmerge but can't. I looked at the “Export-Mailbox” cmdlet but it doesn't seem to have that parameter. I emailed the scripting guys and a few powershell folks but haven't gotten a response. Let me know if you think that this is possible with PowerShell. Sorry for the random posting
 
Blogger /\/\o\/\/
Sorry, can't help you with this one,

I did not work with Exchange 2007 Yet.

Greetings /\/\o\/\/
 
Post a Comment

Links to this post:

Create a Link



<< Home

Archives

October 2005   November 2005   December 2005   January 2006   February 2006   March 2006   April 2006   May 2006   June 2006   July 2006   August 2006   September 2006   October 2006   November 2006   December 2006  

$Links = ("PowerShell RC1 Docs"," PowerShell RC1 X86"," PowerShell RC1 X64"," Monad GettingStarted guide"," Monad Progamming Guide"," Monad SDK"," Monad videos on Channel 9"," MSH Community Workspace"," scripts.readify.net "," MonadSource"," www.reskit.net"," PowerShell Blog"," Under The Stairs"," computerperformance powershell Home"," proudlyserving"," MSH on wikipedia"," MSHWiki Channel 9"," Keith Hill's Blog"," Precision Computing"," PowerShell for fun"," MSH Memo (Japanese)"," monadblog")

find-blog -about "PowerShell","Monad" | out-Technorati.
find-blog -contains "","" | out-Technorati.
Google
 
Web mow001.blogspot.com

This page is powered by Blogger. Isn't yours?