为什么powershell不更新数据库?

时间:2013-05-30 19:53:54

标签: mysql windows powershell

我有以下Powershell脚本:

$a = Get-Content C:\users\diana\desktop\names.txt

snmpwalk -v 2c -c root $a .1.3.6.1.2.1.25.3.3.1.2 > c:\users\diana\desktop\cpu.txt
snmpwalk -v 2c -c root $a .1.3.6.1.2.1.25.5.1.1.2 > c:\users\diana\desktop\ramvid.txt
snmpwalk -v 2c -c root $a .1.3.6.1.2.1.25.2.2 > c:\users\diana\desktop\ram.txt

get-content C:\users\diana\desktop\ramvid.txt | %{ [int]$used+=$_.split(' ')[3]; } ;     echo $used > C:\users\diana\desktop\naujas.txt
get-content C:\users\diana\desktop\ram.txt | %{ [int]$total=$_.split(' ')[3]; } ; echo     $total > C:\users\diana\desktop\ramfiltruotas.txt

[decimal]$b=($used*100)/$total
[math]::floor($b) > C:\users\diana\desktop\naujas2.txt


get-content C:\users\diana\desktop\cpu.txt | %{ [int]$array=$_.split(' ')[3]; }
$c=($array | Measure-Object -Average).average
echo $c > C:\users\diana\desktop\naujas3.txt

[void][system.reflection.Assembly]::LoadWithPartialName(“MySQL.Data”)

$myconnection = New-Object MySql.Data.MySqlClient.MySqlConnection
$myconnection.ConnectionString = "database=db;server=localhost;Persist Security Info=false;user id=root;pwd= "
$myconnection.Open()
$command = $myconnection.CreateCommand()
$command.CommandText = "UPDATE db.server SET (cpu='$c',ram='$b') WHERE server_name like '192.168.95.139'";
$myconnection.Close()

代码的上半部分效果很好,但是当谈到MySQL时没有任何反应。没有一个错误,没有,表不更新。 有人能指出我这里的问题在哪里吗?

1 个答案:

答案 0 :(得分:0)

看起来你没有执行命令,可能是:

$command.ExecuteNonQuery()

我认为你也应该处理:

$command.Dispose()