Created
July 13, 2021 19:12
-
-
Save SoulOfUniverse/c54754c4aaf9014a40940483f9283063 to your computer and use it in GitHub Desktop.
Sitecore Powershell Add User To A Certificate
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
param( | |
[string]$userName, | |
[string]$permission, | |
[string]$certStoreLocation, | |
[string]$certThumbprint | |
); | |
# check if certificate is already installed | |
$certificateInstalled = Get-ChildItem cert:$certStoreLocation | Where thumbprint -eq $certThumbprint | |
# download & install only if certificate is not already installed on machine | |
if ($certificateInstalled -eq $null) | |
{ | |
$message="Certificate with thumbprint:"+$certThumbprint+" does not exist at "+$certStoreLocation | |
Write-Host $message -ForegroundColor Red | |
exit 1; | |
}else | |
{ | |
try | |
{ | |
$rule = new-object security.accesscontrol.filesystemaccessrule $userName, $permission, allow | |
$root = "c:\programdata\microsoft\crypto\rsa\machinekeys" | |
$l = ls Cert:$certStoreLocation | |
$l = $l |? {$_.thumbprint -like $certThumbprint} | |
$l |%{ | |
$keyname = $_.privatekey.cspkeycontainerinfo.uniquekeycontainername | |
$p = [io.path]::combine($root, $keyname) | |
if ([io.file]::exists($p)) | |
{ | |
$acl = get-acl -path $p | |
$acl.addaccessrule($rule) | |
echo $p | |
set-acl $p $acl | |
} | |
} | |
} | |
catch | |
{ | |
Write-Host "Caught an exception:" -ForegroundColor Red | |
Write-Host "$($_.Exception)" -ForegroundColor Red | |
exit 1; | |
} | |
} | |
exit $LASTEXITCODE |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment