Solved

How to store command switches  in a powershell variable

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
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.
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…

724 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