Provide the following code:
$WelcomeMessage = "Hello $UserName, today is $($Date.DayOfWeek)"
..
..
..
function Greet-User
{
$Username = Get-UserNameFromSomewhereFancy
$Date = Get-DateFromSomewhereFancy
$WelcomeMessage
}
This is a very simple example, but what he is trying to show is a script where there is $WelcomeMessageone that the person running the script can install at the top of the script and controls how / what the message is displayed.
First of all: why do something like this? Well, if you pass your script to several people, they may want different messages. Maybe they do not like $($Date.DayOfWeek)and want to get the full date. They may not want to show username, whatever.
-, script? . 1000 script, , , script, , . .., , .
, . Greet-User (, / - ...) Greet-User Hello , today is.
, , script, $UserName $Date.
Invoke-Expression . - . :.
$WelcomeMessage = 'Hello $env:USERNAME'
Invoke-Expression $WelcomeMessage
- , , :
$WelcomeMessage = 'Hello $env:USERNAME'
$InvokeExpression = "`"$WelcomeMessage`""
...
, . - , - ...
$WelcomeMessage 'Hello $([void] (Remove-Item C:\Windows -Force -Recurse))'
(, , , )
, script, , script, , , - script, , - - , .
... Invoke-Expression, , :)