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

Ive got the following powershell script:

 

# Copyright (c) Microsoft Corporation. All rights reserved.  
# 
# THIS CODE IS MADE AVAILABLE AS IS, WITHOUT WARRANTY OF ANY KIND. THE ENTIRE RISK
# OF THE USE OR THE RESULTS FROM THE USE OF THIS CODE REMAINS WITH THE USER. 
#
# Synopsis: This script is designed to import WindowsLiveID to existing domain on a CSV file containing data.
#
#
# Usage example:
#
#    Populate credentials to variable:
#    $Livecred = Get-Credential
#    
#             .\listFromCSV.ps1 -UsersFile user_accounts.csv -RemoteURL https://ps.outlook.com/powershell/ -LiveCredential $LiveCred
#

Param(
               [string] $UsersFile = $(throw "Missing parameter: The -UsersFile parameter is required."),
               [string] $RemoteURL = $(throw "Missing parameter: The -$RemoteURL parameter is required."),
               [system.Management.automation.PSCredential] $LiveCredential = $(throw "Missing parameter: The -LiveCredential parameter is required.")
   )

############################################################ Variable Declarations ################################################################

# Suppress warnings during script execution
$WarningPreference="SilentlyContinue"

############################################################ Variable Declarations End ############################################################

############################################################ Function Declarations ################################################################

#
function clearRS
{
  # Tries to close Runspace - clearing all since I can't create RS using -Name
  $error.Clear()
  Remove-PSSession $Session
}

############################################################ Function Declarations End ############################################################

############################################################ Main Script Block ####################################################################

# Tries to create Runspace
$error.Clear()
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri $RemoteURL -Authentication Basic -Credential $LiveCredential -AllowRedirection
Import-PSSession $Session

# read recipients to be created
$error.Clear()
$UserFile = import-csv -Path $UsersFile -OutVariable string -ErrorAction $WarningPreference 

# Filters records and calls appropriate cmdlets
foreach ($user in $UserFile)
{    
   $this_liveID                = $user.EmailName
   
   # Retrieving mailbox
   echo "Retrieving $this_liveID"
   get-mailbox -Identity $this_liveID |fl
}
# finish script - clear Runspaces
$error.Clear()
clearRS

$WarningPreference="Continue"

############################################################ Main Script End ####################################################################

 

I want to modify it, so that when it iterates through the csv, what it does is runs a different command..

 

I want it to run:

 

Set-Mailbox -Identity John -DeliverToMailboxAndForward $true -ForwardingAddress [email protected]

 

But replacing John' with the username in the csv.. But im not too sure where to put it in..

 

Anyone clued up on powershell can see how Id put it in? Cheers!

Posted

Set-Mailbox -Identity $user.Name -DeliverToMailboxAndForward $true -ForwardingAddress $user.EmailName
[/quote]

Should do it if I've got the naming right. You may want to test the properties by putting:

[code]foreach ($user in $UserFile)
{
   $user
}
Get-Member $user

 

That should list out all of the users with all of their properties, and give you a list of all the properties available at the end.

Posted
Set-Mailbox -Identity $user.Name -DeliverToMailboxAndForward $true -ForwardingAddress $user.EmailName
[/quote]

Should do it if I've got the naming right. You may want to test the properties by putting:

[code]foreach ($user in $UserFile)
{
   $user
}
Get-Member $user

 

That should list out all of the users with all of their properties, and give you a list of all the properties available at the end.

 

Cheers, Ill try the test first..

 

The address im trying to put as the -ForwardingAddress is different form any existing email address it will know about..

 

How can I append the $user. to a hardcoded seperate domain ?

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