Monitor CPU utilization from T-SQL & Powershell

This piece of code may help when you are not able to remote login to the server.

Script:

SET NOCOUNT ON

DECLARE @CPU TABLE (Output varchar(max))

DECLARE @Text varchar(20);

DECLARE @Utilization varchar(10);

insert into @CPU exec master.dbo.xp_cmdshell ‘Powershell -Command Get-WmiObject win32_processor’

delete from @CPU where output not like ‘Load%’

select @Text= ‘CPU Utilization’ ,

@Utilization=substring(output,charindex(‘ :’,output)+2, len(output)) + ‘ %’ from @CPU where Output IS NOT NULL

Print @Text + ‘ : ‘ +@Utilization

SET NOCOUNT OFF

go 10
CPU Utilization

Leave a Reply

Your email address will not be published. Required fields are marked *