Announcement

Collapse
No announcement yet.

PowerShell: Select Computername ?

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • PowerShell: Select Computername ?

    Is there anyway to display the computer name in the output of a PowerShell command using select ?

    When I run the command below, the column for computername is blank.

    PS > get-wmiobject win32_service -computername SERVERNAME | select computername,name,startname

    See the attached screen shot for the output (since pasting as text does not format the spaces properly).


    If I was just running this on one computer, it wouldn't be a problem. But I want to run this against a list of multiple computer names, using get-content to read a text file with a list of server names, and having the computer name displayed in-line with the rest of the output would be useful.

    Thanks.





    PS - PowerShell version is 2

    PS > $psversiontable

    Name Value
    ---- -----
    CLRVersion 2.0.50727.4918
    BuildVersion 6.1.7100.0
    PSVersion 2.0
    WSManStackVersion 2.0
    PSCompatibleVersions {1.0, 2.0}
    SerializationVersion 1.1.0.1
    PSRemotingProtocolVersion 2.0
    Attached Files
    Last edited by Robert R.; 15th August 2009, 00:17.

  • #2
    Re: PowerShell: Select Computername ?

    Check out the format-list and format-table cmdlets -- I think they will do what you are looking for
    http://www.computerperformance.co.uk...rmat_table.htm
    http://www.activexperts.com/activmon...rshell10/0053/
    Tom Jones
    MCT, MCSE (2000:Security & 2003), MCSA:Security & Messaging, MCDBA, MCDST, MCITP(EA, EMA, SA, EDA, ES, CS), MCTS, MCP, Sec+
    PhD, MSc, FIAP, MIITT
    IT Trainer / Consultant
    Ossian Ltd
    Scotland

    ** Remember to give credit where credit is due and leave reputation points where appropriate **

    Comment


    • #3
      Re: PowerShell: Select Computername ?

      With 'SELECT' you can make a choice of properties only of the Win32_Service class. 'Computername' is not defined by the Win32_Service class.

      To insert an extra column for the Computername you first have to define the string variable for the computername.

      (expand the name of this computer),
      Code:
      $computer = gc env:computername
      Then use format-table (or, ft) to format the table, to be able to create an extra column in the table
      (extra column==> @ label (column title) and ;expression (dynamic or static value) )

      Code:
      get-wmiobject win32_service -computername $computer | select name, startname | ft -property @{Label="Computername"; Expression={$computer}}, name, startname

      But since you are not creating a script object for win32_service, there is not realy a need to use 'SELECT' any more. Here is the new sample:
      Code:
      PS > $computer = gc env:computername
      PS > get-wmiobject win32_service -computername $computer | ft -property @{Label="Computername"; Expression={$computer}}, name, startname
      If you want to create the object for the collection of services - then,
      Code:
      PS> $computer = gc env:computername
      PS> $Services = get-wmiobject win32_service -computername $computer | select name, startname
      PS> $Services | format-table @{Label="Computername"; Expression={$computer}}, name, startname
      \Rems
      Last edited by Rems; 15th August 2009, 10:24.

      This posting is provided "AS IS" with no warranties, and confers no rights.

      __________________

      ** Remember to give credit where credit's due **
      and leave Reputation Points for meaningful posts

      Comment


      • #4
        Re: PowerShell: Select Computername ?

        The code works for single machines, either locally or remote.

        Eg: If I substitute

        $computer = gc env:computername

        with

        $computer = get-content "ASOK"


        I get the properly formatted output I want for the remote server ASOK.

        Computername name startname
        ------------ ---- ---------
        ASOK AeLookupSvc LocalSystem
        ASOK Alerter NT AUTHORITY\LocalService
        ASOK ALG NT AUTHORITY\LocalService
        ASOK AppMgmt LocalSystem
        ASOK AudioSrv LocalSystem
        ASOK BITS LocalSystem
        ASOK Browser LocalSystem



        Thank you, because I wouldn't have even gotten that far without your help, being as new at this as I am

        At this point, I thought it would be a simple matter of using get-content to read from a list of server names. For testing purposes, I have a file called lists.txt which contains the following four items

        ASOK
        EX2K3
        MI6
        MORDAC



        However, it turns out not to be the case.


        $computer = get-content list.txt
        $Services = get-wmiobject win32_service -computername $computer | select name, startname


        , or anything else with -computername $computer results in a

        Get-WmiObject : Cannot validate argument on parameter 'ComputerName'.
        The argument is null or empty. Supply an argument that is not null or empty and then try the command again.


        error message. I've tried several variations, including without the $computer = statement, and including foreach-object .

        A proper syntax I've discovered for reading through a file is:

        get-content list.txt |
        foreach-object { get-wmiobject win32_service -computername $_ | select name, startname }


        but that puts me back to my original position of not having the computername in the output.

        After spending a day fiddling around with this some more, the closest I've come is

        $computer = get-content list.txt
        $Services = ( $computer | get-wmiobject win32_service -computername $_ | select name, startname )
        $Services | format-table @{Label="Computername"; Expression={$computer}}, name, startname


        which does read through list.txt properly -- services unique to EX2K3 and MI6 are listed in the output -- and outputs a column with the computername.

        The only problem is that it puts all four computernames on one line:

        Computername name startname
        ------------ ---- ---------
        {ASOK, EX2K3, MI6, MORD... AeLookupSvc LocalSystem
        {ASOK, EX2K3, MI6, MORD... Alerter NT AUTHORITY\LocalService
        {ASOK, EX2K3, MI6, MORD... ALG NT AUTHORITY\LocalService
        {ASOK, EX2K3, MI6, MORD... AppMgmt LocalSystem
        {ASOK, EX2K3, MI6, MORD... AudioSrv LocalSystem
        {ASOK, EX2K3, MI6, MORD... BITS LocalSystem
        Last edited by Robert R.; 20th August 2009, 18:08.

        Comment


        • #5
          Re: PowerShell: Select Computername ?

          Try this,

          Create an array containing the names of the 4 computers
          Code:
          PS> $a = "ASOK", "EX2K3", "MI6", "MORDAC"; $l = "=" * 26 
          PS> "`n"; foreach ($computer in $a) {$l + "`n" + $computer + "`n" + $l; get-wmiobject win32_service -computername $computer | ft -property @{Label="Computername"; Expression={$computer}}, name, startname}; "`a"
          Or,
          Read text file, add lines into an array
          Code:
          PS> $a = get-content "c:\list.txt"; $l = "=" * 26
          PS> "`n"; foreach ($computer in $a) {$computer = $computer.Trim(); $l + "`n" + $computer + "`n" + $l; get-wmiobject win32_service -computername $computer | ft -property @{Label="Computername"; Expression={$computer}}, name, startname}; "`a"

          Where $l = "=" * 26 repeats the "=" sign 26 times, you'll see this as a short double line in the output.


          \Rems

          This posting is provided "AS IS" with no warranties, and confers no rights.

          __________________

          ** Remember to give credit where credit's due **
          and leave Reputation Points for meaningful posts

          Comment


          • #6
            Re: PowerShell: Select Computername ?

            Thank you.

            That did exactly what I wanted.

            I was confused when I started this, I thought $computer was an array by default, because I can enter multiple values into it. eg,

            PS C:> $computername = "asok", "ex2k3", "mi6", "mordac"
            PS C:> write-host $computername
            asok ex2k3 mi6 mordac


            At that point, I thought it would be a simple matter to get-content from a text file, and apply a loop to it which would read each value for $computer in succession.

            Obviously, I was wrong.

            Comment

            Working...
            X