Jump to content
EduGeek EdSec 2026 is Go! 27th Oct in Derby! Join us for a day of EdTech security focused talks, networking, and an evening social ×

Recommended Posts

Posted (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 by RabbieBurns
Posted

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

Posted

$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.

  • Thanks 1
Posted
$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!

Posted

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

Posted

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.

  • Thanks 1
Posted (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 by RabbieBurns
Posted (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 by RabbieBurns

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 account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...