Advertisement
Guest User

Untitled

a guest
Mar 8th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 KB | None | 0 0
  1. workflow Runbook-UPDATESTATS
  2. {
  3. param(
  4. [parameter(Mandatory=$True)]
  5. [string] $SqlServer,
  6.  
  7. [parameter(Mandatory=$True)]
  8. [string] $Database,
  9.  
  10. [parameter(Mandatory=$True)]
  11. [string] $SQLCredentialName
  12.  
  13.  
  14. )
  15.  
  16. # Get the stored username and password from the Automation credential
  17. $SqlCredential = Get-AutomationPSCredential -Name $SQLCredentialName
  18. if ($SqlCredential -eq $null)
  19. {
  20. throw "Could not retrieve '$SQLCredentialName' credential asset. Check that you created this first in the Automation service."
  21. }
  22.  
  23. $SqlUsername = $SqlCredential.UserName
  24. $SqlPass = $SqlCredential.GetNetworkCredential().Password
  25.  
  26. $TableNames = Inlinescript {
  27.  
  28. # Define the connection to the SQL Database
  29. $Conn = New-Object System.Data.SqlClient.SqlConnection("Server=tcp:$using:SqlServer,$using:SqlServerPort;Database=$using:Database;User ID=$using:SqlUsername;Password=$using:SqlPass;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;")
  30.  
  31. # Open the SQL connection
  32. $Conn.Open()
  33.  
  34. # Return the tables
  35. $Cmd=new-object system.Data.SqlClient.SqlCommand($SQLCommandString, $Conn)
  36. $Cmd.CommandTimeout=120
  37.  
  38. # Get the list of tables with their object ids
  39. $SQLCommandString = @"
  40. SELECT '['+s.name +'].[' + t.name + ']' AS TableName
  41. FROM sys.tables t
  42. join sys.schemas s on t.schema_id = s.schema_id
  43. "@
  44.  
  45. $Cmd=new-object system.Data.SqlClient.SqlCommand($SQLCommandString, $Conn)
  46. $Cmd.CommandTimeout=120
  47.  
  48. # Execute the SQL command
  49. $TableSchema =New-Object system.Data.DataSet
  50. $Da=New-Object system.Data.SqlClient.SqlDataAdapter($Cmd)
  51. [void]$Da.fill($TableSchema)
  52.  
  53. $Conn.Close()
  54. }
  55.  
  56. # Interate through tables with high fragmentation and rebuild indexes
  57. ForEach ($TableName in $TableNames)
  58. {
  59. Write-Verbose "Creating checkpoint"
  60. Checkpoint-Workflow
  61. Write-Verbose "Updating Table $TableName..."
  62.  
  63. InlineScript {
  64.  
  65. $SQLCommandString = @"
  66. EXEC('UPDATE STATISTICS $Using:TableName WITH FULLSCAN')
  67. "@
  68.  
  69. # Define the connection to the SQL Database
  70. $Conn = New-Object System.Data.SqlClient.SqlConnection("Server=tcp:$using:SqlServer,$using:SqlServerPort;Database=$using:Database;User ID=$using:SqlUsername;Password=$using:SqlPass;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;")
  71.  
  72. # Open the SQL connection
  73. $Conn.Open()
  74.  
  75. # Define the SQL command to run. In this case we are getting the number of rows in the table
  76. $Cmd=new-object system.Data.SqlClient.SqlCommand($SQLCommandString, $Conn)
  77. # Set the Timeout to be less than 30 minutes since the job will get queued if > 30
  78. # Setting to 25 minutes to be safe.
  79. $Cmd.CommandTimeout=1500
  80.  
  81. # Close the SQL connection
  82. $Conn.Close()
  83. }
  84. }
  85.  
  86. Write-Verbose "Finished Indexing"
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement