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 - references an object name with a variable

Reply
 
Old 07-25-2007   #1 (permalink)
tconley@gmail.com


 
 

references an object name with a variable

I'm working on a powershell script for exchange, and variable-
variables seem to behave correctly, but not if your referencing
something that's an object. I have a loop on $x, starting at 2 and
increasing by 1. I want to be able to reference $i.email2, $i.email3,
$i.email4 , etc etc. I've tried $i.email$x $i.email$$x $i.email${x}
and $i.email$(x)


##
## $i.email2, $i.email3, $i.email4 all exist, how do i reference them
in this loop on $x ????
##

## Import data from csv and store it in variable 'data'
$data = import-csv $args[0]


foreach ($i in $data) {
$ss = new-securestring $i.password
$upn = $i.alias + "@" + $i.fqdn
new-mailbox -Password $ss -Database $i.database -UserPrincipalName
$upn -Name $i.name -OrganizationalUnit $i.ou
for ($x = 2; $x -lt 100; $x++) {
##
## $i.email2, $i.email3, $i.email4 all exist, how do i reference them
in this loop on $x ????
##
if ($i.email$x.length>0) {
$email$x=$i.email$x+"@"+$i.domain
set-Mailbox $upn -EmailAddresses ((get-Mailbox $upn).EmailAddresses
+ "$email$x")
} else {
break;
}
}
}


My System SpecsSystem Spec
Old 07-25-2007   #2 (permalink)
Shay Levi


 
 

Re: references an object name with a variable

In fact you can reference an object name with a variable

PS C:\Scripts> $prop = "mode"
PS C:\Scripts> $i=(dir)[0]
PS C:\Scripts> $i.$prop
d----



Try to assign/concat each property to a varaible and then query it


for ($x = 2; $x -lt 100; $x++) {
$emailNum = "email$x"

if ($i.$emailNum.length>0) {
....
}
}

Shay
http://scriptolog.blogspot.com



> I'm working on a powershell script for exchange, and variable-
> variables seem to behave correctly, but not if your referencing
> something that's an object. I have a loop on $x, starting at 2 and
> increasing by 1. I want to be able to reference $i.email2, $i.email3,
> $i.email4 , etc etc. I've tried $i.email$x $i.email$$x $i.email${x}
> and $i.email$(x)
>
> ##
> ## $i.email2, $i.email3, $i.email4 all exist, how do i reference them
> in this loop on $x ????
> ##
> ## Import data from csv and store it in variable 'data' $data =
> import-csv $args[0]
>
> foreach ($i in $data) {
> $ss = new-securestring $i.password
> $upn = $i.alias + "@" + $i.fqdn
> new-mailbox -Password $ss -Database $i.database -UserPrincipalName
> $upn -Name $i.name -OrganizationalUnit $i.ou
> for ($x = 2; $x -lt 100; $x++) {
> ##
> ## $i.email2, $i.email3, $i.email4 all exist, how do i reference them
> in this loop on $x ????
> ##
> if ($i.email$x.length>0) {
> $email$x=$i.email$x+"@"+$i.domain
> set-Mailbox $upn -EmailAddresses ((get-Mailbox $upn).EmailAddresses
> + "$email$x")
> } else {
> break;
> }
> }
> }
>



My System SpecsSystem Spec
Old 07-25-2007   #3 (permalink)
Keith Hill [MVP]


 
 

Re: references an object name with a variable

"Shay Levi" <no@addre.ss> wrote in message
news:8766a944354a8c99d21002f40e6@news.microsoft.com...
> for ($x = 2; $x -lt 100; $x++) {
> $emailNum = "email$x"
>
> if ($i.$emailNum.length>0) {
> ....
> } }


Or simply $i."email$x"

--
Keith

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Variable within Object Reference Line VB Script
Imbricated foreach-object and $_ variable PowerShell
Writing a cmdlet: using a variable named "object" PowerShell
Binding a powershell variable to the COM Object of a running application. PowerShell
Using group-object with a variable string 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