Method invocation failed because [System.Data.DataRow] does not contain a method named 'Split'.

Hello

I try to display starttime and endtime but error occurs Method invocation failed because [System.Data.DataRow] does not contain a method named 'Split'.

$server = "test\srv"
$sqlcmd = (
"DECLARE @ts_now bigint = (SELECT cpu_ticks/(cpu_ticks/ms_ticks)FROM sys.dm_os_sys_info); 

SELECT TOP(1) 
               convert(varchar, DATEADD(ms, -1 * (@ts_now - [timestamp]), GETDATE()))  +'|'+
			   convert(varchar, GETDATE()) +'|'+
               convert(varchar, SQLProcessUtilization)
FROM ( 
	  SELECT record.value('(./Record/@id)[1]', 'int') AS record_id, 
			record.value('(./Record/SchedulerMonitorEvent/SystemHealth/SystemIdle)[1]', 'int') AS [SystemIdle], 
			record.value('(./Record/SchedulerMonitorEvent/SystemHealth/ProcessUtilization)[1]','int') AS [SQLProcessUtilization],
			[timestamp] 
	  FROM ( 
			SELECT [timestamp], CONVERT(xml, record) AS [record] 
			FROM sys.dm_os_ring_buffers 
			WHERE ring_buffer_type = N'RING_BUFFER_SCHEDULER_MONITOR' 
			AND record LIKE N'%<SystemHealth>%') AS x 
	  ) AS y 
ORDER BY record_id DESC;"
)

$datasource = Invoke-SqlCmd -Query "$sqlcmd" -ServerInstance $server -QueryTimeout 200000
if ($datasource) {
foreach ($column in $datasource) {
$array_str = $column.Split("|")
$starttime = $array_str[0].Trim()
$endtime = $array_str[1].Trim()
$Title=$starttime + " to " + $endtime
$Title 
}
}		

Open in new window


Why ?

Thanks

Regards
bibi92Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rainer JeschorCommented:
Hi,
the response is a data table which contains first the rows, then the items/columns therefore you get the error as you try to split on a datarow.

You might try the following:
$server = "test\srv"
$sqlcmd = (
"DECLARE @ts_now bigint = (SELECT cpu_ticks/(cpu_ticks/ms_ticks)FROM sys.dm_os_sys_info); 

SELECT TOP(1) 
               convert(varchar, DATEADD(ms, -1 * (@ts_now - [timestamp]), GETDATE()))  +'|'+
			   convert(varchar, GETDATE()) +'|'+
               convert(varchar, SQLProcessUtilization)
FROM ( 
	  SELECT record.value('(./Record/@id)[1]', 'int') AS record_id, 
			record.value('(./Record/SchedulerMonitorEvent/SystemHealth/SystemIdle)[1]', 'int') AS [SystemIdle], 
			record.value('(./Record/SchedulerMonitorEvent/SystemHealth/ProcessUtilization)[1]','int') AS [SQLProcessUtilization],
			[timestamp] 
	  FROM ( 
			SELECT [timestamp], CONVERT(xml, record) AS [record] 
			FROM sys.dm_os_ring_buffers 
			WHERE ring_buffer_type = N'RING_BUFFER_SCHEDULER_MONITOR' 
			AND record LIKE N'%<SystemHealth>%') AS x 
	  ) AS y 
ORDER BY record_id DESC;"
)

$datasource = Invoke-SqlCmd -Query "$sqlcmd" -ServerInstance $server -QueryTimeout 200000
if ($datasource) {
	foreach ($row in $datasource) {
		$array_str = $row.Item[0].ToString().Split("|")
		$starttime = $array_str[0].Trim()
		$endtime = $array_str[1].Trim()
		$Title=$starttime + " to " + $endtime
		$Title 
	}
}

Open in new window


Columns are objects, therefore the "ToString".

HTH
Rainer

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.