0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

PowerShell 自己署名証明書 スクリプトに埋め込む方法2 cer

Last updated at Posted at 2024-01-26

.cer ファイルを PowerShell スクリプトに埋め込むことは通常は適切ではありませんが、.cer ファイルを使用して自己署名証明書を作成し、それをスクリプト内で取得する方法を示します。そして、そのスクリプトを実行する方法も以下に説明します。

1. 自己署名証明書の作成とエクスポート

# 証明書の発行者とサブジェクトを指定
$certParams = @{
    Subject = "CN=YourCertificateName"
    KeyAlgorithm = "RSA"
    KeyLength = 2048
}

# 自己署名証明書の作成
$certificate = New-SelfSignedCertificate @certParams

# 証明書を CER フォーマットでエクスポートして、.cer ファイルに保存
$cerFilePath = "C:\Path\To\Your\Certificate.cer"
$certificate | Export-Certificate -FilePath $cerFilePath -Type CERT

# エクスポートした .cer ファイルの場所を確認
Write-Host "Certificate exported to: $cerFilePath"

2. PowerShell スクリプトに埋め込む

作成した .cer ファイルを PowerShell スクリプトに埋め込むことはできませんが、そのファイルへのパスをスクリプトに記述することはできます。

# .cer ファイルのパス
$cerFilePath = "C:\Path\To\Your\Certificate.cer"

# .cer ファイルから証明書を読み込み
$certificate = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2
$certificate.Import($cerFilePath)

# 取得した証明書を利用する
# 例: HTTPS サーバーの設定など
Write-Host "Certificate Subject: $($certificate.Subject)"

$cert=Get-ChildItem -Path Cert:\CurrentUser\My -CodeSigningCert
Set-AuthenticodeSignature -FilePath PsTestInternet2.ps1 -Certificate $cert

3. スクリプトの実行

PowerShell スクリプトを実行するには、PowerShell コンソールまたはターミナルでスクリプトのパスを指定します。例:

.\YourScript.ps1

ただし、自己署名証明書はセキュリティのため、慎重に扱う必要があります。適切な手順とセキュリティプラクティスに従って証明書を管理してください。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?