我有一个连接到数据库的powershell脚本,将结果存储在一个对象中。我受困的地方是遍历对象并执行脚本。每个脚本运行之后,它应该使用状态更新数据库。
$Server = 'Server'
$database = 'database'
$u = 'un'
$p = 'pw'
$Connection = New-Object System.Data.SQLCient.SQLConnection
$Connection.ConnectionString = "Server=$('$Server');trusted_connection=true;Database=$('$Database');User Id=$('$u');Password=$('$p')"
$Connection.Open()
$Command = New-Object System.Data.SQLClient.SQLCommand
$Command.Connection = $Connection
$Command.CommandText ="SELECT * FROM dbo.jobs WHERE Run = 0"
$jobs = $Command.ExecuteReader()
While ($jobs.Read()){
switch($jobs.Script){
15
{
c:\xxx\xxx\myscript.bat $jobs.Date
$Command.CommandText= "UPDATE dbo.jobs SET Run = 1 WHERE ID = $jobs.ID; "
$Command.ExecuteNonQuery()
}
}
}
$Connection.Close()