Advertisement
Combreal

DBChecks.ps1

Mar 27th, 2024
1,295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. [void][system.reflection.Assembly]::LoadFrom("C:\Program Files (x86)\MySQL\MySQL Connector NET 8.3.0\MySql.Data.dll")
  2.  
  3. Function New-SQLUpdate ($connectionProperties, $id, $pageCount, $cover) {
  4.     $Connection = New-Object  MySql.Data.MySqlClient.MySqlConnection
  5.     $Connection.ConnectionString="server=$($connectionProperties.mysqlHost);uid=$($connectionProperties.mysqlUser);pwd=$($connectionProperties.mysqlPass);database=$($connectionProperties.mysqlDatabase)"
  6.     $Connection.Open()
  7.    
  8.     $command = New-Object MySql.Data.MySqlClient.MySqlCommand
  9.     $command.Connection = $Connection
  10.     $command.CommandText = "UPDATE isbninfo SET pagecount = @pagecount, cover = @cover WHERE id = @id"
  11.     $command.Parameters.AddWithValue("@pagecount", $pageCount) | Out-Null
  12.     $command.Parameters.AddWithValue("@cover", $cover) | Out-Null
  13.     $command.Parameters.AddWithValue("@id", $id) | Out-Null
  14.     $command.ExecuteNonQuery() | Out-Null
  15.      
  16.     $Connection.Close()
  17. }
  18.  
  19. $connectionParameters = New-Object PSObject
  20. $connectionParameters | Add-Member Noteproperty "mysqlHost" "localhost"
  21. $connectionParameters | Add-Member Noteproperty "mysqlUser" "myuser"
  22. $connectionParameters | Add-Member Noteproperty "mysqlPass" "mypass"
  23. $connectionParameters | Add-Member Noteproperty "mysqlDatabase" "mydb"
  24.  
  25. $connection = [MySql.Data.MySqlClient.MySqlConnection]@{ConnectionString="server=$($connectionParameters.mysqlHost);uid=$($connectionParameters.mysqlUser);pwd=$($connectionParameters.mysqlPass);database=$($connectionParameters.mysqlDatabase)"}
  26. $connection.Open()
  27.  
  28. $mySQLCommand = "SELECT id FROM isbninfo WHERE pagecount = 0 OR cover = ''"
  29. $SQLCommand = [MySql.Data.MySqlClient.MySqlCommand]@{Connection=$connection; CommandText=$mySQLCommand}
  30.  
  31. $SQLReader = $SQLCommand.ExecuteReader()
  32. While($SQLReader.Read()) {
  33.     #magic correction happens here - use Add-Book.ps1
  34.     #New-SQLUpdate $connectionParameters $SQLReader["id"] $pagecount $cover
  35. }
  36.  
  37. $SQLReader.Close()
  38. $connection.Close()
Tags: sql powershell
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement