1

I'm making a menu system for a script I have where I can change the values I need to by using the menu or by passing them as arguments to the script. One of the annoyances I have at the moment is after entering a new value when the menu refreshes, the variable in the menu text does not update to the new values.

      $global:drive="C:"

      $title = "Setup 

      "
           $message = "The default variables are:
            VARIABLES TO CHANGE

            1. The Drive Location: $global:drive <<<--- This is the variable that does not update after I change it when I run the script.
       "

      $one = New-Object System.Management.Automation.Host.ChoiceDescription "&1 Drive", "The Drive Location:"

      $options = [System.Management.Automation.Host.ChoiceDescription[]]($one)

     :OuterLoop do 
{ 
    for ($i = 1; )
    {

      $result =  $host.ui.PromptForChoice($title, $message, $options, 1) 

    switch ($result)
        {
            0 {$global:drive = Read-Host "Drive is $global:drive .Set the  Drive Location";
                "The Drive is now: $global:drive";
                break;}

          }

                }
}

 while ($y -ne 100)

Initially I did not set the variable to to global but read on here that that might help. It did not but it did not hurt either. I also tried setting it to script too. The variable does change, so this is cosmetic more than anything.

Thanks

1 Answer 1

1

I ran your code, but mine changes in the menu. The only thing I did was comment out your first $global:drive="C:". If this is always at the top of the script, then $global:drive will always display C:.

You can use the following code to check for the existance of a variable, then assign the value if it doesn't already exist:

if $(!(Get-Variable -Name Drive -Scope global -ErrorAction SilentlyContinue)) { $global:drive="C:" }

If the global variable Drive exists, it will do nothing. If it doesn't exist, $global:drive will be set to C:. Hope this helps.

Edit after @Norm comment:

The reason your message isn't updating, is because the $title is set outside of the loop. Because $Title is already defined, it doesn't need to change every time the loop runs. Simply move the declaration for $Title inside the loop before the line $result = $host.ui.PromptForChoice($title, $message, $options, 1). This should fix the problem you are having.

Edit2: I'm sorry, it's $message that needs moved, not $title

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Nick. The code I gave was inaccurate as is part if a larger set and I had to hack it down a little to get to the point. Doing so I left off the while loop. If you run that again, you should see what I mean. You brought up a great point though. Thanks
OH MAN THAT WAS SOOOO OBVIOUS. Thanks Nick. I just keep telling myself... DUH1 :)
No problem, we've all done the same thing at some point or another :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.