Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to store command switches  in a powershell variable

Posted on 2011-09-22
2
Medium Priority
?
293 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 2000 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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
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…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

618 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