Solved

PowerShell CommonParameters in Custom Script

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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
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…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

617 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