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

mark terry 65 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

jsmith@test.com

mjones@test.com

sjohnson@test.com

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

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

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

"D:\Certificates\sjohnson@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\jsmith@test.com.cer")

$certArray = New-Object System.Collections.ArrayList

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

Set-Mailbox -Identity jsmith@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!

Microsoft Exchange Online Management
Microsoft Exchange Online Management
Microsoft Exchange Online: A Microsoft email and calendaring hosted service.Management: The act or process of organizing, handling, directing or controlling something.
4,599 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,617 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Rich Matheisen 46,971 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.

    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.