Link to home
Start Free TrialLog in
Avatar of ashwin_kumar525
ashwin_kumar525Flag for India

asked on

Getting computer name in the out put

I run the command:
PS C:\temp> $computerlist = Get-Content .\complist.txt
PS C:\temp> Get-WmiObject win32_service -comp $computerlist | Where-Object { $_.name -eq "bits"} | ft

I get this:
           ExitCode Name                          ProcessId StartMode           State               Status
           -------- ----                          --------- ---------           -----               ------
               1077 BITS                                  0 Manual              Stopped             OK
               1077 BITS                                  0 Manual              Stopped             OK
               1077 BITS                                  0 Manual              Stopped             OK

Well, how do I know which one's for which server in the server list I provided in the variable? is there a way tp get the computer name against each of those entries no matter what I chose to, list or table?

What I am expecting:
Name     ExitCode Name       ProcessId      StartMode           State          Status
--------       --------      ----             ---------            ---------                -----               ------
 Server1   1077    BITS           0               Manual              Stopped             OK
 Server2   1077    BITS           0               Manual              Stopped             OK
 Server3   1077    BITS           0               Manual              Stopped             OK

I hope this makes sense.
Avatar of unsatiated
unsatiated

Something along the lines of this:  I normally use xml as my source to iterate over, but you can do something like the below.  Pipe the computer list into variable.  perform a for-each on the items.

$computerlist = Get-Content .\complist.txt
For each ($computer in $computerlist)
     {
     Write-Host $computer
     Write-Host
     Get-WmiObject win32_service -comp $computer | Where-Object { $_.name -eq "bits"} | ft
     }
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ashwin_kumar525

ASKER

Thanks for the response folks, each of those looks promising. let me try that tomorrow and let you know how it goes.
Add the computer name to the object you output.
$computerlist = Get-Content .\complist.txt
foreach ($computer in $computerlist)
{
   Get-WmiObject win32_service -comp $computerlist | Where-Object { $_.name -eq "bits"} | Select @{Name="Computer";Expression={$computer}}, ProcessId, Startmode, State, Status #any other properties you want to return
}

Open in new window