Last Updated on 2025-09-03 by william
前陣子使用 vs 2022 開發 UWP 程式,佈署程式先決條件有二:
一是安裝相關元件,二是安裝用於APP套件簽署的憑證
系統很貼心,自動幫你產生 pfx 憑證檔案。

但是憑證最多只能一年,也沒地方設定,於是我求助 gemini pro,他幫我寫了一組 powershell 程式,一開始不能套用,原因在於必須指定類型為 CodeSigning ,也就是用於程式簽署用途,再讓 gemini 重新產生程式即可。
# 設定憑證的有效期 10 年
$certExpiryDate = (Get-Date).AddYears(10)
# 設定 PFX 檔案的密碼
$password = ConvertTo-SecureString -String "密碼" -Force -AsPlainText
# 建立自簽章憑證,專用於程式碼簽署
$cert = New-SelfSignedCertificate `
-Subject "CN=william" `
-Type CodeSigning `
-NotAfter $certExpiryDate `
-KeyUsage DigitalSignature `
-KeyExportPolicy Exportable `
-CertStoreLocation "Cert:\CurrentUser\My"
# 將憑證匯出為 PFX 檔案
Export-PfxCertificate `
-Cert $cert `
-FilePath "C:\temp\MyTestApp_CodeSigning.pfx" `
-Password $password
Write-Host "程式碼簽署憑證已成功建立並匯出至 C:\temp\MyTestApp_CodeSigning.pfx"
以上範例要調整密碼、CN=william、以及有效期,這樣就能匯出 pfx 憑證,再讓 vs 2022 重新讀取,這樣建立匯出的程式,含有 xxx.cert 憑證檔案,點選右鍵,想辦法匯入到 windows 的「受信任的根憑證授權單位」,就大功告成了。

搶先發佈留言