Using Verbose Parameter from the Own Cmdlet


I am working on a exciting  book review from a good friend and one of my thoughts is that , when applicable, we can use the verbose from the own cmdlet .

I always thought that if you pass a –verbose parameter to an advanced function , it is recursive to the other cmdlets inside it. Well, I can be wrong, but my test said that is not what happens .

My point is ,lets say that you have a function to copy files from a path to other. (of course you don’t need a function for this, but just to illustrate)  you don’t need to write-verbose “Copying…..”…You can use the verbose message from the copy-item

   1: function Copy-Files

   2: {

   3:     [CmdletBinding()]

   4:     param( 

   5:         [Parameter(Position=0, Mandatory=$true)] [string] $sourcefolder,

   6:         [Parameter(Position=1, Mandatory=$true)] [string] $destinationfolder

   7:     ) 

   8:         

   9:     $Verbose = (($PSBoundParameters.ContainsKey('Verbose')) -or ($VerbosePreference -ne 'SilentlyContinue'))

  10:  

  11:     Get-ChildItem -Path $sourcefolder `

  12:     | foreach     { 

  13:             Copy-Item -Path  $_.fullname `

  14:             -Destination $destinationfolder -Verbose:$verbose

  15:     }

  16: }

You can see that I am testing if the –verbose parameter was passed and if the $verbosepreference variable was change :

   1: $Verbose = (($PSBoundParameters.ContainsKey('Verbose')) -or ($VerbosePreference -ne 'SilentlyContinue'))

Then it is just pass the $verbose variable to the  cmdlet : –Verbose:$verbose

   1: Get-ChildItem -Path $sourcefolder `

   2: | foreach     { 

   3:         Copy-Item -Path  $_.fullname `

   4:         -Destination $destinationfolder -Verbose:$verbose

   5: }

Now it is just to call using verbose :

   1: Copy-Files -sourcefolder c:\Teste\Source -destinationfolder c:\Teste\Destination –verbose

 

image

or not using verbose :

   1: Copy-Files -sourcefolder c:\teste\Source -destinationfolder c:\teste\Destination 

or you just can change the $verbosepreference before the calling the function , put  –verbose  in the copy-item, or just use write-verbose and you will not to worry about this code that I did. But I liked to do this and avoiding to change the variable or use write-verbose. Just another approach maybe.

I confess that I DO need to do more test,but so far that is what I figured out

Sorriso

About Laerte Junior

Laerte Junior Laerte Junior is a SQL Server specialist and an active member of WW SQL Server and the Windows PowerShell community. He also is a huge Star Wars fan (yes, he has the Darth Vader´s Helmet with the voice changer). He has a passion for DC comics and living the simple life. "May The Force be with all of us"
This entry was posted in Powershell, SQL SERVER EM GERAL, Virtual Pass BR. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s