how to combine 2 ForEach in exchange shell

Ilya Bokov 145 Reputation points
2023-09-22T08:42:54.3333333+00:00

Hello everyone.

Can you help me combine 2 scripts in one powershell command

  1. export mailbox from csv
  2. in status completed - disable user
  3. clean username from all distributiongroups

all my scripts is working, but i need in 1 task. Thank you

i think this script need to be out-file with sAMAccountName of users, but i don't know how)

$users = import-csv "C:\csv\СписокУволенныхСотрудников.csv" $Data = @() Import-Module ActiveDirectory Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn foreach ($user in $users){
$UserName = $user.name +" "+$user.Surname
$temp = Get-ADUser -Filter 'Name -eq $UserName' | select sAMAccountName

and then

  1. starts from export mailbox from csv script,

$users = import-csv "C:\csv\СписокУволенныхСотрудников.csv" $Data = @() Import-Module ActiveDirectory Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn foreach ($user in $users){
New-MailboxExportRequest -Mailbox $temp.sAMAccountName -Name $user -FilePath \exchange\fired$user.pst }

then need disable mailbox and remove completed list

Import-Module ActiveDirectory Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn
foreach ($user in $users){

Disable-ADAccount -Identity $($sAMAccountName)
Get-ADUser -Identity $sAMAccountName | Move-ADObject -TargetPath "OU=DisabledAcounts,DC=next,DC=local" If ((Get-MailboxExportRequest $fileName).Status -eq "Completed"){
Disable-Mailbox -Identity $sAMAccountName -confirm:$false
Get-MailboxExportRequest -Status Completed | Remove-MailboxExportRequest -Confirm:$false
}
}

and combine in 1 powershell with finish script remove from all distribgroups by list

ForEach ($Group in $DistributionGroups)
    {
        If ((Get-DistributionGroupMember $Group.Name | Select -Expand PrimarySmtpAddress) -contains $($sAMAccountName))
        {
            Remove-DistributionGroupMember -Identity $Group.Name -Member $($sAMAccountName) -Confirm:$false
            Write-host "Removed user from group '$Group'" -f Green
        }
    }  


IF anyone can help me - i'll answer immediately.

Exchange Server
Exchange Server
A family of Microsoft client/server messaging and collaboration software.
1,289 questions
Outlook
Outlook
A family of Microsoft email and calendar products.
3,804 questions
Exchange Server Development
Exchange Server Development
Exchange Server: A family of Microsoft client/server messaging and collaboration software.Development: The process of researching, productizing, and refining new or existing technologies.
544 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,627 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,499 questions
0 comments No comments
{count} votes

Accepted answer
  1. Luis Arias 6,791 Reputation points
    2023-09-22T12:35:57.1166667+00:00

    Hi Ilya,

    I Prepare something for you, the logic works(Also you can verify that without the exchange cmdlets)

    Import-Module ActiveDirectory
    Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn
    $users = import-csv "C:\csv\СписокУволенныхСотрудников.csv"
    function exportMailbox {
        param (
            $sam
        )
        New-MailboxExportRequest -Mailbox $sam `
            -Name $user -FilePath \exchange\fired$sam.pst
    }
    function getDgroups {
        param (
            $sam
        )
        $groups=`
            $(Get-DistributionGroup -ResultSize Unlimited | Where-Object { $_.Members -like "*,$($sam)" })
        $groupMemberships = $groups | Select-Object Name
        return $groupMemberships
    }
    function disableuser {
        param (
            $sam
        )
        Write-Host("Disable identity "+$sam)
        Disable-ADAccount -Identity $sam
        Write-Host("Move to OU disable "+$sam)
        Get-ADUser -Identity $sam | Move-ADObject -TargetPath "OU=DisabledAcounts,DC=next,DC=local"
        $fileName = $sam+"PST"
        if ((Get-MailboxExportRequest $fileName).Status -eq "Completed") {
            Write-Host("Disable and remove mailbox"+$sam)
            Disable-Mailbox -Identity $sam -confirm:$false
            Get-MailboxExportRequest -Status Completed | Remove-MailboxExportRequest -Confirm:$false
        }
    }
    
    foreach ($user in $users){
        Write-Host("Get SamaccountName "+$user.name)
        $username = Get-ADUser -Filter 'Name -eq $UserName' | Select-Object sAMAccountName
        exportMailbox($username)
        $groups=getDgroups($username)
        foreach ($group in $groups) {
            Remove-DistributionGroupMember -Identity $group -Member $($username) -Confirm:$false
            Write-Host("removing "+$username+" from group: "+$group)
        }
        disableuser($username)
    }
    
    

    Verify and customize with your requirement and let me know any question.

    Cheers,

    Luis Arias


    If the information helped address your question, please Accept the answer.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Ilya Bokov 145 Reputation points
    2023-10-19T09:21:11.0133333+00:00

    all working. thanks !!!

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.