@TechNOIR

Как дополнять переменную текстом, чтобы потом получить текст полностью?

Тест дополняется с помощью двух функций и должен в итоге представлять из себя целый текст.

То есть скрипт например такой:

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

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

funct1
funct2
Write-Host $msg

Результат должен быть таков: Привет Друзья! Как дела?.

Но в ответ тишина.

Что я не правильно делаю?
  • Вопрос задан
  • 78 просмотров
Пригласить эксперта
Ответы на вопрос 2
ApeCoder
@ApeCoder
Либо добавить global: перед именем переменной, либо пользоваться возвращаемым значением:


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.
Ответ написан
Комментировать
function funct1 {
    $Msg ="hello"
    $Msg +="friends!"
	return $Msg
}

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

funct1
funct2
Write-Host $Msg
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы