0

I have a list of servers. I want to check what all automatic services are not running. I want to store it in array and later display in grid view format. I tried below, but it didn't work.

foreach ($i in $list) {
    $Res = @(Get-WmiObject Win32_Service -ComputerName $i | Where-Object {
               $_.StartMode -eq 'Auto' -and $_.State -ne 'Running'
           } | Select PSComputerName, Name, DisplayName, State, StartMode)
}

$Res | Out-GridView

1 Answer 1

1

Assigning values to $Res inside the loop overwrites the value with every loop cycle. for a construct like that you'd need to define the variable as an array outside the loop and then append to it:

$Res = @()
foreach ($i in $list) {
    $Res += Get-WmiObject ...
}

However, this is bound to perform poorly. It's usually better to just assign the output generated in the loop to the variable:

$Res = foreach ($i in $list) {
    Get-WmiObject ...
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.