Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Is there an equivalent to New-Variable's -scope for functions?

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 07-11-2008   #1 (permalink)
tojo2000
Guest


 

Is there an equivalent to New-Variable's -scope for functions?

A little background: I want to be able to share a set of PowerShell
scripts and functions with my co-workers, so I came up with this:

function Import-Script ([string]$script) {
$local:ReportErrorShowSource = 0

# Clean up the Exception messages a bit
trap [Exception] {
$errmsg = "`n`tError importing '$script': "
$errmsg += ($_.Exception.Message + "`n")
echo $errmsg
break
}

# Check PSPATH
if (-not (Test-Path "Envspath")) {
throw "PSPath environment variable not set."
}elseif (-not (Test-Path $envspath)) {
throw "PSPATH environment variable points to invalid path."
}

$files = (, (dir $envspath $script -Recurse))

# Make sure we find a single matching file
if ($files.Length -gt 1) {
throw ([string]$files.Length + " files of name '$script' found in
PSPATH.")
}elseif (-not $files) {
throw "No files named '$script' found in PSPATH."
}

# Do the needful
. $files[0].FullName
}


This works great as long as I declare all functions to be global like
so:

# Get-Top($count)
# Gets the first X number of objects from the input pipeline.
#
# Args:
# $count: The number of objects to return
#
# Returns:
# $count objects
#
function global:Get-Top ([int]$count) {
$i = 0

foreach ($object in $input) {
if ($i -ge $count) {
return
}
Write-Output $object
$i = $i + 1
}
}

But I would much prefer it if I could set it to a set number of parent
levels, because then I could leave Import-Script in the system profile
and then import files as needed. Declaring the scripts global is okay
for a workaround, but it goes against my general philosophy of not
declaring anything global unless I really need to.

Any ideas?

My System SpecsSystem Spec
Old 07-12-2008   #2 (permalink)
Jon
Guest


 

Re: Is there an equivalent to New-Variable's -scope for functions?

You could perhaps use scriptblocks as a substitute for functions eg

#-----------------------------------------
Function DefineFunction {

$a = {foreach ($b in $args) {$b}}
New-Variable -Name HigherFunction -Value $a -Scope 1
}

DefineFunction
& $HigherFunction 1 2 3
#-----------------------------------------


--
Jon


"tojo2000" <tojo2000@xxxxxx> wrote in message
news:cd554f62-9ace-4106-808f-33e674e2546d@xxxxxx
Quote:

>A little background: I want to be able to share a set of PowerShell
> scripts and functions with my co-workers, so I came up with this:
>
> function Import-Script ([string]$script) {
> $local:ReportErrorShowSource = 0
>
> # Clean up the Exception messages a bit
> trap [Exception] {
> $errmsg = "`n`tError importing '$script': "
> $errmsg += ($_.Exception.Message + "`n")
> echo $errmsg
> break
> }
>
> # Check PSPATH
> if (-not (Test-Path "Envspath")) {
> throw "PSPath environment variable not set."
> }elseif (-not (Test-Path $envspath)) {
> throw "PSPATH environment variable points to invalid path."
> }
>
> $files = (, (dir $envspath $script -Recurse))
>
> # Make sure we find a single matching file
> if ($files.Length -gt 1) {
> throw ([string]$files.Length + " files of name '$script' found in
> PSPATH.")
> }elseif (-not $files) {
> throw "No files named '$script' found in PSPATH."
> }
>
> # Do the needful
> . $files[0].FullName
> }
>
>
> This works great as long as I declare all functions to be global like
> so:
>
> # Get-Top($count)
> # Gets the first X number of objects from the input pipeline.
> #
> # Args:
> # $count: The number of objects to return
> #
> # Returns:
> # $count objects
> #
> function global:Get-Top ([int]$count) {
> $i = 0
>
> foreach ($object in $input) {
> if ($i -ge $count) {
> return
> }
> Write-Output $object
> $i = $i + 1
> }
> }
>
> But I would much prefer it if I could set it to a set number of parent
> levels, because then I could leave Import-Script in the system profile
> and then import files as needed. Declaring the scripts global is okay
> for a workaround, but it goes against my general philosophy of not
> declaring anything global unless I really need to.
>
> Any ideas?
My System SpecsSystem Spec
Old 07-12-2008   #3 (permalink)
tojo2000
Guest


 

Re: Is there an equivalent to New-Variable's -scope for functions?

On Jul 12, 2:49*pm, "Jon" <Email_Addr...@xxxxxx> wrote:
Quote:

> You could perhaps use scriptblocks as a substitute for functions eg
>
> #-----------------------------------------
> Function DefineFunction {
>
> $a = {foreach ($b in $args) {$b}}
> New-Variable -Name HigherFunction -Value $a -Scope 1
>
> }
>
> DefineFunction
> & $HigherFunction 1 2 3
> #-----------------------------------------
>
> --
> Jon
>

I'd found that solution while searching the Internet before, but I
have two concerns: 1) I don't want to make other people jump through
too many hoops in order to add to the repository, and 2) I'd much
rather be able to pass real arguments to the functions, rather than
muck with a list of arguments.
My System SpecsSystem Spec
Old 07-13-2008   #4 (permalink)
Jon
Guest


 

Re: Is there an equivalent to New-Variable's -scope for functions?

"tojo2000" <tojo2000@xxxxxx> wrote in message
news:b33883e0-2db0-423f-941b-cbc4bee86fc5@xxxxxx
Quote:

>I'd found that solution while searching the Internet before, but I
>have two concerns: 1) I don't want to make other people jump through
>too many hoops in order to add to the repository, and 2) I'd much
>rather be able to pass real arguments to the functions, rather than
>muck with a list of arguments.

(2) isn't an issue since you can also pass 'real arguments' to scriptblocks
eg ...

#-------------------------
Function DefineFunction {

$a = {
param([string]$Name=$(Throw "Specify a name"),
$Type = $(Throw "Specify a type"))
Write-Host "$Name is a $Type"
}

New-Variable -Name HigherFunction -Value $a -Scope 1

}

DefineFunction
& $HigherFunction -Name "Jon" -Type "Cool Guy"
#-------------------------



but imho only you can resolve (1). From an outsider's perspective adding a
&$ before a function name isn't an overly complicated loophole to jump
through.


There will undoubtedly be other better solutions, though. Perhaps someone
will suggest one .....




--
Jon



My System SpecsSystem Spec
Old 07-13-2008   #5 (permalink)
tojo2000
Guest


 

Re: Is there an equivalent to New-Variable's -scope for functions?

On Jul 13, 7:47*am, "Jon" <Email_Addr...@xxxxxx> wrote:
Quote:

> "tojo2000" <tojo2...@xxxxxx> wrote in message
>
> news:b33883e0-2db0-423f-941b-cbc4bee86fc5@xxxxxx
>
Quote:

> >I'd found that solution while searching the Internet before, but I
> >have two concerns: *1) I don't want to make other people jump through
> >too many hoops in order to add to the repository, and 2) I'd much
> >rather be able to pass real arguments to the functions, rather than
> >muck with a list of arguments.
>
> (2) isn't an issue since you can also pass 'real arguments' to *scriptblocks
> eg ...
>
> #-------------------------
> Function DefineFunction {
>
> $a = {
> param([string]$Name=$(Throw "Specify a name"),
> $Type = $(Throw "Specify a type"))
> Write-Host "$Name is a $Type"
>
> }
>
> New-Variable -Name HigherFunction -Value $a -Scope 1
>
> }
>
> DefineFunction
> & $HigherFunction -Name "Jon" -Type "Cool Guy"
> #-------------------------
>
> but imho only you can resolve (1). From an outsider's perspective adding a
> &$ before a function name isn't an overly complicated loophole to jump
> through.
>
> There will undoubtedly be other better solutions, though. Perhaps someone
> will suggest one .....
>
> --
> Jon
Ah, I didn't know about the param thing (I'm still new at this). That
might be a workaround. I was thinking of the hoop of asking people to
convert all of their functions to variables pointing to script blocks,
although most of the people I'm dealing with are also beginners, so
that might not be so bad, but I'd rather our code conform to whatever
book they're reading and examples they can find as much as possible.
My System SpecsSystem Spec
Closed Thread
Update your Vista Drivers Update Your Drivers Now!!

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
Is this a bug in the scope? Joel (Jaykul) Bennett PowerShell 1 08-18-2008 11:33 AM
How do I examine a variable's scope? RickB PowerShell 1 06-04-2008 02:12 PM
Variable scope Altraf PowerShell 6 12-13-2007 10:41 AM
trap scope Bob Butler PowerShell 9 10-27-2007 08:41 PM
Powershell and scope wkempf PowerShell 12 04-02-2007 04:13 PM


Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51