Skip to content

Instantly share code, notes, and snippets.

@potatoqualitee
Created August 5, 2025 21:51
Show Gist options
  • Save potatoqualitee/62fd712e748e7564cf48a953a95130be to your computer and use it in GitHub Desktop.
Save potatoqualitee/62fd712e748e7564cf48a953a95130be to your computer and use it in GitHub Desktop.
Import-Dbatools
function Import-Dbatools {
<#
.SYNOPSIS
Imports the dbatools module after trusting its code signing certificate.
.DESCRIPTION
This function checks if the dbatools module's signing certificate is trusted.
If not, it adds the certificate to the current user's Trusted Publishers store,
then imports the module. This is particularly useful when working with
AllSigned execution policies and the new Azure Trusted Signing certificate.
Thanks to Jordan Borean for helping with this function!
.NOTES
Related Blog: https://blog.netnerds.net/2025/08/dbatools-azure-trusted-signing/
dbatools recently transitioned to Azure Trusted Signing (Azure Code Signing)
which provides a more modern and secure code signing solution. This function
ensures the new certificate is properly trusted in your environment.
.EXAMPLE
Import-Dbatools
Checks the certificate trust status and imports dbatools, adding the cert
to Trusted Publishers if needed.
#>
# Check and trust the certificate if needed
$moduleBase = (Get-Module dbatools -ListAvailable | Select-Object -First 1).ModuleBase
$moduleManifest = Join-Path $moduleBase dbatools.psd1
$cert = (Get-AuthenticodeSignature -FilePath $moduleManifest).SignerCertificate
if (-not (Test-Path -Path "Cert:\CurrentUser\TrustedPublisher\$($cert.Thumbprint)")) {
Write-Host "Trusting new dbatools certificate..." -ForegroundColor Yellow
Write-Host "Certificate Subject: $($cert.Subject)" -ForegroundColor Cyan
Write-Host "Certificate Thumbprint: $($cert.Thumbprint)" -ForegroundColor Cyan
$store = Get-Item Cert:\CurrentUser\TrustedPublisher
$store.Open('ReadWrite')
$store.Add($cert)
$store.Close()
Write-Host "Certificate successfully added to Trusted Publishers" -ForegroundColor Green
} else {
Write-Host "dbatools certificate already trusted" -ForegroundColor Green
}
# Now import the module
Import-Module dbatools
Write-Host "dbatools module imported successfully" -ForegroundColor Green
}
# Usage: Simply call the function to handle certificate trust and import
# Import-Dbatools
# For more information about dbatools' transition to Azure Trusted Signing:
# https://blog.netnerds.net/2025/08/dbatools-azure-trusted-signing/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment