More on PowerShell Common Parameter

February 25, 2011 at 4:35 pm Leave a comment

Following up on how to use the Common Parameters with your own functions utlising the cmdlets such as Write-Verbose I went exploring the preference variables to see how they reacted to the switches to see if I could detect the use of the switches for my own code.  The following script is what I used to see the variables’ values change and to show that I could detect the switches being used.

function Test-CommonParameterVariables
{
[CmdletBinding()]
Param
(
[string]$switches
)
“Commandline switches: $switches`n”
“Preference Variables:”
“———————”
dir variable:\*Preference | Sort-Object -Property Name
if ($VerbosePreference -ne ‘SilentlyContinue’)
{
Write-Warning “In the VerbosePreference block”
}
if ($DebugPreference -ne ‘SilentlyContinue’)
{
Write-Warning “In the DebugPreference block”
}
}

Clear-Host
Test-CommonParameterVariables ‘No switches’
Write-Host “—————————————-”
Test-CommonParameterVariables ‘-Verbose’ -Verbose

Write-Host “—————————————-”
Test-CommonParameterVariables ‘-Debug’ -Debug

Write-Host “—————————————-”
Test-CommonParameterVariables ‘-Verbose -Debug’ -Verbose -Debug

Write-Host “—————————————-”
Test-CommonParameterVariables ‘-ErrorAction Continue’ -ErrorAction Continue

Write-Host “—————————————-”
Test-CommonParameterVariables ‘-ErrorAction Inquire’ -ErrorAction Inquire
Write-Host “—————————————-”
Test-CommonParameterVariables ‘-ErrorAction Stop’ -ErrorAction Stop
Write-Host “—————————————-“

Advertisements

Entry filed under: PowerShell.

Using Common Parameters in PowerShell SharePoint 2010 Dev references

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: