[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 298
  • Last Modified:

How to store command switches in a powershell variable

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
lguerra07
Asked:
lguerra07
1 Solution
 
Dale HarrisCommented:
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
 
lguerra07Author Commented:
So simple!
Thanks.. It works as expected.
0

Featured Post

Free recovery tool for Microsoft Active Directory

Veeam Explorer for Microsoft Active Directory provides fast and reliable object-level recovery for Active Directory from a single-pass, agentless backup or storage snapshot — without the need to restore an entire virtual machine or use third-party tools.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now