2023-03-24

foreach in powershell to retrieve processes

The output of processes does not show in the results. I'm logging into each server and pulling the processes. If I do just one server without the foreach it outputs the processes, however when I add a for each with a list of servers, it doesn't show the results

$serverListFile = "servers.csv"
$global:ServerLists = Import-Csv -Path $global:ScriptPath\$ServerListFile -Delimiter "," | ForEach-Object  {
    $_.servers

    $currentServer = $_.servers
    Write-Host $currentServer

    Write-Host "Getting first 5 Processes on" $currentServer

    Invoke-Command -ComputerName $_.servers -Credential $Cred -ScriptBlock {
        Get-Process | Select-Object -First 5
    }
}

Here are the results:

Server1
Getting first 5 Processes on Server1
Server2
Getting first 5 Processes on Server2
Server3
Getting first 5 Processes on Server3


No comments:

Post a Comment