powershell to insert data into mysql

hey all experts

I'm working on a script that will collected metrics data (cpu load, memory etc, ...) then insert numbers into mysql database server. Then, third-party software will visualize that data.

I used .NET mysql connector provided by  mysql.net. The script is pasted below. I've tried many ways, from simple to complex method.

The script below will give up an error that said "Unexpected token '23' in expression or statement" . The query itself will work in mysql console.
Any insight will be so great !!  
[void][system.reflection.Assembly]::LoadFrom(“C:\Program Files\MySQL\MySQL Connector Net 6.2.3\Assemblies\MySQL.Data.dll”)
$dbconnect = New-Object MySql.Data.MySqlClient.MySqlConnection
$dbconnect.ConnectionString = “server=192.168.0.25;user id=root;password=mypass$;database=perform;pooling=false”
$dbconnect.Open()
$sql = New-Object MySql.Data.MySqlClient.MySqlCommand

$time=get-date -format u
$cpu = (gwmi win32_processor | select-object loadpercentage).loadpercentage
$availMem =(gwmi win32_perfFormattedData_perfos_memory | select-object availableMbytes).availableMBytes / 1024

$sql.Connection = $dbconnect
$sql.CommandText='INSERT INTO `serverA` (cpu_percentage,available_mbytes) VALUES ('23', '12')'
$dr = $sql.ExecuteNonQuery()
$sql.Dispose()
if ($dr)
{
	return $dr
	}
	else
	{
	return $false
	}
$dbconnect.Close()
$time

Open in new window

valleytechAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

Chris StanyonWebDevCommented:
I think your SQL line is breaking when it gets to the single quote.

Try changing line 12 to the following - i.e wrap the whole line in double quotes)


$sql.CommandText="INSERT INTO `serverA` (cpu_percentage,available_mbytes) VALUES ('23', '12')"

Open in new window

0

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
valleytechAuthor Commented:
Hi C
yeah, i tried many ways, including that, and i got error
"Exception calling executeNoQuery with 0 arguments..." you syntax error ...."
thanks!
0
valleytechAuthor Commented:
actually
it was my bad ! I created table name as "server-a". The minus sign really changed things alot.
The working command is as follow: it successfully inserts correct date-time , cpu load and memory usage into database ;)

$time=get-date -format u
$time=$time.TrimEnd("Z")
$cpu = (gwmi win32_processor | select-object loadpercentage).loadpercentage
$availMem =(gwmi win32_perfFormattedData_perfos_memory | select-object availableMbytes).availableMBytes / 1024

$sql.Connection = $dbconnect
$sql.CommandText="INSERT INTO clusterA (datetime, cpu_percentage,available_mbytes) VALUES ('$time', $cpu, $availMem)"

Open in new window

0
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.