I am trying to create a Windows PowerShell monitor to display more than one results/outputs.
I have following script:
$Error.Clear();
function Get-ODBC-Data{
param(
[string]$query=$("
SELECT
col1,
col2
FROM [master].[sys].[table_name] dm
WHERE [CONDITION];"),
[string]$username='db_User_name',
[string]$password='db_password'
)
$conn = New-Object System.Data.Odbc.OdbcConnection
$conn.ConnectionString = "DRIVER={SQL Server};Server=123.456.78.90;Initial Catalog=master;Uid=$username;Pwd=$password;"
$conn.open()
$cmd = New-object System.Data.Odbc.OdbcCommand($query,$conn)
$ds = New-Object system.Data.DataSet
(New-Object system.Data.odbc.odbcDataAdapter($cmd)).fill($ds) | out-null
$conn.close()
$ds.Tables[0]
$rowCount = 1;
foreach ($row in $ds.Tables[0].Rows)
{
Write-Host "Statistic.$rowCount" $row.col2;
Write-Host "Message.$rowCount" "Value of col2 for" $row.col1 "is" $row.col2;
$rowCount++;
}
}
$result = Get-ODBC-Data
exit 0
When I test this script by clicking on "GET SCRIPT OUTPUT" I get:
Output Result: * Get Output Failed:
Output: ==============================================
Statistic.1 0
Message.1 Value of for ABC is 0
Statistic.2 0
Message.2 Value of for DEFG_HIJKL is 0
Statistic.3 0
Message.3 Value of for MNO_Pqrstuvwxyz is 0
And I think it gives output in expected format except the ":" after Statistic.1. Another monitor I created which is of same type works fine when I use
Write-Host "Statistic.01:" $count[0];
So, if I just add ":" in the Write-Host statement like
Write-Host "Statistic.$rowCount:" $row.col2;
And then click on "GET SCRIPT OUTPUT" I get error: * Not Defined
My question is: What adjustment do I need to make (may be in Write-Host?) to get this component working?