VMware Cloud Community
w1nter
Enthusiast
Enthusiast
Jump to solution

exporting variables created in a function

hey all,

this isnt strictly a powercli question, but hopefully someone can provide some insight into this.

I am building some pretty chunky scripts to do stuff and want to use functions where i can, as you do Smiley Happy

problem is any variables i create/update during the execution of the function are not available outside of the function Smiley Sad on PS 1 does anyone know how to do this?

think of the code like this:

function blah

{

Write-Host "yea do some stuff"

$var = 1 + 1

}

$var is only accessible inside the function...i guess by design, but the code blocks i am trying to build need to be able to spit out a $var - am i missing some fundamental part of functions?

hopefully someone can push me in the right direction - thanks in advance for the help!

Regards,

David

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You can let the function return the variable to your main like this

function blah
{
Write-Host "yea do some stuff"
$var = 1 + 1
$var
}

$mainvar = blah


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
2 Replies
LucD
Leadership
Leadership
Jump to solution

You can let the function return the variable to your main like this

function blah
{
Write-Host "yea do some stuff"
$var = 1 + 1
$var
}

$mainvar = blah


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
w1nter
Enthusiast
Enthusiast
Jump to solution

many thanks!

0 Kudos