ldx-pki.psm1

function Get-AllIssuedCertificates {
    Param(
        [Parameter(Mandatory = $true,
        ValueFromPipelineByPropertyName = $true)]
        [PKI.CertificateServices.CertificateAuthority]
        $CA,
        [Parameter(Mandatory = $false)]
        [switch]
        $OnlyValid
    )
    begin {}

    process {
        if ($OnlyValid) {
            return Get-IssuedRequest -CertificationAuthority $CA | Where-Object {$_.NotAfter -ge (Get-Date)}
        } else {
            return Get-IssuedRequest -CertificationAuthority $CA
        }
    }
    end {}
}

function Get-AllTemplates {
    return Get-CertificateTemplate
}

function Get-TemplateReport {
    Param(
        [Parameter(Mandatory = $false,
        ValueFromPipelineByPropertyName = $true)]
        [PKI.CertificateServices.CertificateAuthority]
        $CA,
        [Parameter(Mandatory = $false,
        ValueFromPipelineByPropertyName = $true)]
        [switch]
        $AllIssuedCertificates
    )
    begin {}

    process {
        if ($AllIssuedCertificates) {
            $Templates = (Get-CATemplate -CertificationAuthority $CA).Templates
        } else {
            $Templates = Get-AllTemplates
        }
        return $Templates | Select-Object Name, DisplayName, SchemaVersion, @{
            Name = "OID";
            Expression = {$_.OID.Value}
        }, @{
            Name = "OIDFriendlyName";
            Expression = {$_.OID.FriendlyName}
        }, ValidityPeriod, RenewalPeriod
    }
    end {}
}

function Get-CertificateReport {
    Param(
        [Parameter(Mandatory = $true,
        ValueFromPipelineByPropertyName = $true)]
        [object]
        $Certificates
    )

    begin {}

    process {
        return $Certificates | Select-Object Request.RequesterName, CommonName, NotBefore, NotAfter, @{
            Name = "CertificateTemplate";
            Expression = {$_.CertificateTemplate}
        }, @{
            Name = "OID";
            Expression = {$_.CertificateTemplateOID.Value}
        }
    }
    end {}
}
function Get-TemplateInUse {
    Param(
        [Parameter(Mandatory = $true,
        ValueFromPipelineByPropertyName = $true)]
        [object]
        $Certificates
    )
    begin {}

    process {
        $CertReport = Get-CertificateReport -Certificates $Certificates
        $TemplateReport = Get-TemplateReport
        return $templateReport | Where-Object {$_.Name -in $CertReport.oid -or $_.oid -in $CertReport.oid} | Select-Object DisplayName, Name, Oid
    }
    end {}
}

function Get-TemplateNotInUse {
    Param(
        [Parameter(Mandatory = $true,
        ValueFromPipelineByPropertyName = $true)]
        [object]
        $Certificates,
        [Parameter(Mandatory = $false,
        ValueFromPipelineByPropertyName = $true)]
        [switch]
        $OnlyIssuedCertificates,
        [Parameter(Mandatory = $false,
        ValueFromPipelineByPropertyName = $true)]
        [PKI.CertificateServices.CertificateAuthority]
        $CA
 
    )
    begin {}

    process {
        $CertReport = Get-CertificateReport -Certificates $Certificates
        if ($OnlyIssuedCertificates) {
            $TemplateReport = Get-TemplateReport -AllIssuedCertificates -CA $CA
        } else {
            $TemplateReport = Get-TemplateReport
        }
        return $TemplateReport | Where-Object {$_.oid -notin $CertReport.oid -and $_.name -notin $CertReport.oid} | Select-Object DisplayName, Name, Oid # -and för ca + machine
    }
    end {}
}

function Get-CertificateFromTemplate {
    Param(
        [Parameter(Mandatory = $true,
        ValueFromPipelineByPropertyName = $true)]
        [object]
        $Certificates
    )
    begin {}
    process {
        $UsedTemplates = Get-TemplateInUse -Certificates $Certificates
        $CertificateInfo = New-Object 'System.Collections.Generic.List[CertificateInfo]'
        foreach ($Template in $UsedTemplates) {
            $Oid = $Template.Oid
            $TemplateName = $Template.Name
            $count = ($Certificates | Where-Object {$_.certificateTemplate -eq $Oid -or $_.certificateTemplate -eq $TemplateName}).count
            $Cert = [CertificateInfo]::new(
                $TemplateName,
                $Oid,
                $count
            )
            $CertificateInfo.Add($cert)
        }
        return $CertificateInfo
    }
    end {}
}

function Get-CertificateFromAllTemplates {
    Param(
        [Parameter(Mandatory = $true,
        ValueFromPipelineByPropertyName = $true)]
        [object]
        $Certificates
    )
    begin {}
    process {
        $CertificateInfo = New-Object 'System.Collections.Generic.List[CertificateInfo]'
        [int]$ProgressInitialValue = 5
        [int]$ProgressGoal = 100 - $ProgressInitialValue
        [int]$ProgressStep = 1
        Write-Progress -Activity "Processing" -Status "$ProgressInitialValue% complete" -PercentComplete $ProgressInitialValue
        $TemplateReport = Get-TemplateReport
        [int]$TemplateCount = $TemplateReport.count
        foreach ($Template in $TemplateReport) {
            [float]$ProgressValue = [math]::Round($ProgressInitialValue + $ProgressStep * $ProgressGoal / $TemplateCount , 2)
            Write-Progress -Activity "Processing" -Status "$ProgressValue% complete" -PercentComplete $ProgressValue
            $ProgressStep++
            $Oid = $Template.OID
            $TemplateName = $Template.Name
            $count = ($Certificates | Where-Object {$_.certificateTemplate -eq $Oid -or $_.certificateTemplate -eq $TemplateName}).count
            $Cert = [CertificateInfo]::new(
                $TemplateName,
                $Oid,
                $count
            )
            $CertificateInfo.Add($cert)
        }
        return $CertificateInfo
    }
    end {}
}
Class CertificateInfo {
    [string]$TemplateName
    [string]$OID
    [int]$IssuedCount

    CertificateInfo (
        [string]$TemplateName,
        [string]$OID,
        [int]$IssuedCount
    )
    {
        $this.TemplateName = $TemplateName
        $this.OID = $OID
        $this.IssuedCount = $IssuedCount
    }
}