Solved

Getting computer name in the out put

Posted on 2013-02-04
5
385 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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…

828 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