Hi,
We seem to be having an adding a custom powershell sensor to PRTG. The sensor accept literals within values on the sensor, however when I use variables within the script it returns 0 instead of a value.
Please see the script and sample output below
Script
$Query = " SET NOCOUNT ON; IF OBJECT_ID('TempDB..#tokens','U') IS NOT NULL DROP TABLE #tokens; IF OBJECT_ID('TempDB..#TokenOutput','U') IS NOT NULL DROP TABLE #TokenOutput; DECLARE @publication sysname ,@tokenID AS INT ,@StartTime DATETIME = GETDATE(); SET @publication = N'TRN-WLRCRM'; EXEC sys.sp_posttracertoken @publication = @publication, @tracer_token_id = @tokenID OUTPUT; WAITFOR DELAY '00:00:2'; CREATE TABLE #tokens (tracer_id int, publisher_commit datetime); INSERT #tokens (tracer_id, publisher_commit) EXEC sys.sp_helptracertokens @publication = @publication; SET @tokenID = (SELECT TOP 1 tracer_id FROM #tokens ORDER BY publisher_commit DESC); CREATE TABLE #TokenOutput (ID INT IDENTITY(1,1), [distributor_latency] int, [subscriber] nvarchar(128), [subscriber_db] nvarchar(128), [subscriber_latency] int, [overall_latency] int ); WHILE 1 = 1 BEGIN WAITFOR DELAY '00:00:01'; INSERT INTO #TokenOutput EXEC sys.sp_helptracertokenhistory @publication = @publication, @tracer_id = @tokenID; IF EXISTS (SELECT TOP 1 1 FROM #TokenOutput WHERE overall_latency IS NULL) BEGIN IF DATEDIFF(SECOND,@StartTime,GETDATE()) > 60 BEGIN UPDATE #TokenOutput SET overall_latency = 99 WHERE overall_latency IS NULL; END ELSE BEGIN DELETE FROM #TokenOutput WHERE overall_latency IS NULL; END END IF (SELECT COUNT(DISTINCT subscriber) FROM #TokenOutput WHERE overall_latency IS NOT NULL) = 2 BEGIN BREAK; END END SELECT DISTINCT CASE subscriber WHEN 'REP-01' THEN 1 WHEN 'Server2' THEN 2 END Id ,subscriber ,overall_latency FROM #TokenOutput; " $Output = Invoke-Sqlcmd -ServerInstance TargetServer -Database WLRCRM -Query $Query -QueryTimeout 65 $Rep01 = $Output | Where-Object {$_.subscriber -eq "Server1"}|SELECT-Object overall_latency $DBS01 = $Output | Where-Object {$_.subscriber -eq "Server2"}|SELECT-Object overall_latency $Server1.Value = $Server1.overall_latency $Server2Value = $Server2overall_latency Powershell script Write-Host "<prtg>" "<result>" "<channel>Server2</channel>" "<value>$Server2Value</value>" "</result>" "<result>" "<channel>Server1</channel>" "<value>$Server1Value</value>" "</result>" "</prtg>" Output <prtg> <result> <channel>Server1</channel> <value>9</value> </result> <result> <channel>Server2</channel> <value>7</value> </result> </prtg>
Add comment