简体   繁体   中英

How I can pass multiple array in value in PowerShell function

Below function I want to pass multiple value in array. When I'm passing more than one value I am getting an error.

function CheckProcess([String[]]$sEnterComputerNameHere, [String[]]$sEnterProccessNameHere) {
    #Write-Host " $sEnterComputerNameHere hello"

    @($sEnterComputerNameHere) | ForEach-Object {
        # Calling Aarray
        @($sEnterProccessNameHere) | ForEach-Object {
            if (Get-Process -ComputerName $sEnterComputerNameHere | where {$_.ProcessName -eq $sEnterProccessNameHere}) {
                Write-Output "$_ is running"
            } else {
                Write-Output "$_ is not running"
            }
        }
    }
}

$script:sEnterProccessNameHere = @("VPNUI")     # Pass the process agreement here
$script:sEnterComputerNameHere = @("hostname")  # Pass the process agreement here

CheckProcess $sEnterComputerNameHere $sEnterProccessNameHere

Give it a try with this one:

Function CheckProcess([String[]]$sEnterComputerNameHere,[String[]]$sEnterProccessNameHere) 
{ #Write-host " $sEnterComputerNameHere"

    @($sEnterComputerNameHere) | Foreach-Object {
        $computer = $_
        Write-Host $computer        
        @($sEnterProccessNameHere) | Foreach-Object {
            $process = $_
            Write-Host $process
            try{
                $x = get-process -computername $computer #Save all processes in a variable
                If ($x.ProcessName -contains $process) #use contains instead of equals
                {
                    Write-Output "$process is running"
                }
                else
                {
                    Write-Output "$process is not running"
                }
            }
            catch
            {
                Write-Host "Computer $computer not found" -ForegroundColor Yellow
            }
        }
    }
}

$script:sEnterProccessNameHere = @("VPNUI","Notepad++","SMSS") 
$script:sEnterComputerNameHere = @("remotecomputer1","remotecomputer2") 

CheckProcess -sEnterComputerNameHere $sEnterComputerNameHere -sEnterProccessNameHere $sEnterProccessNameHere

In general, it would be great if you write the error you get in your question. That helps others to help you.

If I work with arrays and | Foreach | Foreach , I always write the $_ in a new variable. That helps if I have another | Foreach | Foreach (like you had) to know for sure, with which object I'm working with..

EDIT: I changed the script, so it uses "-contains" instead of "-eq" and I added a try/catch block, so if the other computer is not found, it gives you a message.. It works on my network
EDIT2: Do you have access to the other computers? If you run get-process -computername "name of remote computer" do you get the processes?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM