![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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) |
| | 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 "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![]() |
| | #3 (permalink) |
| | 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 Specs![]() |
| | #4 (permalink) |
| | 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 Specs![]() |
| | #5 (permalink) |
| | 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 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 | |
| |
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 | |||