?
Solved

Need a Powershell Script to Export Exchange 2007 Mailbox

Posted on 2009-04-06
6
Medium Priority
?
1,967 Views
Last Modified: 2012-06-27
I am needing a Powershell script that can export a users mailbox to a pst file.  The reason for the script is I want to try and run a vbscript that does some other funcitons and then call this powershell script to run this task.
0
Comment
Question by:seaninman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 9

Expert Comment

by:Sander Stad
ID: 24084507
0
 
LVL 4

Author Comment

by:seaninman
ID: 24086828
This helps a little, but I am still stuck on how I can use the user name already entered in at the begining of my vbscript, so that when the PowerShell script is called i can pass that username to it for the mailbox export.
0
 
LVL 9

Expert Comment

by:Sander Stad
ID: 24094309
Could you please post the code of your vbscript
0
Ransomware Attacks Keeping You Up at Night?

Will your organization be ransomware's next victim?  The good news is that these attacks are predicable and therefore preventable. Learn more about how you can  stop a ransomware attacks before encryption takes place with our Ransomware Prevention Kit!

 
LVL 4

Author Comment

by:seaninman
ID: 24139707
It is at the very end of this script.
Const ForAppending = 8
Const ADS_SCOPE_SUBTREE = 2
Const ADS_UF_ACCOUNTDISABLE = &H02
Const ADS_GROUP_TYPE_GLOBAL_GROUP = &h2
Const ADS_GROUP_TYPE_LOCAL_GROUP = &h4
Const ADS_GROUP_TYPE_UNIVERSAL_GROUP = &h8
Const ADS_GROUP_TYPE_SECURITY_ENABLED = &h80000000
 
 
strLogFile = "manage-rougue-users-log.csv"
strSuspectAccountOU = "OU=Suspect Account"
 
strQueryName = InputBox("Enter the user name.","Manage Suspect Accounts","username")
if strQueryName = "" then
    wscript.quit
end if
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
If NOT objFSO.FileExists(strLogFile) Then
    Set objFile = objFSO.OpenTextFile(strLogFile, 8, True, 0)
    objFile.WriteLine("""DATE"",""USER"",""LAST LOGIN"",""GROUP MEMBERSHIP""")
else
    Set objFile = objFSO.OpenTextFile (strLogFile, ForAppending, True)
End If
 
Set objRootDSE = GetObject("LDAP://RootDSE")
strDomain = objRootDSE.Get("DefaultNamingContext")
 
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand =   CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection
 
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE 
 
objCommand.CommandText = "SELECT distinguishedName FROM 'LDAP://" & strDomain & "' WHERE objectCategory='user' AND sAMAccountName='" & strQueryName & "'"
Set objRecordSet = objCommand.Execute
 
objRecordSet.MoveFirst
Do Until objRecordSet.EOF
    strUser = objRecordSet.Fields("distinguishedName").Value
    strLastLogin = GetLastLogonTime(strUser)
    strGroupMembership = GetGroupMembership(strUser)
    DisableAccount(strUser)
    MoveToNewOU strUser,strSuspectAccountOU,strDomain
    ArchiveMailbox(strQueryName)
    objFile.WriteLine("""" & now() & """,""" & strUser & """,""" & strLastLogin & """,""" & strGroupMembership & """")
    objRecordSet.MoveNext
Loop
 
objFile.Close
 
Function GetLastLogonTime(DN)
    set objUser = GetObject("LDAP://" & DN)
    Set objLastLogon = objUser.Get("lastLogon")
    intLastLogonTime = objLastLogon.HighPart * (2^32) + objLastLogon.LowPart 
    intLastLogonTime = intLastLogonTime / (60 * 10000000)
    intLastLogonTime = intLastLogonTime / 1440
    GetLastLogonTime = intLastLogonTime + #1/1/1601#
End Function
 
Function GetGroupMembership(DN)
    Set objUser = GetObject("LDAP://" & DN) 
    objmemberOf = objUser.GetEx("memberOf")
    
    i = 0
    ReDim arrGroupName(UBound(objmemberOf))
    
    For Each objGroup in objmemberOf
        Set objGroup = GetObject("LDAP://" & objGroup)
        intGroupType = objGroup.GroupType
        If intGroupType AND ADS_GROUP_TYPE_SECURITY_ENABLED Then
            arrGroupName(i) = "[S] " & objGroup.sAMAccountName
	Else
	    arrGroupName(i) = "[D] " & objGroup.sAMAccountName
	End If
	i = i + 1
    Next
    For i = UBound(arrGroupName) - 1 To 0 Step -1
        For k= 0 to i
            If arrGroupName(k)>arrGroupName(k+1) then
                temp=arrGroupName(k+1)
                arrGroupName(k+1)=arrGroupName(k) 
                arrGroupName(k)=temp
            End if
        Next
    Next
    GetGroupMembership = join(arrGroupName,", ")
End Function
 
Sub DisableAccount(DN)
    set objUser = GetObject("LDAP://" & DN)
    intUAC = objUser.Get("userAccountControl")
    if  (intUAC AND ADS_UF_ACCOUNTDISABLE) = 0 Then
        objUser.put "userAccountControl",  intUAC XOR ADS_UF_ACCOUNTDISABLE
        objUser.setinfo
    end if
End Sub
 
Sub MoveToNewOU(DN,OU,DOMAIN)
    set objUser = GetObject("LDAP://" & DN)
    Set objNewOU = GetObject("LDAP://" & OU & "," & DOMAIN)
    objNewOU.MoveHere objUser.ADsPath, vbNullString
End SUb
 
Sub ArchiveMailbox(Username)
    'launch powershell and execute something like this...
    'Get-Mailbox "" & Username & "" | Export-Mailbox -PSTFolderPath "\\server\share\folder$($_.SAMAccountName)"
End Sub

Open in new window

0
 
LVL 71

Expert Comment

by:Chris Dent
ID: 24278652

Do you still need this?

You might benefit from saving the little piece you have at the bottom as a ps1 script file.

e.g.

# GetMailbox.ps1
Get-Mailbox $Args[0] | Export-Mailbox -PSTFolderPath "\\server\share\folder$($_.SAMAccountName)"

Then you can call it from your VbScript with:

Set objShell = CreateObject("WScript.Shell")
objShell.Run "PowerShell.exe -Command ""GetMailbox.ps1 " & UserName & """"

You would need to run this once within PowerShell to allow it to run scripts:

Set-ExecutionPolicy Unrestricted

Personally I would be very tempted to rewrite the entire script in PowerShell. I can see it being rather short :)

Chris
0
 
LVL 12

Accepted Solution

by:
GusGallows earned 2000 total points
ID: 24327937
For the sake of argument, let's say the user's identity is joeblow and you want to save the pst to c:\PSTFiles\ and that your global catalog server is dc2.domain.com:


$mb = get-mailbox 'joeblow'
$PSTLocation = 'c:\PSTFiles\'
$gc = 'dc2.domain.com'
export-Mailbox -Identity $mb -PSTFolderPath $PSTLocation -GlobalCatalog $gc -BadItemLimit 1000 -confirm:$false

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

762 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