Functions/SSL/Get-CertCSPKeyInfo.ps1
Function Get-CertCSPKeyInfo { [cmdletbinding()] Param () DynamicParam { # Get available CSPs $CSPs = (Get-CertCSPs) # Instantiate Runtime Parameter Dictionary, Attach Runtime Parameters, and return $RuntimeParameterDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary $RuntimeParameterDictionary.Add('CSPName', (New-DynamicParameter -ParamName 'CSPName' -ValueType string -Dataset $CSPs.name -Mandatory $false)) return $RuntimeParameterDictionary } Begin { # Convert Runtime Parameter Dictionary into Available Constants foreach ($key in $RuntimeParameterDictionary.keys){New-Variable -Name $key -Value $RuntimeParameterDictionary.$key.value} # Select CSP(s) $TargetCSPs = if ($CSPName){[array]($CSPs | where name -eq $CSPName)} else {[array]($CSPs)} } Process { foreach ($CSP in $TargetCSPs) { $CSPKeyRaw = Certutil -csp "$($CSP.name)" -key if ($CSPKeyRaw.count -gt 1) { $CSPKeyTrim = $CSPKeyRaw[1..($CSPKeyRaw.count-3)] $CSPKeyRaw2 = Foreach ($line in $CSPKeyTrim) { if ($Line -like ""){$line = '&'} $Line } $CSPKeyArray = $CSPKeyRaw2 -join '@' -split '&' foreach ($CSPKey in $CSPKeyArray) { $CSPKeyData = ($CSPKey -split '@').trim() -notlike "" $Valid = if($CSPKeyData[0] -notlike "cuLoadKey:*"){$true}else{$false} $CSPKeyDataTrim = $CSPKeyData -notlike "cuLoadKey:*" [pscustomobject]([ordered]@{ Provider = $CSP.Name ProviderType = $CSP.Type KeyContainerName = $CSPKeyDataTrim[0] UniqueContainerName = $CSPKeyDataTrim[1] KeyAlgorithmGroup = $CSPKeyDataTrim[3] KeyAlgorithm = $CSPKeyDataTrim[2] Valid = $Valid }) } } } } } |