Sitecore And New-SelfSignedCertificate Issues with PowerShell 7

Sitecore developers often use PowerShell to create new self signed certificates in their work. New-SelfSignedCertificate and Export-PfxCertificate worked great in PowerShell 5.1, but PowerShell 7 has an open issue making it difficult. Fortunately, there is a workaround. Merging this into a snippet from Mihály Árvai gives us the following code:

Import-Module PKI -UseWindowsPowerShell

$session = Get-PSSession -Name WinPSCompatSession
$secPw = Read-Host -Prompt "Enter password" -AsSecureString
$pfxPath = '.\SitecoreIdentityTokenSigning.pfx'
$outPath = '.\secrets\sitecore-identitycertificate.txt'

Invoke-Command $session {
    $cert = New-SelfSignedCertificate -DnsName "localhost" `
            -FriendlyName "Sitecore Identity Token Signing" `
            -NotAfter (Get-Date).AddYears(5) `
            -KeyExportPolicy 'Exportable'
    Export-PfxCertificate -Cert $cert `
            -Path $pfxPath `
            -Password $using:SecPw | Out-Null 
}
[System.Convert]::ToBase64String(
    [System.IO.File]::ReadAllBytes(
        (Get-Item $pfxPath))
) | Out-File -Encoding ascii `
             -NoNewline `
             -Confirm `
             -Path $outPath

Advertisement

Powershell for SQL Server

For me, dbatools is the best tool around for scripting SQL Sever tasks. It is a PowerShell module made by DBAs for doing DBA stuff. I’m not a DBA, but I need to script database steps in our deployment process at work. I have previously used SQLPS to do some tasks, but finding information was hard and getting it to run reliably in different environments was difficult. SQLPS is part of the SQL Server installation so you need to have access to that to get it. The updated module, SqlServer is available from the PowerShell Gallery which is better when you need to install it, but I still had trouble getting my scripts to run reliably. With dbatools, success has been pretty easy.

Seriously, hop on over to dbatools.io and check it out. Scroll down on the home page and read the endorsements. And then try it out. Use the awesome documentation to see how PowerShell documentation should be done, as well as inform you on how to use the commands. If you need to script SQL Server using PowerShell, this is probably the tool you want to use.

Go a step further and get the source code. Use it like I do, as a resource to improve the way you code PowerShell.

Happy New Year

This morning I woke up to the happy noise of my 18 month old granddaughter playing with her grandma. They are two of the people I love most in the world and the memory of the last week are fresh in my mind. It was a good week with quality time with our family. Our granddaughter stayed with us and her three year old brother stayed with my daughter-in-law’s parents while my son and daughter-in-law were out of town. I’m glad to have started the year off with her. I hope your year starts as well as mine.