-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
261d546
commit b32c22c
Showing
2 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
$executionCode = { | ||
param ( | ||
$Configuration | ||
) | ||
|
||
$filePath = Join-Path -Path 'VMDeploy:\Resources' -ChildPath $Configuration.FileName | ||
if (-not (Test-Path -Path $filePath)) { | ||
Write-PSFMessage -Level Warning -Message "Certificate file not found in the VMDeploy package! Ensure the $($Configuration.FileName) certificate is deployed as a resource!" | ||
return | ||
} | ||
$fullFilePath = (Get-Item -Path $filePath).FullName | ||
try { $certificate = [System.Security.Cryptography.X509Certificates.X509Certificate2]::CreateFromCertFile($fullFilePath) } | ||
catch { | ||
Write-PSFMessage -Level Warning -Message "Error opening certificate $($Configuration.FileName)" -ErrorRecord $_ | ||
return | ||
} | ||
|
||
try { | ||
$store = [System.Security.Cryptography.X509Certificates.X509Store]::new( | ||
$Configuration.Store, | ||
[System.Security.Cryptography.X509Certificates.StoreLocation]::LocalMachine | ||
) | ||
$store.Open('ReadWrite') | ||
} | ||
catch { | ||
Write-PSFMessage -Level Warning -Message "Error accessing certificate store $($Configuration.Store)" -ErrorRecord $_ | ||
return | ||
} | ||
|
||
try { $store.Add($certificate) } | ||
catch { | ||
Write-PSFMessage -Level Warning -Message "Error writing certificate $($Configuration.FileName) to certificate store $($Configuration.Store)" -ErrorRecord $_ | ||
return | ||
} | ||
} | ||
|
||
$validationCode = { | ||
param ( | ||
$Configuration | ||
) | ||
|
||
$filePath = Join-Path -Path 'VMDeploy:\Resources' -ChildPath $Configuration.FileName | ||
if (-not (Test-Path -Path $filePath)) { | ||
Write-PSFMessage -Level Warning -Message "Certificate file not found in the VMDeploy package! Ensure the $($Configuration.FileName) certificate is deployed as a resource!" | ||
return $false | ||
} | ||
$fullFilePath = (Get-Item -Path $filePath).FullName | ||
try { $certificate = [System.Security.Cryptography.X509Certificates.X509Certificate2]::CreateFromCertFile($fullFilePath) } | ||
catch { | ||
Write-PSFMessage -Level Warning -Message "Error opening certificate $($Configuration.FileName)" -ErrorRecord $_ | ||
return $false | ||
} | ||
|
||
try { | ||
$store = [System.Security.Cryptography.X509Certificates.X509Store]::new( | ||
$Configuration.Store, | ||
[System.Security.Cryptography.X509Certificates.StoreLocation]::LocalMachine | ||
) | ||
$store.Open('ReadOnly') | ||
} | ||
catch { | ||
Write-PSFMessage -Level Warning -Message "Error accessing certificate store $($Configuration.Store)" -ErrorRecord $_ | ||
return $false | ||
} | ||
$store.Certificates.ThumbPrint -contains $certificate.ThumbPrint | ||
} | ||
|
||
$PreDeploymentCode = { | ||
param ( | ||
$Configuration, | ||
|
||
$WorkingDirectory | ||
) | ||
} | ||
|
||
$param = @{ | ||
Name = 'CertDeploy' | ||
ScriptBlock = $executionCode | ||
Validate = $validationCode | ||
Description = 'Deploys a certificate to the specified certificate store' | ||
PreDeploymentCode = $PreDeploymentCode | ||
ParameterMandatory = @( | ||
'FileName' | ||
'Store' | ||
) | ||
ParameterOptional = @( | ||
) | ||
Tag = 'certificate', 'pki' | ||
} | ||
Register-VMGuestAction @param |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters