Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 367
  • Last Modified:

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
0
Jerry Seinfield
Asked:
Jerry Seinfield
  • 9
  • 8
3 Solutions
 
SubsunCommented:
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
 
Jerry SeinfieldAuthor Commented:
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
 
SubsunCommented:
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
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

 
Jerry SeinfieldAuthor Commented:
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
 
SubsunCommented:
#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
 
Jerry SeinfieldAuthor Commented:
Hi Subsun

You rock

Can you please clarify on line 6? Is there a - at the beginning of the line?
0
 
SubsunCommented:
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
 
Jerry SeinfieldAuthor Commented:
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
 
SubsunCommented:
You need to have management tools for Lync and exchange installed on the computer..
Is the management tools installed?
0
 
Jerry SeinfieldAuthor Commented:
no
0
 
SubsunCommented:
Then it wont work.. you need those modules to run the commands added to the script..
0
 
Jerry SeinfieldAuthor Commented:
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
 
SubsunCommented:
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
 
Jerry SeinfieldAuthor Commented:
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
 
SubsunCommented:
Just add -ErrorAction SilentlyContinue at the end of the command..

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

Open in new window

0
 
Jerry SeinfieldAuthor Commented:
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
 
SubsunCommented:
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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 9
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now