Link to home
Start Free TrialLog in
Avatar of Jerry Seinfield
Jerry SeinfieldFlag for United States of America

asked on

powershell script help required

Hi Experts

I need from your expertise to validate and/or modify the script attached

Basically, I am looking to build a script that will do following:

Given a input CSV file, and based on the value of the column named ADUsername.

1. Export all emails from user's regular mailbox to a PST file[please remember in exchange 2010, each user has 2 mailboxes. The regular mailbox, and the archive mailbox]

2. Export all emails from user's archive mailbox to a PST

3. Disable all Lync attributes for that username

I ran the 3 powershell cmdlet against individual accounts on my lab, and works fine. Now i need to script the process and run the 2 powershell cmdlet against each user that is part of the csv input file and based on ADUsername
UserList.csv
BackupEmailDLync.txt
Avatar of SubSun
SubSun
Flag of India image

Try..
PS : Make sure you import lync & exchange PowerShell modules to the PowerShell console before you run this script..
$usernames = Import-csv -path c:\temp\UserList.csv
foreach ($user in $usernames)
{
     
     # export all email items from regular and archive mailbox to a pst
     # disable Lync attributes for a user that will be disabled in AD
     
    New-MailboxExportRequest -Mailbox $user.ADUsername -FilePath "\\servername\L$\PST\$($user.ADUsername).pst" -DomainController xxxx.xxxx.domain.on.ca
    New-MailboxExportRequest -Mailbox $user.ADUsername -IsArchive -FilePath "\\server\L$\PST\Arch$($user.ADUsername).pst" -DomainController xxx.xxx.domain.on.ca
    Disable-CsUser -Identity $user.ADUsername
}

Open in new window

Avatar of Jerry Seinfield

ASKER

Hi Subsun

Is there a way to include in the script the option to import lync & exchange modules to the powershell console before running the script?

If so,

can you please attach the lines to script?
You need to have management tools for Lync and exchange installed on the computer..

Add the following lines in the starting of the script..
#To Import Lync Module
Import-Module Lync
#To add Snapin for Exchange 2010
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010
. $env:ExchangeInstallPath\bin\RemoteExchange.ps1
Connect-ExchangeServer -auto

Open in new window

Or add the line Import-Module Lync as the first line of the script and run the script from EMS (Exchange Management Shell).
just to validate we are on same page, can you please send the script with the modifications?

I want to make sure there is no misspelling, typos, etc

Thanks in advance
#To Import Lync Module
Import-Module Lync

#To add Snapin for Exchange 2010
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010
. $env:ExchangeInstallPath\bin\RemoteExchange.ps1
Connect-ExchangeServer -auto

$usernames = Import-csv -path c:\temp\UserList.csv
foreach ($user in $usernames)
{
     
     # export all email items from regular and archive mailbox to a pst
     # disable Lync attributes for a user that will be disabled in AD
     
    New-MailboxExportRequest -Mailbox $user.ADUsername -FilePath "\\servername\L$\PST\$($user.ADUsername).pst" -DomainController xxxx.xxxx.domain.on.ca
    New-MailboxExportRequest -Mailbox $user.ADUsername -IsArchive -FilePath "\\server\L$\PST\Arch$($user.ADUsername).pst" -DomainController xxx.xxx.domain.on.ca
    Disable-CsUser -Identity $user.ADUsername
}

Open in new window

Hi Subsun

You rock

Can you please clarify on line 6? Is there a - at the beginning of the line?
It's a dot "."

it's because we are running another script (exchange startup script) inside this script. Also the script need to be dot sourced into the PowerShell session as it initializes some variables and imports several Exchange specific functions to the session.
Hi Subsun

i got multiple errors when i tried to run the script from a domain controller. i did launch the active directory module and ran from there

Please see screen shoot attached
Unabletorunscriptfromdomaincontr.jpg
You need to have management tools for Lync and exchange installed on the computer..
Is the management tools installed?
Then it wont work.. you need those modules to run the commands added to the script..
Hi Subsun and experts team

Thank you so much for all the support

My understanding is there is an option to run remote PowerShell commands as well.  In that way, you won’t have to import/install AD/Exchange/Lync PowerShell modules on the management server.  If you use remote you may be able to do it from one of the DC because nothing needs to be installed as you are invoking commands that reside on the remote server.

http://www.howtogeek.com/117192/how-to-run-powershell-commands-on-remote-computers/

http://howexchangeworks.com/2012/01/connect-to-lync-server-using-remote-powershell.html

http://flinchbot.wordpress.com/2013/04/19/working-with-powershell-modules-and-remote-sessions/

Based on links above,

Can you or someone else please help me to finish this script instead of importing modules?

The problem is my manager does not want to import any modules to our Domain controller, and our security standard policies do not allow either
SOLUTION
Avatar of SubSun
SubSun
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Subsun and Experts team

Really appreciated all your help on this ticket

I have the last question regarding this script

The script with latest modification based on your recommendations works OK. However, I would like to clarify that every time that I run the script and/or run just the powershell cmdlet to export email to pst, sometimes fails because couldn't connect to the source mailbox, this is normal given our configuration. so, all i have to do is repeat the command or F1 hit enter from powershell until the powershell cmdlet or script works and export all data to a PST.


Based on that explanation, and the screen shoot attached, I would like to add a condition between lines 24 and 25 and 25 and 26, to ignore the message when is unable to connect to source mailbox, and repeat the powershell cmdlet until is executed successfully, then jump to next instruction[second powershell to export archive emails to a pst]

Can you please modify the latest script and send me the updates?
Couldntconnecttosourcemailbox.jpg
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Subsun

I just add the -ErrorAction SilentlyContinue and did not see the error, however the cmdlet was not executed, therefore the emails were not exported to a PST, so I had to manually press F1, then hit enter to repeat the cmdlet until the PST is created

Is there a way to tell Powershell, please repeat this cmdlet until is successfully completed and the pst is generated as per my code?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial