I can't see the values of a variable written in a function, in another function












0















Here is my code. My main problem is that I ask for a variable in the function location, but when I try to write it in the function Show-Menu it apparently is empty.



I made a search around and I found that should be with the global before the variable. Like this:



function Show-Menu {
Param(
[string]$Title = 'Menu'
)
cls
Write-Host "================ $Title ================"
Write-Host " "
Write-Host "The path is $global:path"
Write-Host "1: Press '1' for IDF config."
Write-Host "2: Press '2' for MDF config."
Write-Host "Q: Press 'Q' to quit."
}

function location {
cls
$global:path = Read-Host -Prompt "Set path to save your configurations (default C:Network_configs)"
#Acuerdate que no funciona bien, por el hecho de tener variables en diferentes funciones.
if ([lenght]$global:path -le 1) {
$global:path = C:Network_configs
}
}


I expect the output with a path C:Network_configs in case there is no keyboard entrance, but I have this output:




================ Menu ================

The path is
1: Press '1' for IDF config.
2: Press '2' for MDF config.
Q: Press 'Q' to quit.
Please make a selection:









share|improve this question




















  • 1





    Where does type [lenght] defined?

    – PetSerAl
    Dec 28 '18 at 19:36













  • To be very honest, i thought count the lenght of the characters. But it doesn´t?

    – Alvaro Senosiain Azpilicueta
    Dec 28 '18 at 19:41











  • Besides the word being misspelled you are also trying to use "lenght" as a (non existing) datatype. If you want to get the character count on a string I would suggest reading the basic documentation of Powershell instead of "just trying something" (getting the count of something in Powershell is one of the very first you'll learn doing when learning Powershell)

    – bluuf
    Dec 28 '18 at 20:56













  • Aside from what already has been said there are other issues with your code as well. $global:path = C:Network_configs will throw an error, b/c the statement would try to execute a program/script C:Network_configs. To assign a path string to a variable put the value in quotes ($global:path = 'C:Network_configs'). Also, it's bad practice to modify global variables in functions. Use parameters and return values for the data flow in your scripts.

    – Ansgar Wiechers
    Dec 28 '18 at 21:07
















0















Here is my code. My main problem is that I ask for a variable in the function location, but when I try to write it in the function Show-Menu it apparently is empty.



I made a search around and I found that should be with the global before the variable. Like this:



function Show-Menu {
Param(
[string]$Title = 'Menu'
)
cls
Write-Host "================ $Title ================"
Write-Host " "
Write-Host "The path is $global:path"
Write-Host "1: Press '1' for IDF config."
Write-Host "2: Press '2' for MDF config."
Write-Host "Q: Press 'Q' to quit."
}

function location {
cls
$global:path = Read-Host -Prompt "Set path to save your configurations (default C:Network_configs)"
#Acuerdate que no funciona bien, por el hecho de tener variables en diferentes funciones.
if ([lenght]$global:path -le 1) {
$global:path = C:Network_configs
}
}


I expect the output with a path C:Network_configs in case there is no keyboard entrance, but I have this output:




================ Menu ================

The path is
1: Press '1' for IDF config.
2: Press '2' for MDF config.
Q: Press 'Q' to quit.
Please make a selection:









share|improve this question




















  • 1





    Where does type [lenght] defined?

    – PetSerAl
    Dec 28 '18 at 19:36













  • To be very honest, i thought count the lenght of the characters. But it doesn´t?

    – Alvaro Senosiain Azpilicueta
    Dec 28 '18 at 19:41











  • Besides the word being misspelled you are also trying to use "lenght" as a (non existing) datatype. If you want to get the character count on a string I would suggest reading the basic documentation of Powershell instead of "just trying something" (getting the count of something in Powershell is one of the very first you'll learn doing when learning Powershell)

    – bluuf
    Dec 28 '18 at 20:56













  • Aside from what already has been said there are other issues with your code as well. $global:path = C:Network_configs will throw an error, b/c the statement would try to execute a program/script C:Network_configs. To assign a path string to a variable put the value in quotes ($global:path = 'C:Network_configs'). Also, it's bad practice to modify global variables in functions. Use parameters and return values for the data flow in your scripts.

    – Ansgar Wiechers
    Dec 28 '18 at 21:07














0












0








0


0






Here is my code. My main problem is that I ask for a variable in the function location, but when I try to write it in the function Show-Menu it apparently is empty.



I made a search around and I found that should be with the global before the variable. Like this:



function Show-Menu {
Param(
[string]$Title = 'Menu'
)
cls
Write-Host "================ $Title ================"
Write-Host " "
Write-Host "The path is $global:path"
Write-Host "1: Press '1' for IDF config."
Write-Host "2: Press '2' for MDF config."
Write-Host "Q: Press 'Q' to quit."
}

function location {
cls
$global:path = Read-Host -Prompt "Set path to save your configurations (default C:Network_configs)"
#Acuerdate que no funciona bien, por el hecho de tener variables en diferentes funciones.
if ([lenght]$global:path -le 1) {
$global:path = C:Network_configs
}
}


I expect the output with a path C:Network_configs in case there is no keyboard entrance, but I have this output:




================ Menu ================

The path is
1: Press '1' for IDF config.
2: Press '2' for MDF config.
Q: Press 'Q' to quit.
Please make a selection:









share|improve this question
















Here is my code. My main problem is that I ask for a variable in the function location, but when I try to write it in the function Show-Menu it apparently is empty.



I made a search around and I found that should be with the global before the variable. Like this:



function Show-Menu {
Param(
[string]$Title = 'Menu'
)
cls
Write-Host "================ $Title ================"
Write-Host " "
Write-Host "The path is $global:path"
Write-Host "1: Press '1' for IDF config."
Write-Host "2: Press '2' for MDF config."
Write-Host "Q: Press 'Q' to quit."
}

function location {
cls
$global:path = Read-Host -Prompt "Set path to save your configurations (default C:Network_configs)"
#Acuerdate que no funciona bien, por el hecho de tener variables en diferentes funciones.
if ([lenght]$global:path -le 1) {
$global:path = C:Network_configs
}
}


I expect the output with a path C:Network_configs in case there is no keyboard entrance, but I have this output:




================ Menu ================

The path is
1: Press '1' for IDF config.
2: Press '2' for MDF config.
Q: Press 'Q' to quit.
Please make a selection:






powershell






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 28 '18 at 21:10









Ansgar Wiechers

141k13125185




141k13125185










asked Dec 28 '18 at 19:25









Alvaro Senosiain AzpilicuetaAlvaro Senosiain Azpilicueta

33




33








  • 1





    Where does type [lenght] defined?

    – PetSerAl
    Dec 28 '18 at 19:36













  • To be very honest, i thought count the lenght of the characters. But it doesn´t?

    – Alvaro Senosiain Azpilicueta
    Dec 28 '18 at 19:41











  • Besides the word being misspelled you are also trying to use "lenght" as a (non existing) datatype. If you want to get the character count on a string I would suggest reading the basic documentation of Powershell instead of "just trying something" (getting the count of something in Powershell is one of the very first you'll learn doing when learning Powershell)

    – bluuf
    Dec 28 '18 at 20:56













  • Aside from what already has been said there are other issues with your code as well. $global:path = C:Network_configs will throw an error, b/c the statement would try to execute a program/script C:Network_configs. To assign a path string to a variable put the value in quotes ($global:path = 'C:Network_configs'). Also, it's bad practice to modify global variables in functions. Use parameters and return values for the data flow in your scripts.

    – Ansgar Wiechers
    Dec 28 '18 at 21:07














  • 1





    Where does type [lenght] defined?

    – PetSerAl
    Dec 28 '18 at 19:36













  • To be very honest, i thought count the lenght of the characters. But it doesn´t?

    – Alvaro Senosiain Azpilicueta
    Dec 28 '18 at 19:41











  • Besides the word being misspelled you are also trying to use "lenght" as a (non existing) datatype. If you want to get the character count on a string I would suggest reading the basic documentation of Powershell instead of "just trying something" (getting the count of something in Powershell is one of the very first you'll learn doing when learning Powershell)

    – bluuf
    Dec 28 '18 at 20:56













  • Aside from what already has been said there are other issues with your code as well. $global:path = C:Network_configs will throw an error, b/c the statement would try to execute a program/script C:Network_configs. To assign a path string to a variable put the value in quotes ($global:path = 'C:Network_configs'). Also, it's bad practice to modify global variables in functions. Use parameters and return values for the data flow in your scripts.

    – Ansgar Wiechers
    Dec 28 '18 at 21:07








1




1





Where does type [lenght] defined?

– PetSerAl
Dec 28 '18 at 19:36







Where does type [lenght] defined?

– PetSerAl
Dec 28 '18 at 19:36















To be very honest, i thought count the lenght of the characters. But it doesn´t?

– Alvaro Senosiain Azpilicueta
Dec 28 '18 at 19:41





To be very honest, i thought count the lenght of the characters. But it doesn´t?

– Alvaro Senosiain Azpilicueta
Dec 28 '18 at 19:41













Besides the word being misspelled you are also trying to use "lenght" as a (non existing) datatype. If you want to get the character count on a string I would suggest reading the basic documentation of Powershell instead of "just trying something" (getting the count of something in Powershell is one of the very first you'll learn doing when learning Powershell)

– bluuf
Dec 28 '18 at 20:56







Besides the word being misspelled you are also trying to use "lenght" as a (non existing) datatype. If you want to get the character count on a string I would suggest reading the basic documentation of Powershell instead of "just trying something" (getting the count of something in Powershell is one of the very first you'll learn doing when learning Powershell)

– bluuf
Dec 28 '18 at 20:56















Aside from what already has been said there are other issues with your code as well. $global:path = C:Network_configs will throw an error, b/c the statement would try to execute a program/script C:Network_configs. To assign a path string to a variable put the value in quotes ($global:path = 'C:Network_configs'). Also, it's bad practice to modify global variables in functions. Use parameters and return values for the data flow in your scripts.

– Ansgar Wiechers
Dec 28 '18 at 21:07





Aside from what already has been said there are other issues with your code as well. $global:path = C:Network_configs will throw an error, b/c the statement would try to execute a program/script C:Network_configs. To assign a path string to a variable put the value in quotes ($global:path = 'C:Network_configs'). Also, it's bad practice to modify global variables in functions. Use parameters and return values for the data flow in your scripts.

– Ansgar Wiechers
Dec 28 '18 at 21:07












1 Answer
1






active

oldest

votes


















0














Using global variables is not recommended check out variable scoping.



However you're very close, try checking for user input, if it is null use the default value.



function Show-Menu
{
Param(
[string]$Title = 'Menu'
)
cls
Write-Host "================ $Title ================"
Write-Host " "
Write-Host "The path is $global:path"
Write-Host "1: Press '1' for IDF config."
Write-Host "2: Press '2' for MDF config."
Write-Host "Q: Press 'Q' to quit."
}
function location
{
cls
$userInput = Read-Host -Prompt "Set path to save your configurations (default C:Network_configs)"
if ($null -eq $userInput)
{
$global:path = 'C:Network_configs'
}
else
{
$global:path = $userInput
}
}





share|improve this answer





















  • 1





    Please avoid formatting code as a snippet unless the snippet is actually runnable. Proper code formatting is done with the {} button.

    – Ansgar Wiechers
    Dec 28 '18 at 23:48











  • Got it. you are absolutely right. I will keep working to avoid the global variables

    – Alvaro Senosiain Azpilicueta
    Jan 7 at 6:23











  • Thanks @AlvaroSenosiainAzpilicueta Please up-vote the answer if it resolved your question.

    – michael dejulia
    Jan 7 at 23:30













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53963400%2fi-cant-see-the-values-of-a-variable-written-in-a-function-in-another-function%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Using global variables is not recommended check out variable scoping.



However you're very close, try checking for user input, if it is null use the default value.



function Show-Menu
{
Param(
[string]$Title = 'Menu'
)
cls
Write-Host "================ $Title ================"
Write-Host " "
Write-Host "The path is $global:path"
Write-Host "1: Press '1' for IDF config."
Write-Host "2: Press '2' for MDF config."
Write-Host "Q: Press 'Q' to quit."
}
function location
{
cls
$userInput = Read-Host -Prompt "Set path to save your configurations (default C:Network_configs)"
if ($null -eq $userInput)
{
$global:path = 'C:Network_configs'
}
else
{
$global:path = $userInput
}
}





share|improve this answer





















  • 1





    Please avoid formatting code as a snippet unless the snippet is actually runnable. Proper code formatting is done with the {} button.

    – Ansgar Wiechers
    Dec 28 '18 at 23:48











  • Got it. you are absolutely right. I will keep working to avoid the global variables

    – Alvaro Senosiain Azpilicueta
    Jan 7 at 6:23











  • Thanks @AlvaroSenosiainAzpilicueta Please up-vote the answer if it resolved your question.

    – michael dejulia
    Jan 7 at 23:30


















0














Using global variables is not recommended check out variable scoping.



However you're very close, try checking for user input, if it is null use the default value.



function Show-Menu
{
Param(
[string]$Title = 'Menu'
)
cls
Write-Host "================ $Title ================"
Write-Host " "
Write-Host "The path is $global:path"
Write-Host "1: Press '1' for IDF config."
Write-Host "2: Press '2' for MDF config."
Write-Host "Q: Press 'Q' to quit."
}
function location
{
cls
$userInput = Read-Host -Prompt "Set path to save your configurations (default C:Network_configs)"
if ($null -eq $userInput)
{
$global:path = 'C:Network_configs'
}
else
{
$global:path = $userInput
}
}





share|improve this answer





















  • 1





    Please avoid formatting code as a snippet unless the snippet is actually runnable. Proper code formatting is done with the {} button.

    – Ansgar Wiechers
    Dec 28 '18 at 23:48











  • Got it. you are absolutely right. I will keep working to avoid the global variables

    – Alvaro Senosiain Azpilicueta
    Jan 7 at 6:23











  • Thanks @AlvaroSenosiainAzpilicueta Please up-vote the answer if it resolved your question.

    – michael dejulia
    Jan 7 at 23:30
















0












0








0







Using global variables is not recommended check out variable scoping.



However you're very close, try checking for user input, if it is null use the default value.



function Show-Menu
{
Param(
[string]$Title = 'Menu'
)
cls
Write-Host "================ $Title ================"
Write-Host " "
Write-Host "The path is $global:path"
Write-Host "1: Press '1' for IDF config."
Write-Host "2: Press '2' for MDF config."
Write-Host "Q: Press 'Q' to quit."
}
function location
{
cls
$userInput = Read-Host -Prompt "Set path to save your configurations (default C:Network_configs)"
if ($null -eq $userInput)
{
$global:path = 'C:Network_configs'
}
else
{
$global:path = $userInput
}
}





share|improve this answer















Using global variables is not recommended check out variable scoping.



However you're very close, try checking for user input, if it is null use the default value.



function Show-Menu
{
Param(
[string]$Title = 'Menu'
)
cls
Write-Host "================ $Title ================"
Write-Host " "
Write-Host "The path is $global:path"
Write-Host "1: Press '1' for IDF config."
Write-Host "2: Press '2' for MDF config."
Write-Host "Q: Press 'Q' to quit."
}
function location
{
cls
$userInput = Read-Host -Prompt "Set path to save your configurations (default C:Network_configs)"
if ($null -eq $userInput)
{
$global:path = 'C:Network_configs'
}
else
{
$global:path = $userInput
}
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Dec 28 '18 at 23:47









Ansgar Wiechers

141k13125185




141k13125185










answered Dec 28 '18 at 22:17









michael dejuliamichael dejulia

1216




1216








  • 1





    Please avoid formatting code as a snippet unless the snippet is actually runnable. Proper code formatting is done with the {} button.

    – Ansgar Wiechers
    Dec 28 '18 at 23:48











  • Got it. you are absolutely right. I will keep working to avoid the global variables

    – Alvaro Senosiain Azpilicueta
    Jan 7 at 6:23











  • Thanks @AlvaroSenosiainAzpilicueta Please up-vote the answer if it resolved your question.

    – michael dejulia
    Jan 7 at 23:30
















  • 1





    Please avoid formatting code as a snippet unless the snippet is actually runnable. Proper code formatting is done with the {} button.

    – Ansgar Wiechers
    Dec 28 '18 at 23:48











  • Got it. you are absolutely right. I will keep working to avoid the global variables

    – Alvaro Senosiain Azpilicueta
    Jan 7 at 6:23











  • Thanks @AlvaroSenosiainAzpilicueta Please up-vote the answer if it resolved your question.

    – michael dejulia
    Jan 7 at 23:30










1




1





Please avoid formatting code as a snippet unless the snippet is actually runnable. Proper code formatting is done with the {} button.

– Ansgar Wiechers
Dec 28 '18 at 23:48





Please avoid formatting code as a snippet unless the snippet is actually runnable. Proper code formatting is done with the {} button.

– Ansgar Wiechers
Dec 28 '18 at 23:48













Got it. you are absolutely right. I will keep working to avoid the global variables

– Alvaro Senosiain Azpilicueta
Jan 7 at 6:23





Got it. you are absolutely right. I will keep working to avoid the global variables

– Alvaro Senosiain Azpilicueta
Jan 7 at 6:23













Thanks @AlvaroSenosiainAzpilicueta Please up-vote the answer if it resolved your question.

– michael dejulia
Jan 7 at 23:30







Thanks @AlvaroSenosiainAzpilicueta Please up-vote the answer if it resolved your question.

– michael dejulia
Jan 7 at 23:30




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53963400%2fi-cant-see-the-values-of-a-variable-written-in-a-function-in-another-function%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas