如何在PowerShell/PowerShell ISE脚本中传入SSL证书密码
给PowerShell脚本添加PFX证书密码的方法
要在脚本中嵌入证书密码,需要将密码转换为PowerShell的SecureString类型,再传递给Get-PfxCertificate的-Password参数,修改后的脚本如下:
$certPath = "D:\Folder\SSLCertName.pfx" # 将明文密码转换为安全字符串 $certPassword = ConvertTo-SecureString "你的实际证书密码" -AsPlainText -Force # 加载带密码的证书 $cert = Get-PfxCertificate -FilePath $certPath -Password $certPassword Invoke-RestMethod -Method Get -Uri 'https://API_URL' -Certificate $cert
关键说明:
- 把代码中的
"你的实际证书密码"替换为证书的真实密码 -AsPlainText -Force是必须的参数组合,用于将明文密码转换为PowerShell要求的SecureString格式- 若担心硬编码密码不安全,可改用交互式输入方式,避免密码暴露在脚本中:
$certPath = "D:\Folder\SSLCertName.pfx" $certPassword = Read-Host "请输入证书密码" -AsSecureString $cert = Get-PfxCertificate -FilePath $certPath -Password $certPassword Invoke-RestMethod -Method Get -Uri 'https://API_URL' -Certificate $cert
内容的提问来源于stack exchange,提问作者Craig
相关产品推荐
相关产品推荐

