Я пытаюсь изменить правила доступа для конкретного $cert
с помощью следующего кода:
$csp = New-Object System.Security.Cryptography.CspParameters (
$cert.PrivateKey.CspKeyContainerInfo.ProviderType,
$cert.PrivateKey.CspKeyContainerInfo.ProviderName,
$cert.PrivateKey.CspKeyContainerInfo.KeyContainerName)
$csp.Flags = [System.Security.Cryptography.CspProviderFlags]::UseExistingKey -bor [System.Security.Cryptography.CspProviderFlags]::UseMachineKeyStore
$csp.CryptoKeySecurity = $cert.PrivateKey.CspKeyContainerInfo.CryptoKeySecurity
$csp.KeyNumber = $cert.PrivateKey.CspKeyContainerInfo.KeyNumber
$access = New-Object System.Security.AccessControl.CryptoKeyAccessRule (
$identity,
[System.Security.AccessControl.CryptoKeyRights]::GenericRead,
[System.Security.AccessControl.AccessControlType]::Allow)
$csp.CryptoKeySecurity.AddAccessRule($access)
Но в последней строке выдается исключение, потому что $csp.CryptoKeySecurity
равно нулю. Во время отладки оказалось, что $cert.PrivateKey.CspKeyContainerInfo.CryptoKeySecurity
также является нулевым. Однако самое сложное в том, что это происходит только на 1 из 5 машин, не зависит ни от версии ОС, ни от версии PS, происходит только в нашей среде, почему ...? К вашему сведению $cert.PrivateKey
не является нулевым, ни $cert.PrivateKey.CspKeyContainerInfo
.