Solved

powershell script help required

Posted on 2013-06-25
17
346 Views
Last Modified: 2013-06-26
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
0
Comment
Question by:Jerry Seinfield
  • 9
  • 8
17 Comments
 
LVL 40

Expert Comment

by:Subsun
ID: 39275148
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

0
 

Author Comment

by:Jerry Seinfield
ID: 39275225
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?
0
 
LVL 40

Expert Comment

by:Subsun
ID: 39275263
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).
0
 

Author Comment

by:Jerry Seinfield
ID: 39275317
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
0
 
LVL 40

Expert Comment

by:Subsun
ID: 39275344
#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

0
 

Author Comment

by:Jerry Seinfield
ID: 39275392
Hi Subsun

You rock

Can you please clarify on line 6? Is there a - at the beginning of the line?
0
 
LVL 40

Expert Comment

by:Subsun
ID: 39275419
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.
0
 

Author Comment

by:Jerry Seinfield
ID: 39275937
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
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 40

Expert Comment

by:Subsun
ID: 39275943
You need to have management tools for Lync and exchange installed on the computer..
Is the management tools installed?
0
 

Author Comment

by:Jerry Seinfield
ID: 39275957
no
0
 
LVL 40

Expert Comment

by:Subsun
ID: 39275960
Then it wont work.. you need those modules to run the commands added to the script..
0
 

Author Comment

by:Jerry Seinfield
ID: 39277972
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
0
 
LVL 40

Assisted Solution

by:Subsun
Subsun earned 500 total points
ID: 39278162
You can run it from any computer which is connected to domain where the management tools are installed..

But if you want to try with remote session then check this code and see if it works for you..

Replace appropriate values for $ExchServer and $CSPoolFQDN
$ExchServer = "Your Server Name"
$CSPoolFQDN = "Your Lync Pool FQDN"

#To Import Exchange 2010 Session
If (!(Get-PSSession | ?{$_.ConfigurationName -eq "Microsoft.Exchange"})){
   $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://$ExchServer/PowerShell/ -Authentication Kerberos
   Import-PSSession $Session  | Out-Null
}

#To Import Lync Session
If (!(Get-PSSession |?{$_.computername -eq $CSPoolFQDN})){
    $CSSession = New-PSSession -ConnectionUri https://$CSPoolFQDN/ocspowershell -Authentication NegotiateWithImplicitCredential
    Import-PSSession -Session $CSSession | Out-Null
}

$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

0
 

Author Comment

by:Jerry Seinfield
ID: 39279139
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
0
 
LVL 40

Assisted Solution

by:Subsun
Subsun earned 500 total points
ID: 39279210
Just add -ErrorAction SilentlyContinue at the end of the command..

For example..
Disable-CsUser -Identity $user.ADUsername -ErrorAction SilentlyContinue

Open in new window

0
 

Author Comment

by:Jerry Seinfield
ID: 39279342
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?
0
 
LVL 40

Accepted Solution

by:
Subsun earned 500 total points
ID: 39279512
New-MailboxExportRequest should be queued by default, are you saying the script waits for the export to get completed?

If it is a network issue causing the pst access failure then you should consider exporting to a local volume first and once the export complete then copy files to the remote share..
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now