2

I have the following code which prints out some properties of each SQL Server related service on a host:

[reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.SqlWmiManagement")|Out-Null;
$mc = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer localhost;
$mc.Services | % {$_|select-object Name,DisplayName,ServiceState}

I want to output the result with the HostName appended. I've tried this:

$hostName = hostname
$mc.Services | % {$_|select-object Name,DisplayName,ServiceState,@{Name="HostName";Expression={$hostname}}

But I just get >> displayed. Any ideas on how to accomplish what I want?

1 Answer 1

4

I believe you are missing a }:

$hostName = hostname
$mc.Services | % {$_|select-object Name,DisplayName,ServiceState,@{Name="HostName";Expression={$hostname}}}

And you can do it without the foreach-object:

$mc.Services | select-object Name,DisplayName,ServiceState,@{Name="HostName";Expression={$hostname}}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the additional comment on select-object

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.