T
T
TechNOIR2018-08-17 13:29:43
PowerShell
TechNOIR, 2018-08-17 13:29:43

Powershell. How to supplement a variable with text to get the full text later?

Good afternoon.
The test is supplemented with the help of two functions and should eventually be a whole text, that is, a
script like this:

function funct1 {
    $Msg +="Привет"
    $Msg +="Друзья!"
}

function funct2 {
        $Msg += "Как дела?"
}

funct1
funct2
Write-Host $msg

The result should be This:
Hello Friends! How are you?
But in response, silence ...
Please tell me what's wrong? Thanks in advance

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
ApeCoder, 2018-08-17
@ApeCoder

Either add global: before the variable name, or use the return value:
VARIABLES AND SCOPE
By default, variables are available only in the scope in which they are created.
For example, a variable that you create in a function is available only within the function. A variable that you create in a script is available only within the script (unless you dot-source the script, which adds it to the current scope).
You can use a scope modifier to change the default scope of the variable. The following expression creates a variable named "Computers". The variable has a global scope, even when it is created in a script or function.
$global:computers = "Server01"For more information, see about_Scopes.

V
Vladislav Lyskov, 2018-08-17
@Vlatqa

function funct1 {
    $Msg ="hello"
    $Msg +="friends!"
  return $Msg
}

function funct2 {
        $Msg += "how are you?"
    return $Msg
}

funct1
funct2
Write-Host $Msg

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question