ответ

7

Вот ответ.

Созданный файл сценария PowerShell AddUserToCertificate.ps1

Вот содержание для файла сценария.

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 

Теперь запустите его как часть развертывания. Пример для запуска над сценарием в окне консоли PowerShell.

C:\>.\AddUserToCertificate.ps1 -userName testuser1 -permission read -certStoreLocation \LocalMachine\My -certThumbprint 1fb7603985a8a11d3e85abee194697e9784a253 

этот пример даст прочитать разрешение пользователя testuser1 на сертификат, установленный в \ LocalMachine \ My и имеет большой палец печати 1fb7603985a8a11d3e85abee194697e9784a253

Если вы используете ApplicationPoolIdentity то имя пользователя будет «IIS AppPool \ AppPoolNameHere»

Примечание: Вам нужно будет использовать '', так как между IIS и AppPool существует пробел.

+0

благодарю вас так много! – ncomputers

+0

Большое спасибо, отличный сценарий. если кому-то нужно дать полное разрешение на управление, это действительно - _fullcontrol_ – iBobb

+0

У меня есть старая машина Win2008 с PowerShell V2. Мне пришлось использовать '$ certificateInstalled = Get-ChildItem cert: $ certStoreLocation | Где {$ _. Thumbprint -eq $ certThumbprint} 'в строке 8 (первый оператор) – Tony

 Смежные вопросы

  • Нет связанных вопросов^_^