0

I have a Text Box function that is creating a variable $text How would I pull that Variable out of the function to use later in the script.

Function System-Box{
[void][Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')

$title = 'System Identification'
$msg   = 'Type in the System Name you 
are Generating Certificates for.

The acceptable responses are:
//////////////////////////////////////////////////////
                    NCC-D

                    ICAP  

                    SCP  

                    RFK
//////////////////////////////////////////////////////

Press OK to Continue.'
$text = [Microsoft.VisualBasic.Interaction]::InputBox($msg, $title,"")
$AcceptableText = @("NCC-D","ICAP","SCP","RFK","")

If($text -in $AcceptableText)
    {
    If(!(Test-Path $CertPath\$text))
    {
        New-Item -ItemType directory -Path $CertPath\$text
    }
}
Else
{
$wshell = New-Object -ComObject Wscript.Shell
$Warning = $wshell.Popup(
"----------------------- WARNING!! -----------------------

You have entered an Invalid Response.

Press OK to enter a valid System Name.", #Text within the Pop-up
0,
"WARNING!", #Title of the Pop-up
48+1
)

if($Warning -eq 1){System-Box}
}
}

/// Other un-related parts of the script ///

If($text -in "NCC-D"){
Move-Certs -NCC-D
}
Else
{
If($text -in "ICAP"){
Move-Certs -ICAP
}
}

The Move-Certs function that is also being called has parameters set to run certain parts. I'm trying to use the $text variable to call those certain parameters.

0

1 Answer 1

1

This is a scoping issue. You are wanting a variable that is defined in a function's scope to be available within the script scope. To do this easily, you could define the variable in the script scope.

# p.ps1 script contents
Function Set-TextVariable {
    $script:text = "mytext"
}
Set-TextVariable
$script:text

# Calling p.ps1 script
.\p.ps1
mytext

The scope where a variable is defined and assigned in relation to the calling scope determines the output result. When you execute a script, a script scope is created. When you create a function, that function has its own scope as well. Any variable defined within a scope can be called in that scope. You can explicitly choose your scope using a modifier when interacting with variables.

See About Scopes for more information. About Functions also has information regarding a function's scope.

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

Comments

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.