![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 Specs![]() |
| | #3 (permalink) |
| | 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 Specs![]() |
![]() |
| 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 | |||