0

I want to know how i will use the tasklist cmd that will output the imagename plus the hostname

i have thus syntax but could not combine the result into one.

EXEC xp_cmdshell 'tasklist /fi "imagename eq notepad.exe"';  

plus hostname

Nick.Mc
  • 18,304
  • 6
  • 61
  • 91
Mel Cuya
  • 25
  • 8
  • What is `plus hostname`? is it trying to be part of the syntax that you are running or an explanation of what you want? The hostname will always be the SQL Server so that is `SELECT @@SERVERNAME`. If you want the hostname to appear as another column, tell us exactly which version of SQL you are running as that has a bearing on the approach – Nick.Mc Oct 22 '14 at 04:28
  • Hello Nick, yes plus is my explanation on what to expect in the result, my version is mssql 2012 and 2008 r2, i need to insert the result in a table. i have two columns, output and servername. thnaks for your reply – Mel Cuya Oct 23 '14 at 01:06
  • You first need to _connect_ to a single SQL Server to run this. What mechanism are you using to run this? You'll need to run it against many servers. – Nick.Mc Oct 23 '14 at 03:57
  • by the way i'm using orchestrator. so i'm having a hard time incorporate the 2 sql server and microsoft orchestrator – Mel Cuya Oct 23 '14 at 05:21
  • I've never used Orchestrator but I guess its some kind of job management system. Why don't you post a specific issue and we can try and address it – Nick.Mc Oct 23 '14 at 05:47

1 Answers1

0

This will give you what you want:

DECLARE @T TABLE (capture VARCHAR(5000))

INSERT @T
EXEC xp_cmdshell 'tasklist /fi "imagename eq notepad.exe"'; 

INSERT INTO dbo.[N4FO_OpenGUIOutput] (Output,system) 
SELECT capture, @@SERVERNAME as host FROM @T WHERE capture IS NOT NULL

But you have to actually connect to the SQL Server to run it so you'll already need to know the host name to connect to.

Nick.Mc
  • 18,304
  • 6
  • 61
  • 91
  • hi nick sorry i'm away for a couple of days, here is my script and having some errors insert into ControlDB.[dbo].[N4FO_OpenGUIOutput] (Output,system) EXEC xp_cmdshell 'tasklist /fi "imagename eq notepad.exe"'; SELECT @@SERVERNAME and here is my error Msg 213, Level 16, State 7, Procedure xp_cmdshell, Line 1 Column name or number of supplied values does not match table definition. – Mel Cuya Oct 29 '14 at 04:13
  • Yes. That isn't the script I posted and it is not valid. I will update my script with this new information about table `[N4FO_OpenGUIOutput]` that you've just mentioned – Nick.Mc Oct 29 '14 at 05:16
  • thanks nick, will wait for your update. thanks a lot – Mel Cuya Oct 30 '14 at 05:43
  • If this question was of assistance please mark as correct. – Nick.Mc Nov 02 '14 at 10:48
  • hi nick yes the answer is correct. how do i mark it as correct? – Mel Cuya Nov 04 '14 at 05:18