Solved

PowerShell CommonParameters in Custom Script

Posted on 2014-09-16
3
327 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 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 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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

A brief introduction to what I consider to be the best editor for PowerShell.
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
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…

752 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