Using Common Parameters in PowerShell

February 23, 2011 at 10:30 am Leave a comment

I have been exploring how the Common Parameter functionality gets used in custom code.  Basically it looks as though you need to use the CmdletBinding attribute to have the Common Parameters bound to your function, which means that you need to have parameters for this to work.  Then you have to use cmdlets such as Write-Verbose to hook in the the in built support for the Common Parameters.

The following code is what I have been using to check the functionality:

###############################################
# Script to explore common parameters
# Brent Challis
# Dimension Data Learning Solutions
# Melbourne, Victoria, Australia
###############################################
function Test-CommonParametersWithCmdletBinding
{
[CmdletBinding()]
Param
(
[string]$FunctionMessage = “Initial Value”
)
Write-Output “Starting Function with CmdletBinding.”
Write-Host “Writing to the host.”
Write-Output $FunctionMessage
Write-Verbose “Verbose message.”
Write-Debug “Debug message.”
Write-Warning “Warning message.”
Write-Output “Function Complete.”
}

function Test-CommonParametersWithoutCmdletBinding
{
Param
(
[string]$FunctionMessage = “Initial Value”
)
Write-Output “Starting Function without CmdletBinding.”
Write-Host “Writing to the host.”
Write-Output $FunctionMessage
Write-Verbose “Verbose message.”
Write-Debug “Debug message.”
Write-Warning “Warning message.”
Write-Output “Function Complete.”
}

Clear-Host
Write-Host “Starting.”
Write-Host “==========================================================”
Write-Host “Getting help info”
Get-Help Test-CommonParametersWithCmdletBinding
Write-Host “———————————————————-”
Get-Help Test-CommonParametersWithoutCmdletBinding
Write-Host “==========================================================”
Write-Host “Run with CmdletBinding.”
Test-CommonParametersWithCmdletBinding “Test Message” -Verbose -Debug
Write-Host “==========================================================”
Write-Host “Run without CmdletBinding.”
Test-CommonParametersWithoutCmdletBinding “Test Message” -Verbose -Debug
Write-Host “==========================================================”
Write-Output “Test complete.”

Advertisements

Entry filed under: PowerShell.

Warming up a SharePoint 2010 Server with PowerShell More on PowerShell Common Parameter

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed


Calendar

February 2011
M T W T F S S
« Dec   Mar »
 123456
78910111213
14151617181920
21222324252627
28  

Most Recent Posts


%d bloggers like this: