如何运行在循环中更新的Powershell脚本?

时间:2019-05-15 16:38:00

标签: sql-server powershell

我有一个连接到数据库的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() 

0 个答案:

没有答案