![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
|
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.
br> br> |
| |||||||
![]() |
| | Thread Tools | Display Modes |
| | #1 (permalink) |
| 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 "Env spath")) {throw "PSPath environment variable not set." }elseif (-not (Test-Path $env spath)) {throw "PSPATH environment variable points to invalid path." } $files = (, (dir $env spath $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 Specs![]() |
| | #2 (permalink) | ||||||||||||
| 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
| ||||||||||||
My System Specs![]() | |||||||||||||
| | #3 (permalink) | ||||||||||||
| Guest | Re: Is there an equivalent to New-Variable's -scope for functions? On Jul 12, 2:49*pm, "Jon" <Email_Addr...@xxxxxx> wrote:
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 Specs![]() | |||||||||||||
| | #4 (permalink) | ||||||||||||
| 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
(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 Specs![]() | |||||||||||||
| | #5 (permalink) | ||||||||||||||||||||||||
| Guest | Re: Is there an equivalent to New-Variable's -scope for functions? On Jul 13, 7:47*am, "Jon" <Email_Addr...@xxxxxx> wrote:
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 Specs![]() | |||||||||||||||||||||||||
![]() |
| 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 |