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 - Creating objects with variable names in functions

Reply
 
Old 04-13-2007   #1 (permalink)
Frank


 
 

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)
$scriptcproc${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 SpecsSystem Spec
Old 04-13-2007   #2 (permalink)
Xs3hell


 
 

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)
> $scriptcproc${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 SpecsSystem Spec
Old 04-13-2007   #3 (permalink)
Keith Hill


 
 

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)
> $scriptcproc${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 SpecsSystem Spec
Old 04-14-2007   #4 (permalink)
Jeffrey Snover [MSFT]


 
 

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 SpecsSystem Spec
Old 04-14-2007   #5 (permalink)
Jeffrey Snover [MSFT]


 
 

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 SpecsSystem Spec
Reply

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


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