Assign S/MIME certificates (.cer files) to multiple Exchange Mailboxes using Set-Mailbox cmdlet and UserSMIMECertificate parameter

mark terry 185 Reputation points
2024-11-16T17:13:56.38+00:00

Hi all,

I have a CSV input file which contains a number of mailbox PrimarySMTPAddress values e.g.

PrimarySMTPAddress

******@test.com

******@test.com

******@test.com

I also have corresponding .cer (S/MIME Public certificates) stored on a file server for each mailbox i.e.

"D:\Certificates******@test.com.cer"

"D:\Certificates******@test.com.cer"

"D:\Certificates******@test.com.cer"

I have some PowerShell code which works to assign the .cer certificate to an individual mailbox using the Set-Mailbox cmdlet (see below):

$cert=New-Object System.Security.Cryptography.X509Certificates.X509Certificate2("C:\temp******@test.com.cer")

$certArray = New-Object System.Collections.ArrayList

$certArray.Insert(0,$cert.GetRawCertData())

*Set-Mailbox -Identity *****@test.com -UserSMIMECertificate $certArray

What I would like to have is a PowerShell script which will read the contents of the input.csv file (which contains the PrimarySMTPAddress values of each mailbox) and then assign (using the Set-Mailbox cmdlet and UserSMIMECertificate parameter) the .cer files to each of the related mailboxes.

Thanks in advance!

Exchange Online
Exchange Online
A Microsoft email and calendaring hosted service.
6,195 questions
Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 47,906 Reputation points
    2024-11-16T19:27:24.2033333+00:00

    Something like this should work:

    ForEach ($u in (Import-CSV C:\MyStuff\PrimarySMTPAddress.csv)){
        $p = Join-Path C:\Temp -ChildPath ("{0}.cer" -f $u.PrimarySMTPAddress)
        $cert=New-Object System.Security.Cryptography.X509Certificates.X509Certificate2($p)
        $certArray = New-Object System.Collections.ArrayList
        $certArray.Insert(0,$cert.GetRawCertData())
        Set-Mailbox -Identity $u.PrimarySMTPAddress -UserSMIMECertificate $certArray
    }
    

    Adjust the path and file names accordingly.

    The code's untested as I have no access to an Exchange server or any certificates.

    2 people found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Anonymous
    2024-11-18T06:19:23.5733333+00:00

    Hi, @mark terry

    I noticed that you are using the Exchange Online tag, please understand that Exchange Online is currently focused on Exchange and mail flow issues, not on scripts.

    Based on my personal search, the following script is just for your reference:

    # Define the path to the CSV file
    $csvPath = "C:\path\to\input.csv"
     
    # Import the CSV file
    $mailboxes = Import-Csv -Path $csvPath
     
    # Loop through each mailbox in the CSV file
    foreach ($mailbox in $mailboxes) {
        # Get the PrimarySMTPAddress
        $primarySMTPAddress = $mailbox.PrimarySMTPAddress
     
        # Define the path to the .cer file
        $certPath = "D:\Certificates\$primarySMTPAddress.cer"
     
        # Load the certificate
        $cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2($certPath)
     
        # Create an array list and add the certificate data
        $certArray = New-Object System.Collections.ArrayList
        $certArray.Insert(0, $cert.GetRawCertData())
     
        # Assign the certificate to the mailbox
        Set-Mailbox -Identity $primarySMTPAddress -UserSMIMECertificate $certArray
    }
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".


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.