Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

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

Reply
 
Old 07-11-2008   #1 (permalink)
tojo2000


 
 

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


 
 

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


 
 

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


 
 

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


 
 

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
Reply

Thread Tools


Similar Threads
Thread Forum
Scope of Err Variable VB Script
What is the default scope value? PowerShell
Is this a bug in the scope? PowerShell
How do I examine a variable's scope? PowerShell
Powershell and scope PowerShell


Vista Forums 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 Ltd

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