Tommy_Cooper
asked on
Get-WMIObject Output issue
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:
My issue is the output. All I can get is too much information! This is what I'm getting:
This is on two lines in a text file.
This is my code:
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:
Please can anyone point out what I'm doing wrong and how I fix it?
Many thanks
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
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
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
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
Please can anyone point out what I'm doing wrong and how I fix it?
Many thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Nope, just banged my head against the same wall a while back :)
Glad I could help.
Glad I could help.
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:
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.
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)"
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.
ASKER
Footech - That is excellent. Thank you. If I could give you some points now I would :)
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 :)
ASKER
You're a star!
Thank you.