This is the current code we are using to run a SQL command to return a value. I'm trying to pass the Credentials for monitoring for the login. We cant seem to figure out how to use the ${CREDENTIAL}
variable Specified from link:
Create a Windows PowerShell monitor
$avg = Get-WmiObject win32_process -Credential '${CREDENTIAL}'
$ServerName = "fnsqlapplog1.fna.local"
$DatabaseName = $args[0]
clear
$Query = "SELECT COUNT (*) as [Returned]
FROM
(SELECT
[TimeStampUTC]
,CASE WHEN ISNULL([ExtendedInternalLog],'') = '' THEN NULL ELSE CAST(JSON_VALUE([ExtendedInternalLog], '$.Properties.StatusCode') AS INT) END AS [StatusCode]
FROM [$DatabaseName].[dbo].[Log]
WHERE TimeStampUTC > DATEADD(mi,-5,GETUTCDATE())
AND UserInfo=SYSTEM_USER
) AS t
WHERE t.StatusCode BETWEEN 400 AND 499"
#Action of connecting to the Database and executing the query and returning results if there were any.
$conn=New-Object System.Data.SqlClient.SQLConnection
$ConnectionString = "Server={0};Database={1};Connect Timeout=30" -f $ServerName,$DatabaseName
$conn.ConnectionString=$ConnectionString
$conn.Open()
$cmd=New-Object system.Data.SqlClient.SqlCommand($Query,$conn)
$cmd.CommandTimeout=120
$ds=New-Object system.Data.DataSet
$da=New-Object system.Data.SqlClient.SqlDataAdapter($cmd)
[void]$da.fill($ds)
$conn.Close();
$rValue = $ds.Tables[0].Rows[0][0];
Write-Host "Statistic.4xxCount: " $rValue;