export mailbox request script

Илья Боков 1 Reputation point
2022-10-06T08:58:24.403+00:00

Hello friends from Kazakhstan! i need your help.
exch 2010
every monts our HR dep send me csv file, which includes over 500 lines of fired employees. BUT
lines are on Russian Language:

Name Surname

in AD it
email and user logon name - name.surname@keyman in English
first name and lastname (Display name) - in Russian language.

what i do.
i copy russian lang line, searching in ad, than copy english name.surname, and copypaste in EXCH Shell like this:

New-MailboxExportRequest -Mailbox name.surname -FilePath \10.0.0.1\e$\Mailbox\firedpst\name.surname.pst,
when it completed, delete
Disable-Mailbox -Identity name.surname -Confirm:$false

what i need help:

can i do it automatically? search from AD by display name, copy logon name, generate new csv with name.surname, then Disable Account by name.surname, move it to DisabledAccounts folder, then

foreach ($Mailbox in (Import-Csv c:\ps1\fired.csv)) {New-MailboxExportRequest -Mailbox $Mailbox.DisplayName -FilePath "\10.0.0.1\e$\Mailbox\firedpst\$($Mailbox.Alias).pst"}

THEN or after completed status

Disable-Mailbox -Identity name.surname (from csv) -Confirm:$false

and cherry is report.

Active Directory
Active Directory
A set of directory-based technologies included in Windows Server.
6,244 questions
Exchange Server Management
Exchange Server Management
Exchange Server: A family of Microsoft client/server messaging and collaboration software.Management: The act or process of organizing, handling, directing or controlling something.
7,503 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,462 questions
{count} votes

3 answers

Sort by: Most helpful
  1. KyleXu-MSFT 26,246 Reputation points
    2022-10-07T02:30:22.77+00:00

    @Илья Боков

    Based on your description, here are test in my lab

    The source file that I used:
    248286-1.png

    Test the first part of script, check whether it could find correct users:

    $users = import-csv C:/temp/users.csv  
    $Data = @()  
      
    foreach ($user in $users){  
    $UserName = $user.name +" "+$user.Surname  
    Get-ADUser -Filter 'Name -eq $UserName' | select UserPrincipalName  
      
    }   
    

    248296-2.png

    Now, we could add the export request command to this script:

    $users = import-csv C:/temp/users.csv  
    $Data = @()  
          
    foreach ($user in $users){  
        $UserName = $user.name +" "+$user.Surname  
        $temp = Get-ADUser -Filter 'Name -eq $UserName' | select UserPrincipalName  
        $fileName = $temp.UserPrincipalName.split('@')[0].tostring()  
        New-MailboxExportRequest -Mailbox $temp.UserPrincipalName -Name $fileName -FilePath \\EXCH\Shared\$fileName.pst  
    }   
    

    About disable mailbox, I would suggest you use a separate script for it. It is suggest to contained them in one script(because the export request need take time to running):

    $users = import-csv C:/temp/users.csv  
    $Data = @()  
          
    foreach ($user in $users){  
        $UserName = $user.name +" "+$user.Surname  
        $temp = Get-ADUser -Filter 'Name -eq $UserName' | select UserPrincipalName  
        $fileName = $temp.UserPrincipalName.split('@')[0].tostring()  
        If ((Get-MailboxExportRequest $fileName).Status -eq "Completed"){  
            Disable-Mailbox $temp.UserPrincipalName -confirm:$false  
        }  
    }   
    

    For more professional script, it is suggested to open a ticket to Microsoft.


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


    1 person found this answer helpful.
    0 comments No comments

  2. Илья Боков 1 Reputation point
    2022-10-10T04:21:22.13+00:00

    error:

    Get-ADUser : The search filter cannot be recognized
    At C:\fired\convert-namesAD.ps1:6 char:12

    • Get-ADUser <<<< -Filter 'Name -eq $UserName'
    • CategoryInfo : NotSpecified: (:) [Get-ADUser], ADException
    • FullyQualifiedErrorId : The search filter cannot be recognized,Microsoft.ActiveDirectory.Management.Commands.Get
      ADUser

  3. Limitless Technology 39,511 Reputation points
    2022-10-10T07:15:00.037+00:00

    Hello there,

    If you need to automate these processes of comparing the Active directory and disabling them you can compare the list of resigned users with the current list of active users and then delete them. The following parameters of the AD object can be specified as an -Identity argument:

    Distinguished Name;
    GUID (objectGUID);
    objectSid;
    sAMAccountName.

    Here is some thread that might help you in getting insights about achieving this.

    Script for searching AD and comparing Givenname.Surname to their Username and exporting any incorrect usernames to a CSV file https://learn.microsoft.com/en-us/answers/questions/743408/script-for-searching-ad-and-comparing-givennamesur.html

    Script for Disabling Inactive AD Accounts https://learn.microsoft.com/en-us/answers/questions/192490/script-for-disabling-inactive-ad-accounts.html

    --------------------------------------------------------------------------------------------------------------------------------------------------

    --If the reply is helpful, please Upvote and Accept it as an answer–

    0 comments No comments