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
Solved

PowerShell CommonParameters in Custom Script

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Set OWA language and time zone in Exchange for individuals, all users or per database.
This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

828 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