3

I'm trying to import an xml file and store as a variable for the remainder of a powershell session. The import is obviously successful but the variable content does not persist outside of the function.

Function auth
{
$cred = import-clixml -Path c:\temp\cred.xml
}

4 Answers 4

9

try this:

Function auth
{
    $global:cred = "test"
}

auth
$global:cred
Sign up to request clarification or add additional context in comments.

6 Comments

It doesn't have to be global, just defined outside the function scope.
not only. You can want a function for init globals variables, what is the problem. Its not the subject asked.
Well if you need function to init globals, you can do this: $Global:Cred= auth. Otherwise you get spaghetti code that is hard to read and maintain.
@beatcracker "It doesn't have to be global, just defined outside the function scope". This is actually not true. If you define $cred = $null outside the function and set $cred = 'test' inside the function, it still will not set $cred to test outside the function... try it - run this code $cred = $null function auth { $cred = 'test' } $cred (add line breaks where needed)
There are several methods to do the same thing. In this case, a variable returned by a function, a global variable, a script variable, a variable in a database, a passage by reference, etc.
|
5

You need to declare the variable outside the scope of the function first and then inside the function explicitly tell the variable to update using the script:var method.


Here's the example is taken from https://www.kongsli.net/2013/04/25/powershell-gotchas-refer-to-variables-outside-a-function/ to which credit is given.

The thing is that we have to explicitly tell Powershell to update the variable in the parent scope instead of creating a new variable in the current scope.

$x = 1
function changeit {
  "Changing `$x. Was: $x"
  $script:x = 2
  "New value: $x"
}
"`$x has value $x"
changeit
"`$x has value $x"

1 Comment

well that tell['s] Powershell to update the variable in the *script* scope; this is how you'd specify the parent scope
4

You can use globals as Esperento57 suggests or you can do this

function auth
{
    return 'test'
}

$cred = auth

More succinct:

function auth
{
    'test'
}

$cred = auth

2 Comments

I'd ditch return and just output variable to the pipeline. It's the most idiomatic way.
@beatcracker I guess I use return because I come from a Java background... but yes, you are correct. I updated my answer
1

If you need to do this but with a number of functions and variables, you can place them all into a script and then dotsource the script.

Imagine a script like this:

   #MyDevFunctions.ps1
   $myImportantVar = "somevar"
   $myOtherVar = "ABC123"
   Function Get-MyCoolValue(){$myImportantVar}
   Write-Host "Finished Loading MyDevFunctions"

If you wanted to run this, and then also persist the values of the variables and also the functions themselves, from your parent script you simply invoke it like so:

   PS > . .\MyDevFunctions.ps1
   "Finished Loading MyDevFunctions"

   PS > $myOtherVar
   ABC123

   PS> Get-MyCoolValue
   someVar

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.