SQLConnector.ps1 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. #
  2. # SQLConnector.ps1
  3. #
  4. # Includes
  5. $PSScriptRoot = Split-Path -Parent -Path $MyInvocation.MyCommand.Definition
  6. . "$PSScriptRoot\Utility.ps1"
  7. Function Get-GlobalConfig{
  8. $sql = "SELECT * FROM [dbo].[GlobalConfig]"
  9. $returnedConfig = Invoke-SQL -connectionString $Config.SQLConnectionString -sqlCommand $sql
  10. return $returnedConfig;
  11. }
  12. Function Get-ServerConfig{
  13. $ret = Invoke-SQLProc -procName "GetServerConfig" -parameters @{"serverName" = $Config.ServerName}
  14. $row = ConvertTableTo-Object -table $ret
  15. $Config | Add-Member -MemberType NoteProperty –Name EndTime –Value $row.EndTime
  16. $Config | Add-Member -MemberType NoteProperty –Name ServerId –Value $row.Id
  17. $Config | Add-Member -MemberType NoteProperty –Name BatchSize –Value $row.BatchSize
  18. $Config | Add-Member -MemberType NoteProperty –Name NumberInstances –Value $row.NumberInstances
  19. $Config | Add-Member -MemberType NoteProperty –Name ServerActive –Value $row.IsActive
  20. $Config | Add-Member -MemberType NoteProperty –Name ServerComplete –Value $row.ServerComplete
  21. If(IsNull($Config.BatchSize) -or $Config.BatchSize -eq 0){
  22. $Config.BatchSize = 1
  23. }
  24. }
  25. Function Get-ActiveServers{
  26. $ret = Invoke-SQLProc -procName "GetActiveServers"
  27. return $ret
  28. }
  29. Function Get-ActiveInstances{
  30. param(
  31. [int] $serverId
  32. )
  33. $ret = Invoke-SQLProc -procName "GetActiveInstances" -parameters @{"serverId" = $serverId}
  34. return $ret
  35. }
  36. Function End-Instance{
  37. param(
  38. [int] $instanceId
  39. )
  40. $ret = Invoke-SQLProc -procName "EndInstance" -parameters @{"instanceId" = $instanceId}
  41. return $ret
  42. }
  43. Function Set-ScriptStart{
  44. $ret = Invoke-SQLProc -procName "LogScriptStart" -parameters @{"serverName" = $Config.ServerName}
  45. # Add to config object
  46. $Config | Add-Member -MemberType NoteProperty –Name InstanceId –Value $ret.Rows[0].ItemArray[0]
  47. }
  48. Function Set-ScriptEnd{
  49. param(
  50. [int] $instanceId,
  51. [int] $numProcessed,
  52. [int] $numErrors,
  53. [string] $exception = $null
  54. )
  55. $ret = Invoke-SQLProc -procName "LogScriptEnd" -parameters @{"instanceId" = $instanceId; "numberProcessed" = $numProcessed; "numberErrors" = $numErrors; "exception" = $exception}
  56. }
  57. Function Log-ServerComplete{
  58. param(
  59. [string] $serverName
  60. )
  61. $ret = Invoke-SQLProc -procName "LogServerComplete" -parameters @{"serverName" = $serverName}
  62. }
  63. Function Get-FileRow{
  64. param(
  65. [string] $mode
  66. )
  67. $table = Invoke-SQLProc -procName "SelectNextFileToProcess" -parameters @{"serverId" = $Config.ServerId; "scriptInstanceId" = $Config.InstanceId; "maxRetries" = $Config.MaxRetries; "mode" = $mode}
  68. $row = $null
  69. If($table -ne $null -and $table.Rows -ne $null -and $table.Rows.Count -gt 0){
  70. $row = ConvertTableTo-Object -table $table
  71. }
  72. return $row
  73. }
  74. Function Get-FileBatch{
  75. param(
  76. [string] $mode
  77. )
  78. $m = "Getting batch of: " + $Config.BatchSize.ToString() + " files"
  79. Write-Log -message $m -severity 4
  80. $table = Invoke-SQLProc -procName "SelectNextBatchToProcess" -parameters @{"serverId" = $Config.ServerId; "scriptInstanceId" = $Config.InstanceId; "maxRetries" = $Config.MaxRetries; "mode" = $mode; "batchSize" = $Config.BatchSize}
  81. $list = @()
  82. If($table -ne $null -and $table.Rows -ne $null -and $table.Rows.Count -gt 0){
  83. foreach($row in $table.Rows){
  84. $list += ConvertRowTo-Object -columns $table.Columns -row $row
  85. }
  86. }
  87. Write-Log -message "Got batch" -severity 4
  88. return $list
  89. }
  90. Function Update-FileRow{
  91. param(
  92. [int] $rowId,
  93. [int] $attemptCount,
  94. [string] $exception = $null,
  95. [int] $status,
  96. [string] $newfilename,
  97. [long] $newfilesize,
  98. [long] $originalfilesize,
  99. [string] $lastModifiedWhen,
  100. [string] $owner
  101. )
  102. $updated = Invoke-SQLProc -procName "UpdateFileRow" -parameters @{"rowId" = $rowId; "status" = $status; "attemptCount" = $attemptCount; "exception" = $exception; "newfilename" = $newfilename; "newfilesize" = $newfilesize; "originalfilesize" = $originalfilesize; "lastModifiedWhen" = $lastModifiedWhen; "owner" = $owner}
  103. Write-Log -severity 4 -message "Updated fileRow $rowId."
  104. }
  105. Function Update-FileRows{
  106. param(
  107. $batch
  108. )
  109. $sqlString = ""
  110. $params = @()
  111. $p = 0
  112. foreach($item in $batch){
  113. $exceptionParam = "@exception" + $p.ToString()
  114. $exceptionParamValue = $item.Exception
  115. $e = @{"name" = $exceptionParam; "value" = $exceptionParamValue}
  116. $params += $e
  117. $fileNameParam = "@fileName" + $p.ToString()
  118. $fileNameParamValue = $item.NewFileName
  119. $f = @{"name" = $fileNameParam; "value" = $fileNameParamValue}
  120. $params += $f
  121. $ownerParam = "@owner" + $p.ToString()
  122. $ownerParamValue = $item.Owner
  123. $o = @{"name" = $ownerParam; "value" = $ownerParamValue}
  124. $params += $o
  125. $origSizeParam = "@origSize" + $p.ToString()
  126. $origSizeParamValue = $item.OriginalFileSize
  127. $os = @{"name" = $origSizeParam; "value" = $origSizeParamValue}
  128. $params += $os
  129. $newSizeParam = "@newSize" + $p.ToString()
  130. $newSizeParamValue = $item.NewFileSize
  131. $ns = @{"name" = $newSizeParam; "value" = $newSizeParamValue}
  132. $params += $ns
  133. $modDateParam = "@lastModifiedWhen" + $p.ToString()
  134. $modDateParamValue = $item.LastModifiedWhen
  135. $mod = @{"name" = $modDateParam; "value" = $modDateParamValue}
  136. $params += $mod
  137. $attemptParam = "@attemptCount" + $p.ToString()
  138. $attemptParamValue = $item.AttemptCount
  139. $at = @{"name" = $attemptParam; "value" = $attemptParamValue}
  140. $params += $at
  141. $statusParam = "@status" + $p.ToString()
  142. $statusParamValue = $item.Status
  143. $st = @{"name" = $statusParam; "value" = $statusParamValue}
  144. $params += $st
  145. $sqlString += " ;UPDATE [Files] SET [CompletedWhen] = GETUTCDATE(), [Status] = $statusParam, [AttemptCount] = $attemptParam, [Exception] = $exceptionParam, [NewFileName] = $fileNameParam, [NewFileSize] = $newSizeParam, [OriginalFileSize] = $origSizeParam, [LastModifiedWhen] = $modDateParam, [Owner] = $ownerParam WHERE [Id] = " + $item.Id
  146. $p++
  147. }
  148. $updated = Invoke-SQLWithParams -sqlCommand $sqlString -params $params
  149. Write-Log -severity 4 -message "Updated Rows"
  150. }
  151. # ----------------------------------------------------------------------------------------------
  152. # SQL Utilities
  153. # ----------------------------------------------------------------------------------------------
  154. Function Open-SQLConnection{
  155. $connection = new-object system.data.SqlClient.SQLConnection($Config.SQLConnectionString)
  156. $connection.Open()
  157. Write-Log -severity 4 -message "Successfully connected to SQL Server"
  158. return $connection
  159. }
  160. Function Close-SQLConnection{
  161. $Conn.Close()
  162. $Conn.Dispose()
  163. Write-Log -severity 4 -message "SQL Connection Closed"
  164. }
  165. function Invoke-SQL {
  166. param(
  167. [string] $sqlCommand = $(throw "Please specify a query.")
  168. )
  169. $command = new-object system.data.sqlclient.sqlcommand($sqlCommand,$Conn)
  170. $adapter = New-Object System.Data.sqlclient.sqlDataAdapter $command
  171. $dataset = New-Object System.Data.DataSet
  172. $adapter.Fill($dataSet) | Out-Null
  173. $message = ("Ran query: [$sqlCommand]. Returned " + $dataSet.Tables[0].Rows.Count.ToString() + " row(s).")
  174. Write-Log -severity 5 -message $message
  175. return $dataSet.Tables
  176. }
  177. function Invoke-SQLWithParams {
  178. param(
  179. [string] $sqlCommand = $(throw "Please specify a query."),
  180. $params = @()
  181. )
  182. $command = new-object system.data.sqlclient.sqlcommand($sqlCommand,$Conn)
  183. Foreach($param in $params){
  184. $command.Parameters.AddWithValue($param.name, $param.value)
  185. }
  186. $adapter = New-Object System.Data.sqlclient.sqlDataAdapter $command
  187. $dataset = New-Object System.Data.DataSet
  188. $adapter.Fill($dataSet) | Out-Null
  189. $message = ("Ran query: [$sqlCommand]. Returned " + $dataSet.Tables[0].Rows.Count.ToString() + " row(s).")
  190. Write-Log -severity 5 -message $message
  191. return $dataSet.Tables
  192. }
  193. function Invoke-SQLProc {
  194. param(
  195. [string] $procName = $(throw "Please specify a stored procedure."),
  196. [hashtable] $parameters=@{}
  197. )
  198. $command = new-object system.data.sqlclient.sqlcommand($sqlCommand,$Conn)
  199. $command.CommandType = [System.Data.CommandType]::StoredProcedure
  200. $command.CommandText = $procName
  201. $paramText = ""
  202. foreach($p in $parameters.Keys){
  203. $command.Parameters.AddWithValue("@$p",[string]$parameters[$p]).Direction = [System.Data.ParameterDirection]::Input
  204. $paramText += ($p + " = " + [string]$parameters[$p] + ", ")
  205. }
  206. $adapter = New-Object System.Data.sqlclient.sqlDataAdapter $command
  207. $dataset = New-Object System.Data.DataSet
  208. $adapter.Fill($dataSet) | Out-Null
  209. $message = ("Ran proc: [$procName] with parameters [$paramText]. Returned " + $dataSet.Tables[0].Rows.Count.ToString() + " row(s).")
  210. Write-Log -severity 5 -message $message
  211. return $dataSet.Tables
  212. }
  213. Function ConvertTableTo-Object {
  214. param(
  215. $table = $null
  216. )
  217. $row = @{}
  218. for($i = 0; $i -lt $table.Columns.Count; $i++){
  219. $colName = $table.Columns[$i]
  220. $val = $table.Rows[0].ItemArray[$i]
  221. $row | Add-Member -MemberType NoteProperty –Name $colName –Value $val
  222. }
  223. return $row
  224. }
  225. Function ConvertRowTo-Object {
  226. param(
  227. $columns = $null,
  228. $row = $null
  229. )
  230. $obj = @{}
  231. for($i = 0; $i -lt $columns.Count; $i++){
  232. $colName = $columns[$i]
  233. $val = $row.ItemArray[$i]
  234. $obj | Add-Member -MemberType NoteProperty –Name $colName –Value $val
  235. }
  236. return $obj
  237. }