October 12, 2011 at 6:00 pm
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.
October 12, 2011 at 8:15 pm
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 } "
October 12, 2011 at 8:51 pm
Works perfect! Thanks your help SpringTownDBA.
Viewing 3 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy