Solved

Getting computer name in the out put

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

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 39

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 16

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Hi all.   The other day I had to change the passwords for a bunch of users on the fly. Because they were so many, I decided to do it in an automated way and I would like to share it with you all.   If you are not doing it directly in a Domain Co…
In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
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…
This video discusses moving either the default database or any database to a new volume.

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now