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

I am writing a PowerShell script to get a list of certificates that expire in 30 days. The script works, but the problem is that there are too many applications and pres servers, and I want to minimize the script code. My function:

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 call this function as:

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

I want to transfer ComputerNames, such as serv1, serv2, serv3, and this number of servers can vary from 1 to 6 depending on the option selected in the menu. ** OR Can I define a list of servers with different environments, pass the list name as a parameter, and change the CheckCert function to cycle for each server and get expired certificate data?

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

And their server lists are something like this:

 CIT_envList = serv1, serv2 SIT_envList = serv1, serv2, serv3, PROD_envList = serv1, serv2, serv3, serv4 
+11
powershell
source share
1 answer

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}} } } } } 
+11
source share

All Articles