Alright, using your example your variable is available while inside the
function. I tested this (Win 7, so Powershell v2) by creating your
function:
function my_function { set-variable -name backupstatus -value 1;
$backupstatus}
Then I ran your function:
my_function
This printed the number 1 as the output.
Now, while outside of the function I checked for the value of
$backupstatus, which should be 1. It returned nothing. The variable set
inside the function is not available to me once I am outside of the
function. This is where the Scope of the variable comes into play.
Changing the function to include Scope:
function my_function { set-variable -name backupstatus -value 1
-Scope 1 }
Now you are able to query $backupstatus outside of the function and
returns the value 1.
Also, you can check this by running the cmdlet 'Variable'. When no scope
is set (actually it defaults to Scope 0) and the function is ran the
variable is not shown as being set. When Scope 1 is set it is shown with
a value of 1.
-----------------------
PowerShell Commands:
PS > function my_function { set-variable -name backupstatus -value 1 }
PS > my_function
PS > $backupstatus
PS > function my_function { set-variable -name backupstatus -value 1
-Scope 1 }
PS > my_function
PS > $backupstatus
1
---------------------
So, if you were to change the Return portion of your test code and add
in variables with a scope of 1 you could execute your function and then
test for the variable values.
As to getting custom functions to work with the pipeline, that is
something I am not experienced with and I haven't had need to try or use
data created in a function outside of the function yet.
-----Original Message-----
From: Joseph L. Casale [mailto:[email protected]]
Sent: Tuesday, January 19, 2010 3:28 PM
To: NT System Admin Issues
Subject: RE: Powershell return values
>My gut would be to use variables, but then you need to create variables
>with a scope such that the variables exist outside of the function.
>
>http://blogs.msdn.com/powershell/archive/2007/04/14/controlling-the-sco
p
>e-of-variables.aspx
Ok Guys,
If you say it always can, it's just me:)
I did find that article actually but my variables seem to exist outside
the
function after its called so I wasn't 100% sure of the scope and its
impact
on my case?
If I do:
my_function {
Set-Variable -Name backupStatus -Value 1
}
Then test it, it always appears. So what is an example when the scope is
required?
Except maybe to limit it to within the function?
BTW, found that k.hill article, just inept at programming:)
Thanks everyone!
jlc
~ Finally, powerful endpoint security that ISN'T a resource hog! ~
~ <http://www.sunbeltsoftware.com/Business/VIPRE-Enterprise/> ~
~ Finally, powerful endpoint security that ISN'T a resource hog! ~
~ <http://www.sunbeltsoftware.com/Business/VIPRE-Enterprise/> ~