Solved

Getting computer name in the out put

Posted on 2013-02-04
5
386 Views
Last Modified: 2013-02-06
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.
0
Comment
Question by:ashwin_kumar525
5 Comments
 
LVL 3

Expert Comment

by:unsatiated
ID: 38851995
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
     }
0
 
LVL 84

Accepted Solution

by:
oBdA earned 300 total points
ID: 38852010
For example like this (and replace the properties for "select" with the properties you want in the required order):
Get-WmiObject win32_service -ComputerName $ComputerList -Filter "Name='Bits'" | Select Systemname,Status,State,StartMode | ft

Open in new window

0
 
LVL 40

Assisted Solution

by:footech
footech earned 200 total points
ID: 38852105
One other method I'll mention.  If you were using PSRemoting, each returned object would include the name of the computer it was returned from.
PS C:\test\PS> invoke-command -computername $computerlist {Get-WmiObject win32_service -Filter "Name='Bits'"} | ft

         ExitCode Name                    ProcessId StartMode        State            Status           PSComputerName
         -------- ----                    --------- ---------        -----            ------           --------------
                0 BITS                          912 Manual           Running          OK               wkstn1

Open in new window

0
 
LVL 10

Author Comment

by:ashwin_kumar525
ID: 38852298
Thanks for the response folks, each of those looks promising. let me try that tomorrow and let you know how it goes.
0
 
LVL 17

Expert Comment

by:Learnctx
ID: 38852813
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

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question