Solved

How to store command switches  in a powershell variable

Posted on 2011-09-22
2
278 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
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This script checks a path to see if a folder exists. If the folder does exist you will get output "The folder has previously been created. No action taken" If not it will create the folder. Then adds one user modify permission to the folder. It …
How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now