[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Get-WMIObject Output issue

Posted on 2014-02-07
6
Medium Priority
?
358 Views
Last Modified: 2014-02-08
Hello All,

Banging my head against the wall with this one...

As part of a script I want to output to a text file the name of a server, the name of a service and the name of the user account that is used to run the service.  Input is obviously a csv or list of servers. just like this:
MyServerName,smstsmgr,LocalSystem

Open in new window


My issue is the output.  All I can get is too much information!  This is what I'm getting:

MyServerName,@{Name=smstsmgr; StartsAs=LocalSystem; DisplayName=ConfigMgr Task Sequence Agent}.name,@{Name=smstsmgr; StartsAs=LocalSystem; DisplayName=ConfigMgr Task Sequence Agent}.Displayname,.Startname
MyServerName,\\MyServerName\root\cimv2:Win32_Service.Name="smstsmgr".name,\\MyServerName\root\cimv2:Win32_Service.Name="smstsmgr".Displayname,\\MyServerName\root\cimv2:Win32_Service.Name="smstsmgr".Startname

Open in new window



This is on two lines in a text file.
This is my code:

function Get-AdminService

{
    [CmdletBinding()]

    Param()

    $Servers = Get-Content -Path $InputDir\RealServers.txt

    foreach ($Server in $Servers)
    {
        $SVC = Get-WMIObject -Class Win32_Service -Computername $Server -Filter "Name = 'smstsmgr'"

        foreach ($Service in $SVC)
        {
            Write-Verbose $Service.name 
            Add-Content -Path $OutputDir\ChgService.txt "$Server,$Service.name,$Service.Displayname,$Service.Startname"
        }
    }
}
Get-AdminService -Verbose

Open in new window


You can see where I'm writing verbose using just a single property and that works fine on the console. If I add multiple properties there it also doesn't work. If I wrap that part with "quotes" and just show a single property, I get a similar extended output to the file I'm generating:
VERBOSE: \\MyServerName\root\cimv2:Win32_Service.Name="smstsmgr".name

Open in new window


Please can anyone point out what I'm doing wrong and how I fix it?

Many thanks
0
Comment
Question by:Tommy_Cooper
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 35

Accepted Solution

by:
Dan Craciun earned 2000 total points
ID: 39842859
I think you should try it like this:
Add-Content -Path $OutputDir\ChgService.txt ($Server + ", " + $Service.name + ", " + $Service.Displayname + ", " + $Service.Startname)

Open in new window


HTH,
Dan
0
 
LVL 3

Author Comment

by:Tommy_Cooper
ID: 39842916
Dan,

You're a star!

Thank you.
0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 39842920
Nope, just banged my head against the same wall a while back :)

Glad I could help.
0
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 41

Expert Comment

by:footech
ID: 39843348
Solution has already been accepted, but here's an explanation of this behavior...
Inside a string, if you're displaying properties of an object you need to use subexpression notation "$($x.property)".  So you could do the following:
           Add-Content -Path $OutputDir\ChgService.txt "$Server,$($Service.name),$($Service.Displayname),$($Service.Startname)"

Open in new window

I have found a few times that this works better than concatenation (which is what Dan's example uses).
In a case where your objective is to output a .CSV file however, in most cases I would use the Select-Object command to choose what properties I want and pipe that to Export-CSV.
0
 
LVL 3

Author Comment

by:Tommy_Cooper
ID: 39843818
Footech - That is excellent. Thank you. If I could give you some points now I would :)
0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 39843823
I'm not versed enough on PS, so I find the $($.) construct harder to read. That's why I prefer concatenation. Easier on my brain :)
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

This article will help you understand what HashTables are and how to use them in PowerShell.
A walk-through example of how to obtain and apply new DID phone numbers to your cloud PBX enabled users that are configured in Office 365. Whether you have 1, 10 or 100+ users in your tenant, it's quite easy to get them phone-enabled and making/rece…
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…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

649 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