RabbieBurns (27th May 2010)

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:
This works fine..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
So the psuedo code for what Im trying to do would be:
If anyone who knows powershell could fix that up to a valid script, it would be awesome!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
Cheers.
Last edited by RabbieBurns; 26th May 2010 at 07:37 AM.
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
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.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 }
RabbieBurns (27th May 2010)

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!

Doesnt seem to be working..
Ive stripped it down to this:
Im running the 3 lines to login to the remote powershell manually, then running the above.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 }
However its giving the following error:
Any idea what those error messages mean?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
My users.csv file looks like this:
Code:user robert robert2
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.
RabbieBurns (27th May 2010)

No errors that time, but it doesnt seem to be pulling the usename...

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 12:17 AM.

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
Gives me: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" }
Hopefully replacing the echos with the actual commands will work now..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
edit: IT does work!! Cheers jamesb for getting me almost there!
Last edited by RabbieBurns; 27th May 2010 at 09:13 AM.
Glad to hear it's working. I'm gonna have to go back and revise on PowerShell object handling methinks.
RabbieBurns (27th May 2010)

Ive just realised I messed up copying yourperhaps thats why it wasnt working originally!Code:foreach ($username in $file.user)
There are currently 1 users browsing this thread. (0 members and 1 guests)