![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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) |
| | Creating objects with variable names in functions Hi, I am sure there is a better way but I would like to create objects with names that are formed in a function that are accessable from the main PS script. The names would be a variable concatenated with a parameter that is passed from a for loop. Is this possible? ie: function setup1{ param([string]$script:num) $script cproc${num} = new-object system.diagnostics.PerformanceCounter} # Setup objects for ($i=1; $i -le 5; $i++){ setup1 $i } # Now work on objects that are now setup for ($i=1; $i -le 5; $i++){ "Work on objects $prproc${num}" } |
My System Specs![]() |
| | #2 (permalink) |
| | Re: Creating objects with variable names in functions You can use invoke expression in your function PS > $c='$global:a=1' PS > Invoke-Expression $c PS > $a 1 "Frank" <Frank@discussions.microsoft.com> wrote in message news:9E5DA680-E232-42BA-BBA2-279FEEB87B05@microsoft.com... > Hi, > > I am sure there is a better way but I would like to create objects with > names that are formed in a function that are accessable from the main PS > script. The names would be a variable concatenated with a parameter that > is > passed from a for loop. Is this possible? > > ie: > > function setup1{ > param([string]$script:num) > $script cproc${num} = new-object system.diagnostics.PerformanceCounter> } > > # Setup objects > > for ($i=1; $i -le 5; $i++){ > setup1 $i > } > > # Now work on objects that are now setup > > for ($i=1; $i -le 5; $i++){ > "Work on objects $prproc${num}" > } > |
My System Specs![]() |
| | #3 (permalink) |
| | Re: Creating objects with variable names in functions "Frank" <Frank@discussions.microsoft.com> wrote in message news:9E5DA680-E232-42BA-BBA2-279FEEB87B05@microsoft.com... > Hi, > > I am sure there is a better way but I would like to create objects with > names that are formed in a function that are accessable from the main PS > script. The names would be a variable concatenated with a parameter that > is > passed from a for loop. Is this possible? > > ie: > > function setup1{ > param([string]$script:num) > $script cproc${num} = new-object system.diagnostics.PerformanceCounter> } You can create them like so: Set-Variable "pcproc$num" <some value> -Scope Script > # Now work on objects that are now setup > > for ($i=1; $i -le 5; $i++){ > "Work on objects $prproc${num}" > } Access it like so: (Get-Variable "pcproc$num").Value -- Keith |
My System Specs![]() |
| | #4 (permalink) |
| | Re: Creating objects with variable names in functions This might help: http://blogs.msdn.com/powershell/arc...variables.aspx -- Jeffrey Snover [MSFT] Windows PowerShell Architect Microsoft Corporation This posting is provided "AS IS" with no warranties, no confers rights. Visit the Windows PowerShell Team blog at: http://blogs.msdn.com/PowerShell Visit the Windows PowerShell ScriptCenter at: http://www.microsoft.com/technet/scr.../hubs/msh.mspx |
My System Specs![]() |
| | #5 (permalink) |
| | Re: Creating objects with variable names in functions This might help: http://blogs.msdn.com/powershell/arc...variables.aspx -- Jeffrey Snover [MSFT] Windows PowerShell Architect Microsoft Corporation This posting is provided "AS IS" with no warranties, no confers rights. Visit the Windows PowerShell Team blog at: http://blogs.msdn.com/PowerShell Visit the Windows PowerShell ScriptCenter at: http://www.microsoft.com/technet/scr.../hubs/msh.mspx |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Functions and Objects | PowerShell | |||
| passing objects as parametes to subs or functions? | VB Script | |||
| Objects returned from functions?? | PowerShell | |||
| How can we prevent collisions in the names of functions? | PowerShell | |||
| Common Windows process names and functions. | Vista installation & setup | |||