【问题标题】:Using PowerShell script below, how to check the SSL certificate validity?使用下面的 PowerShell 脚本,如何检查 SSL 证书的有效性?
【发布时间】:2019-04-22 10:50:49
【问题描述】:

我需要修改下面的脚本,这样我才能得到 AD 服务器的列表,然后检查服务器中的任何 SSL 证书的有效性。

注意:服务器可能运行也可能不运行 IIS,这就是为什么我不确定如何正确执行。

$ComputerName = Get-ADComputer -Filter {Enabled -eq $True} -SearchBase "OU=Servers,OU=Production,DC=Domain,DC=com"
[CmdletBinding()]
param(
    [parameter(Mandatory, ValueFromPipeline)][string[]]$ComputerName,
    [int]$TCPPort = 443,
    [int]$Timeoutms = 3000
)

process {
    foreach ($computer in $computerName) {
        $port = $TCPPort
        write-verbose "$computer`: Connecting on port $port"
        [Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
        $req = [Net.HttpWebRequest]::Create("https://$computer`:$port/")
        $req.Timeout = $Timeoutms
        try {$req.GetResponse() | Out-Null} catch {write-error "Couldn't connect to $computer on port $port"; continue}
        if (!($req.ServicePoint.Certificate)) {write-error "No Certificate returned on $computer"; continue}
        $certinfo = $req.ServicePoint.Certificate

        $returnobj = [ordered]@{
            ComputerName = $computer;
            Port         = $port;
            Subject      = $certinfo.Subject;
            Thumbprint   = $certinfo.GetCertHashString();
            Issuer       = $certinfo.Issuer;
            SerialNumber = $certinfo.GetSerialNumberString();
            Issued       = [DateTime]$certinfo.GetEffectiveDateString();
            Expires      = [DateTime]$certinfo.GetExpirationDateString();
        }

        new-object PSCustomObject -Property $returnobj
    }
}

【问题讨论】:

  • 这个脚本有什么问题?
  • 这是错误:$ComputerName = Get-ADComputer -Filter {Enabled -eq $True} -SearchBas ... + ~~~~~~~~~~~~~ Unexpected token '表达式或语句中的 $ComputerName'。
  • 据我所知,[CmdletBinding()] 行之前不能有任何代码。你有一个process 块...该行看起来应该是process 块或begin 块中的第一件事。

标签: windows powershell scripting active-directory windows-scripting


【解决方案1】:

我不确定您是否忘记将函数实例化放在顶部,但以下应该是 PowerShell 中高级函数的正确格式。您还可以使用 Get-ADComputer cmdlet 为参数 $ComputerName 指定默认值。试试这个看看是否可行。

function Get-ADComputerCert {
    [CmdletBinding()]
    param(
        [int]$TCPPort = 443,
        [int]$Timeoutms = 3000
    )

    process {
        $ComputerName = (Get-ADComputer -Filter {Enabled -eq $True} -SearchBase "OU=Servers,OU=Production,DC=Domain,DC=com").Name
        foreach ($computer in $computerName) {
            $port = $TCPPort
            write-verbose "$computer`: Connecting on port $port"
            [Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
            $req = [Net.HttpWebRequest]::Create("https://$computer`:$port/")
            $req.Timeout = $Timeoutms
            try {$req.GetResponse() | Out-Null} catch {write-error "Couldn't connect to $computer on port $port"; continue}
            if (!($req.ServicePoint.Certificate)) {write-error "No Certificate returned on $computer"; continue}
            $certinfo = $req.ServicePoint.Certificate

            $returnobj = [ordered]@{
                ComputerName = $computer;
                Port         = $port;
                Subject      = $certinfo.Subject;
                Thumbprint   = $certinfo.GetCertHashString();
                Issuer       = $certinfo.Issuer;
                SerialNumber = $certinfo.GetSerialNumberString();
                Issued       = [DateTime]$certinfo.GetEffectiveDateString();
                Expires      = [DateTime]$certinfo.GetExpirationDateString();
            }

            new-object PSCustomObject -Property $returnobj
        }
    }
}

【讨论】:

  • 仍然无法工作,彼得。在 line:4 char:75 + ...meter(Mandatory, ValueFromPipeline)][string[]]$ComputerName = Get-ADC ... + ~ 在 '=' 之后缺少表达式。
  • @SeniorSystemsEngineer 尝试删除参数上的 [string[]] 标识符。由于 get-adcomputer 检索对象,而不是字符串。
  • @SeniorSystemsEngineer 另外,您可能需要评估 Get-ADComputer,因为 PowerShell 对您咆哮,因为在 $ComputerName 之后没有完整的表达式。为此,只需将 Cmdlet 及其参数括在括号中即可。
  • 我已经编写了一个函数来执行此操作,它使用 tcpclient 类读取 SSL 流,因此将从任何 IP 或 FQDN 获取证书详细信息。您可以简单地将 get-adcomputer 结果通过管道传输给它。如果你想看看我可以在这里或我的博客上为你发帖
  • @SeniorSystemsEngineer 我刚刚更新了我的答案并修复了 $ComputerName 变量以获取名称值字符串。现在应该可以了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-07-02
  • 1970-01-01
  • 2023-03-19
  • 1970-01-01
  • 1970-01-01
  • 2017-12-10
  • 1970-01-01
相关资源
最近更新 更多