Norrin Rad
Norrin Rad

Reputation: 991

Computername in output from foreach loop

I’m trying to do a simple thing of listing a computername in an output from a foreach loop, but I just can’t seem to get it working, was hoping someone could point me in the right direction..

I’ve tried the below

$computers = (Get-AdComputer -Filter "name -like ‘vm-*'").Name | Sort-Object
foreach ($computer in $Computers) 
{
Get-Service -Name RpcSs | Select Name, Status, computername
} 

Thanks

Upvotes: 0

Views: 3730

Answers (1)

guiwhatsthat
guiwhatsthat

Reputation: 2434

you need to use the $computer in the get-servie cmdlet. In your code you will always get the services of the local computer.

Try like this:

$computers = (Get-AdComputer -Filter "name -like ‘vm-*'").Name
foreach ($Computer in $Computers) 
{
    Get-Service -Name RpcSs -ComputerName $Computer| Select-Object -Property Name, Status, MachineName
} 

Upvotes: 3

Related Questions