How do I pass an array or list as a parameter to a PowerShell function?

前端 未结 2 1286
感情败类
感情败类 2021-02-19 00:13

I am writing a PowerShell script to get a list of certificates which are getting expired within 30 days. The script is working, but the problem is there are too many app and pre

相关标签:
2条回答
  • 2021-02-19 00:29

    Working with PS 4.0 or later, it´s possible to define as CheckCert([array]$ComputerNames) as well.

    0 讨论(0)
  • 2021-02-19 00:40

    Try this:

    function CheckCert([string[]]$ComputerNames)
    {
        $deadline = (Get-Date).AddDays($global:threshold) # Set deadline date
        foreach ($computer in $ComputerNames)
        {
            Invoke-Command -ComputerName $Computer { Dir Cert:\LocalMachine\My } |
            foreach {
                If ($_.NotAfter -le $deadline)
                {
                    $_ | Select Issuer, Subject, NotAfter, @{N="Expires In (Days)";E={($_.NotAfter - (Get-Date)).Days}}
                }
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题