Quantcast
Channel: PowerShell General
Viewing all articles
Browse latest Browse all 10624

How to create the proper loop

$
0
0

I have this code which now works.

 

$Results = Foreach ($Server in (get-content "C:\Temp\servers.txt")) 

    {Get-Service "Credential Manager" -ComputerName $Server | Select @{Name="Server";Expression={$Server}},Name,Status 

    }

$Results | Select Server,Name,Status

 

If ($Results -like "*Stopped*") { Start-Service -displayname "Credential Manager"}

------------------------------------------------------------------------------------------------------

 

But my IF statement is outside of the For Loop so only the first machine that is read gets the Start-Service command.  How do I add my IF statement inside of the For Loop so any/all servers with a stopped service get the Start-Service command?

 

 

 


Viewing all articles
Browse latest Browse all 10624

Latest Images

Trending Articles



Latest Images