実現したいこと
Powershellスクリプトで証明書のインストールを行いたい
前提
社内の全PCにデジタル証明書のインストールを行う必要性が出てきました。
ほとんどのPCは管理者権限が付いていないのですが、それを一時的に回避して管理者権限を付けたかったのですが、ネットで拾った情報に従い、行ってみたのですが、うまくいかず諦めました。
ADのGPOで証明書のインストールがテンプレートで入っているのですが、何度やってもうまくいかないので、powershellを使う方法があることを知り、試みています。
スクリプトと証明書は、該当者にアクセス権のあるファイルサーバー上に置いており、最終的には、各利用者にショートカットを配布して実行して貰う方法で考えています。
配布結果を資産管理ツールで把握できるよう、レジストリに書き込みを行い、チェックする処理も入れています。
発生している問題・エラーメッセージ
管理者権限を付けた状態で、以下のスクリプトはとりあえず動作するようなのですが、
管理者権限を付けたPCでもエラーが出て動作しないPCがあります。
証明書をインストールするために証明書が要るというのも変な話なので、そもそもやり方がおかしいのかもしれませんね。
ファイル \\svr\cert\cert_install.ps1を読み込めません。 ファイル \\svr\cert\cert_install.ps1はデジタル署名されていません。このスクリプトは現在のシステムで実行できません。
該当のソースコード
powershell
1# 管理者権限の一時的付与 -> うまくいきませんでした。 2if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole("Administrators")) { Start-Process powershell.exe "-File `"$PSCommandPath`"" -Verb RunAs; exit } 3Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process 4# スクリプトのブロック解除 5#Unblock-File -Path "\\svr\cert\cert_install.ps1" 6# 確認する証明書のサムプリント 7$targetThumbprint = "*************" 8# チェックする証明書ストア 9$certificateStoreLocation = "Cert:\LocalMachine\Root" 10 11# インポートする電子証明書のパス 12$certificatePath = "\\svr\cert\CA***.crt" 13# インポートする際に指定するパスワード 14$certificatePassword = "hogehoge" 15 16# レジストリ情報 17$registryPath = "HKCU:\Software\myapp\Security" 18$propertyName = "mycert" 19 20# レジストリ情報の存在チェック 21$propertyExists = Get-ItemProperty -LiteralPath $registryPath -Name $propertyName -ErrorAction SilentlyContinue 22if ($propertyExists) { 23 # レジストリが見つかった場合 24 Write-Host "Resistory already exists." 25 # 証明書がインストールされているかどうかチェック 26 $certificate = Get-Item -LiteralPath (Join-Path $certificateStoreLocation $targetThumbprint) -ErrorAction SilentlyContinue 27 # 証明書が見つからなかった場合はインストール 28 if (-not $certificate) { 29 Write-Host "Certificate is not installed." 30 # 証明書をインストール 31 $certificate = Import-PfxCertificate -FilePath $certificatePath -CertStoreLocation Cert:\CurrentUser\My -Password (ConvertTo-SecureString -String $certificatePassword -AsPlainText -Force) 32 Write-Host "Thank you for your cooperation!! The certificate has been installed." 33 } else { 34 Write-Host "Thank you for your cooperation!! The certificate has been installed." 35 } 36} else { 37 # レジストリが見つからなかった場合 38 Write-Host "Resistory is not exists." 39 # 証明書をインストール 40 $certificate = Import-PfxCertificate -FilePath $certificatePath -CertStoreLocation Cert:\CurrentUser\My -Password (ConvertTo-SecureString -String $certificatePassword -AsPlainText -Force) 41 # レジストリキーが存在するか確認 42 if (-not (Test-Path $registryPath)) { 43 # レジストリキーが存在しない場合は作成 44 New-Item -Path $registryPath -Force 45 } 46 # レジストリプロパティが存在するか確認 47 if (-not (Test-Path "$registryPath\$propertyName")) { 48 # レジストリプロパティが存在しない場合は作成 49 New-ItemProperty -Path $registryPath -Name $propertyName -PropertyType 'Dword' -Value 1 -Force 50 } else { 51 Write-Host "Oops, The certificate is already installed." 52 } 53}
試したこと
先頭に、
powershell
1# スクリプトのブロック解除 2Unblock-File -Path "\\svr\cert\cert_install.ps1"
といれてみましたが、結果は同じでした。
補足情報(FW/ツールのバージョンなど)
ClientはWindows10、
スクリプトの置き場所はWindows2019です。
回答2件
あなたの回答
tips
プレビュー