+ Post New Thread
Results 1 to 11 of 11
Scripts Thread, Powershell Loop Help Please! in Coding and Web Development; I am trying to iterate through a text file of usernames and read the username as a variable, and then ...
  1. #1

    RabbieBurns's Avatar
    Join Date
    Apr 2008
    Location
    Sydney
    Posts
    5,531
    Thank Post
    1,341
    Thanked 470 Times in 307 Posts
    Blog Entries
    6
    Rep Power
    200

    Powershell Loop Help Please!

    I am trying to iterate through a text file of usernames and read the username as a variable, and then take the username, append @mydomain.com to it, and have that as a second variable.

    Here is the code I run when I do it manually:

    Code:
    $Livecred = Get-Credential
    $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $LiveCred -Authentication Basic -AllowRedirection
    
    Import-PSSession $Session
    
    New-InboxRule -Name "Delete Redirected mail" -Mailbox robert -DeleteMessage:$True
       New-InboxRule -Name "Redirect to personal mail" -Mailbox  robert -RedirectTo robert@mydomain.com  -StopProcessingRules:$False
    This works fine..

    So the psuedo code for what Im trying to do would be:

    Code:
    Set userfile = users.txt
    set domain = @mydomain.com
    
    $Livecred = Get-Credential
    
    $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $LiveCred -Authentication Basic -AllowRedirection
    
    Import-PSSession $Session
    
    For each username in the file.. 
    
         read username from file, save it as $user
    
         append $user with @mydomain.com save it as $mailaddress
    
         New-InboxRule -Name "Delete Redirected mail" -Mailbox $user -DeleteMessage:$True
       
         New-InboxRule -Name "Redirect to personal mail" -Mailbox  $user -RedirectTo $mailaddress  -StopProcessingRules:$False
    
    Repeat
    If anyone who knows powershell could fix that up to a valid script, it would be awesome!

    Cheers.
    Last edited by RabbieBurns; 26th May 2010 at 08:37 AM.

  2. #2

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    9,946
    Thank Post
    3,479
    Thanked 1,095 Times in 1,008 Posts
    Rep Power
    372
    Not tried or used power shell but from other scripts / languages that I have had a play with you will need an array to store each of the users and how your text file is formatted will determine how it will be coded to read the text, is it similiar to a csv file so comma seperated or is each user name on its own line or what exactly ?

    ie

    john smith
    mark smith
    joe bloggs

    or is it

    john smith, mark smith, joe bloggs , etc

  3. #3


    Join Date
    Mar 2008
    Location
    Surrey
    Posts
    2,253
    Thank Post
    119
    Thanked 340 Times in 280 Posts
    Blog Entries
    4
    Rep Power
    138
    Code:
    $strAppend = '@mydomain.com'
    
    $credLive = Get-Credential
    
    $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/
    
    Import-PSSession $Session
    $file = Import-CSV('path to CSV file')
    
    foreach ($user in $file) {
        $mail = $user + $domain
        
        New-InboxRule -Name "Delete Redirected mail" -Mailbox $user -DeleteMessage:$True
        New-InboxRule -Name "Redirect to personal mail" -Mailbox $user -RedirectTo $mailaddress -StopProcessingRules:$False
    }
    That won't handle outputting the addresses back to the file. I never could get the hang of output again, but you might be best constructing the result in an object and then using Export-CSV.

  4. Thanks to jamesb from:

    RabbieBurns (27th May 2010)

  5. #4

    RabbieBurns's Avatar
    Join Date
    Apr 2008
    Location
    Sydney
    Posts
    5,531
    Thank Post
    1,341
    Thanked 470 Times in 307 Posts
    Blog Entries
    6
    Rep Power
    200
    Quote Originally Posted by jamesb View Post
    Code:
    $strAppend = '@mydomain.com'
    
    $credLive = Get-Credential
    
    $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/
    
    Import-PSSession $Session
    $file = Import-CSV('path to CSV file')
    
    foreach ($user in $file) {
        $mail = $user + $domain
        
        New-InboxRule -Name "Delete Redirected mail" -Mailbox $user -DeleteMessage:$True
        New-InboxRule -Name "Redirect to personal mail" -Mailbox $user -RedirectTo $mailaddress -StopProcessingRules:$False
    }
    That won't handle outputting the addresses back to the file. I never could get the hang of output again, but you might be best constructing the result in an object and then using Export-CSV.
    Cheers!

    Should the line $mail = $user + $domain not be $mailaddress though to match the variabe further on?


    Also, it doesnt need to do any passing back to a file, what its actually doing is connecting to powershell on outlook.com and running the commands there.

    Gonna test it out now.. thanks again!

  6. #5

    RabbieBurns's Avatar
    Join Date
    Apr 2008
    Location
    Sydney
    Posts
    5,531
    Thank Post
    1,341
    Thanked 470 Times in 307 Posts
    Blog Entries
    6
    Rep Power
    200
    Doesnt seem to be working..

    Ive stripped it down to this:

    Code:
    $strAppend = '@myschool.nsw.edu.au'
    
    $file = Import-CSV('users.csv')
    
    foreach ($user in $file) {
        
        New-InboxRule -Name "Delete Redirected mail" -Mailbox $user -DeleteMessage:$True
    }
    Im running the 3 lines to login to the remote powershell manually, then running the above.

    However its giving the following error:
    Code:
    Cannot process argument transformation on parameter 'Mailbox'. Cannot convert the "@{user=robert}" value of type "Deserialized.System.Management.Automation.PSCustomObject" to type "Microsoft.Exchange.Configuration.Tasks.MailboxIdParameter".
        + CategoryInfo          : InvalidData: (:) [New-InboxRule], ParameterBindin...mationException
        + FullyQualifiedErrorId : ParameterArgumentTransformationError,New-InboxRule
    
    Cannot process argument transformation on parameter 'Mailbox'. Cannot convert the "@{user=robert2}" value of type "Deserialized.System.Management.Automation.PSCustomObject" to type "Microsoft.Exchange.Configuration.Tasks.MailboxIdParameter".
        + CategoryInfo          : InvalidData: (:) [New-InboxRule], ParameterBindin...mationException
        + FullyQualifiedErrorId : ParameterArgumentTransformationError,New-InboxRule
    Any idea what those error messages mean?

    My users.csv file looks like this:

    Code:
    user
    robert
    robert2

  7. #6


    Join Date
    Mar 2008
    Location
    Surrey
    Posts
    2,253
    Thank Post
    119
    Thanked 340 Times in 280 Posts
    Blog Entries
    4
    Rep Power
    138
    Ah, gotcha. You've got a heading.

    Try changing the foreach ($user in $file) to foreach ($username in $file.user). You'll also need to edit the $user attribute where it appears later.

    I think that should take care of it, and you're right on the $mail = $user + $domain being $mailaddress. Sorry, I threw it together rather quickly.

  8. Thanks to jamesb from:

    RabbieBurns (27th May 2010)

  9. #7

    RabbieBurns's Avatar
    Join Date
    Apr 2008
    Location
    Sydney
    Posts
    5,531
    Thank Post
    1,341
    Thanked 470 Times in 307 Posts
    Blog Entries
    6
    Rep Power
    200
    No errors that time, but it doesnt seem to be pulling the usename...

  10. #8

    RabbieBurns's Avatar
    Join Date
    Apr 2008
    Location
    Sydney
    Posts
    5,531
    Thank Post
    1,341
    Thanked 470 Times in 307 Posts
    Blog Entries
    6
    Rep Power
    200
    I changed the csv to not contain the User header line and used the original syntax.. However echoing the variables gives this:

    Code:
    PS C:\Users\robert\Desktop> .\test4.ps1
    Method invocation failed because [System.Management.Automation.PSObject] doesn't contain a method named 'op_Addition
    At C:\Users\rrobert\Desktop\test4.ps1:10 char:26
    +    $mailaddress = $user + <<<<  $strAppend
        + CategoryInfo          : InvalidOperation: (op_Addition:String) [], RuntimeException
        + FullyQualifiedErrorId : MethodNotFound
    
    USER :   @{robert=robert2}
    MAIL :
    Last edited by RabbieBurns; 27th May 2010 at 01:17 AM.

  11. #9

    RabbieBurns's Avatar
    Join Date
    Apr 2008
    Location
    Sydney
    Posts
    5,531
    Thank Post
    1,341
    Thanked 470 Times in 307 Posts
    Blog Entries
    6
    Rep Power
    200
    OK, I seem to have got this to work. I had to create a seperate variable for the username from the file, and this worked.

    I have no idea how or why, but it works

    Code:
    $strAppend = '@mydomain.nsw.edu.au'
    
    # $Livecred = Get-Credential
    # $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $LiveCred -Authentication Basic -AllowRedirection
    # Import-PSSession $Session
    
    $file = Import-CSV('users.csv')
    
    foreach ($file.user in $file) {
       
       $mailaddress = $file.user + $strAppend
       $username = $file.user 
      
      #  New-InboxRule -Name "Delete Redirected mail" -Mailbox $username -DeleteMessage:$True
       # New-InboxRule -Name "Redirect to personal mail" -Mailbox $username -RedirectTo $mailaddress -StopProcessingRules:$False
    
    echo "USER :   $username"
    echo "MAIL :   $mailaddress"
    
    }
    Gives me:

    Code:
    PS C:\Users\robert\Desktop> .\test4.ps1
    USER :   robert
    MAIL :   robert@mydomain.nsw.edu.au
    USER :   test1
    MAIL :   test1@mydomain.nsw.edu.au
    USER :   test2
    MAIL :   test2@mydomain.nsw.edu.au
    USER :   test3
    MAIL :   test3@mydomain.nsw.edu.au
    USER :   test4
    MAIL :   test4@mydomain.nsw.edu.au
    USER :   test5
    MAIL :   test5@mydomain.nsw.edu.au
    Hopefully replacing the echos with the actual commands will work now..

    edit: IT does work!! Cheers jamesb for getting me almost there!
    Last edited by RabbieBurns; 27th May 2010 at 10:13 AM.

  12. #10


    Join Date
    Mar 2008
    Location
    Surrey
    Posts
    2,253
    Thank Post
    119
    Thanked 340 Times in 280 Posts
    Blog Entries
    4
    Rep Power
    138
    Glad to hear it's working. I'm gonna have to go back and revise on PowerShell object handling methinks.

  13. Thanks to jamesb from:

    RabbieBurns (27th May 2010)

  14. #11

    RabbieBurns's Avatar
    Join Date
    Apr 2008
    Location
    Sydney
    Posts
    5,531
    Thank Post
    1,341
    Thanked 470 Times in 307 Posts
    Blog Entries
    6
    Rep Power
    200
    Ive just realised I messed up copying your
    Code:
    foreach ($username in $file.user)
    perhaps thats why it wasnt working originally!



SHARE:
+ Post New Thread

Similar Threads

  1. Advantage Validation shutdown loop
    By bev3561 in forum Windows
    Replies: 3
    Last Post: 14th May 2009, 11:37 PM
  2. XP Machine on Loop
    By itman in forum Windows
    Replies: 15
    Last Post: 18th July 2008, 10:15 PM
  3. PHP Loop Table
    By Jackd in forum Web Development
    Replies: 2
    Last Post: 25th November 2007, 01:28 AM
  4. Powerpoint Playlist loop
    By dan6540 in forum How do you do....it?
    Replies: 2
    Last Post: 20th November 2007, 04:11 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •