Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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 pres servers, and I want to minimize the script code. My function is:

function CheckCert($ComputerNames)
{
    $deadline = (Get-Date).AddDays($global:threshold) # Set deadline date
    Invoke-Command -ComputerName $ComputerNames { Dir Cert:\LocalMachine\My } |
        foreach {
            If ($_.NotAfter -le $deadline)
            {
                $_ | Select Issuer, Subject, NotAfter, @{Label="Expires In (Days)";Expression={($_.NotAfter - (Get-Date)).Days}}
            }
        }
}

And I am calling this function like:

Switch ($xMenu1)
{
    1 {CheckCert -ComputerNames "CUKIRUNCSVR0242"}
    2 {CheckCert}
    3 {CheckCert}
    ...

I want to pass ComputerNames like serv1, serv2, serv3 and this number of servers cound vary from 1 to 6 depending upon the option selected from menu. **OR Can I define a list of servers of different envirinments and pass the list name as parameter and modify my CheckCert function to loop through for each server and get the expired certificate details?

Switch ($xMenu1)
{
    1 {CheckCert -ComputerNames CIT_envList}
    2 {CheckCert -ComputerNames SIT_envList}
    3 {CheckCert -ComputerNames Prod_envList}
    ...

And their server lists are something like:

CIT_envList = serv1, serv2

SIT_envList = serv1, serv2, serv3,

PROD_envList = serv1, serv2, serv3, serv4
like image 283
nectar Avatar asked Mar 26 '14 09:03

nectar


People also ask

How do you pass an array as a parameter in a function?

To pass an entire array to a function, only the name of the array is passed as an argument. result = calculateSum(num); However, notice the use of [] in the function definition. This informs the compiler that you are passing a one-dimensional array to the function.

How do you pass parameters to a function in PowerShell?

How do I pass parameters to PowerShell scripts? Passing arguments in PowerShell is the same as in any other shell: you just type the command name, and then each argument, separated by spaces. If you need to specify the parameter name, you prefix it with a dash like -Name and then after a space (or a colon), the value.

Can you pass an array as a parameter?

Arrays can be passed as arguments to method parameters. Because arrays are reference types, the method can change the value of the elements.

How do I pass multiple parameters to a PowerShell function?

To pass multiple parameters you must use the command line syntax that includes the names of the parameters. For example, here is a sample PowerShell script that runs the Get-Service function with two parameters. The parameters are the name of the service(s) and the name of the Computer.


2 Answers

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}}
            }
        }
    }
}
like image 130
CB. Avatar answered Sep 23 '22 14:09

CB.


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

like image 32
Luiz Eduardo Garcia Avatar answered Sep 20 '22 14:09

Luiz Eduardo Garcia