Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

PowerShell CommonParameters in Custom Script

Posted on 2014-09-16
3
Medium Priority
?
338 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 40

Accepted Solution

by:
Subsun earned 2000 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 29

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

Are You Ready for GDPR?

With the GDPR deadline set for May 25, 2018, many organizations are ill-prepared due to uncertainty about the criteria for compliance. According to a recent WatchGuard survey, a staggering 37% of respondents don't even know if their organization needs to comply with GDPR. Do you?

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

722 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