Solved

PowerShell CommonParameters in Custom Script

Posted on 2014-09-16
3
314 Views
Last Modified: 2014-09-16
Hi.  I wrote a single-line Powershell script, named test.ps1, which attempts to display the value of the CommonParameters errorAction:

"Error action is " + $errorAction

Open in new window


When I run this script, however, using the following command:

& .\Test.ps1 -errorAction ignore

Open in new window


I get the following output, without the errorAction value:

Error action is 

Open in new window


Could you please tell me how to access CommonParameters values in a Powershell script?  Do I need to handle them as I would any other parameter, such as explicitly defining errorAction as a parameter argument that the script accepts?  Or is there a better way?  Thank you.
0
Comment
Question by:deatc
3 Comments
 
LVL 40

Accepted Solution

by:
Subsun earned 500 total points
ID: 40325773
Use [CmdletBinding()] to add the common parameters. Here is an example..

Script:
[CmdletBinding()]
    param (
    )

"Error action is " + $ErrorActionPreference

Open in new window


Run:
.\Test.ps1 -ErrorAction "Continue"

Open in new window

Ref : http://technet.microsoft.com/en-us/library/hh847872.aspx
0
 
LVL 28

Expert Comment

by:becraig
ID: 40325879
What you are looking for is:
"Error action is " + $errorActionpreference

Open in new window

0
 

Author Closing Comment

by:deatc
ID: 40326281
Thank you, Subsun!
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Synchronize a new Active Directory domain with an existing Office 365 tenant
A procedure for exporting installed hotfix details of remote computers using powershell
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now