RabbieBurns Posted May 26, 2010 Posted May 26, 2010 (edited) 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: $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 [b]robert[/b] -DeleteMessage:$True New-InboxRule -Name "Redirect to personal mail" -Mailbox [b]robert[/b] -RedirectTo [email protected] -StopProcessingRules:$False This works fine.. So the psuedo code for what Im trying to do would be: 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. Edited May 26, 2010 by RabbieBurns
mac_shinobi Posted May 26, 2010 Posted May 26, 2010 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
jamesb Posted May 26, 2010 Posted May 26, 2010 $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. 1
RabbieBurns Posted May 26, 2010 Author Posted May 26, 2010 $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!
RabbieBurns Posted May 26, 2010 Author Posted May 26, 2010 Doesnt seem to be working.. Ive stripped it down to this: $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: 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: user robert robert2
jamesb Posted May 26, 2010 Posted May 26, 2010 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. 1
RabbieBurns Posted May 26, 2010 Author Posted May 26, 2010 No errors that time, but it doesnt seem to be pulling the usename...
RabbieBurns Posted May 26, 2010 Author Posted May 26, 2010 (edited) I changed the csv to not contain the User header line and used the original syntax.. However echoing the variables gives this: 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 : Edited May 26, 2010 by RabbieBurns
RabbieBurns Posted May 27, 2010 Author Posted May 27, 2010 (edited) 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 $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: PS C:\Users\robert\Desktop> .\test4.ps1 USER : robert MAIL : [email protected] USER : test1 MAIL : [email protected] USER : test2 MAIL : [email protected] USER : test3 MAIL : [email protected] USER : test4 MAIL : [email protected] USER : test5 MAIL : [email protected] Hopefully replacing the echos with the actual commands will work now.. edit: IT does work!! Cheers jamesb for getting me almost there! Edited May 27, 2010 by RabbieBurns
jamesb Posted May 27, 2010 Posted May 27, 2010 Glad to hear it's working. I'm gonna have to go back and revise on PowerShell object handling methinks. 1
RabbieBurns Posted May 27, 2010 Author Posted May 27, 2010 Ive just realised I messed up copying your foreach ($username in $file.user) perhaps thats why it wasnt working originally!
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now