Solved

How to store command switches  in a powershell variable

Posted on 2011-09-22
2
283 Views
Last Modified: 2012-05-12
I'm writing a script that has a lot of "Write-Host" commands. Example:
Write-Host -backgroundcolor yellow -foregroundcolor black "warning: check configs"
Write-Host -backgroundcolor Red -foregroundcolor white "Error: check configs"
Write-Host -backgroundcolor Green -foregroundcolor Black  "Good: configs are good"

Open in new window


Instead of writing the -backgroundcolor and foregroundcolor switches for each of my write-host commands, I would like to put them in variables. Example:
$warningMSG = "-backgroundcolor yellow -foregroundcolor black"
$ErrorMSG = "-backgroundcolor Red-foregroundcolor black"
$GoodMSG = "-backgroundcolor Green -foregroundcolor black"

Open in new window


The final command would be like this:
write-host $warningMSG "Warning: check configs"

Open in new window

0
Comment
Question by:lguerra07
[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
2 Comments
 
LVL 16

Accepted Solution

by:
Dale Harris earned 500 total points
ID: 36581627
I would write 3 custom functions:

Function Warningmsg{
Param ($Message)
Write-Host "$Message" -backgroundcolor yellow -foregroundcolor black
}

Function Errormsg{
Param ($Message)
Write-Host "$Message" -backgroundcolor Red -foregroundcolor black
}

Function Goodmsg{
Param ($Message)
Write-Host "$Message" -backgroundcolor Green -foregroundcolor black
}


Then to call the functions from your script, just type in the following:

Warningmsg "Warning! Warning!"

Errormsg "Error!!"

Goodmsg "This is a great message!"

HTH,

Dale Harris
0
 
LVL 1

Author Closing Comment

by:lguerra07
ID: 36582190
So simple!
Thanks.. It works as expected.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

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…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
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…

738 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