powershell returning services info

  • Hi All,

    I'm trying to run a powershell script running which returns windows services information.

    If I run the following in a powershell session it work fine:-

    Get-WmiObject -Class Win32_Service -computername SERVERNAME | select @{name="ServerName";expression={$_.__Server}}, Name, DisplayName, StartName, StartMode | Export-Csv c:\temp\monitor\test.csv -Delimiter "|" -NoTypeInformation

    But if I try and run from command line:-

    Powershell Get-WmiObject -Class Win32_Service -computername SERVERNAME | select @{name="ServerName";expression={$_.__Server}}, Name, DisplayName, StartName, StartMode | Export-Csv c:\temp\monitor\test.csv -Delimiter "|" -NoTypeInformation

    I get the following error message:-

    'select' is not recognized as an internal or external command,

    operable program or batch file.

    There seems to be a problem with the "|" (pipe) if I rearrange the statement it always can't seem to recognized the command after the '|"

    I don't want to put the powershell command in a file because eventually I would like to achieve the following:-

    declare @Sql varchar(3000)

    declare @ServerName varchar(200)

    set @ServerName = 'SERVERNAME'

    set @Sql = 'powershell Get-WmiObject -Class Win32_Service -computername ' + @ServerName + ' | select @{name="ServerName";expression={$_.__Server}}, Name, DisplayName, StartName, StartMode | Export-Csv c:\temp\monitor\Services.csv -Delimiter "|" -NoTypeInformation'

    exec master.dbo.xp_cmdshell @Sql

    -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    Error Message:-

    'select' is not recognized as an internal or external command,

    operable program or batch file.

    Any feedback would be greatly appreciated.

    Thanks

    Warwick.

  • you need to quote the commands and enclose them in a script block.

    powershell "&{ Get-WmiObject -Class Win32_Service -computername SERVERNAME | select @{name='ServerName';expression={$_.__Server}}, Name, DisplayName, StartName, StartMode | Export-Csv c:\temp\monitor\test.csv -Delimiter '|' -NoTypeInformation } "

  • Works perfect! Thanks your help SpringTownDBA.

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply