Advertisement
Guest User

Untitled

a guest
Aug 10th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. <#
  2. .SYNOPSIS
  3. Outputs the number of records in the specified SQL Server database table.
  4.  
  5. .DESCRIPTION
  6. This runbook demonstrates how to communicate with a SQL Server. Specifically, this runbook
  7. outputs the number of records in the specified SQL Server database table.
  8.  
  9. In order for this runbook to work, the SQL Server must be accessible from the runbook worker
  10. running this runbook. Make sure the SQL Server allows incoming connections from Azure services
  11. by selecting 'Allow Windows Azure Services' on the SQL Server configuration page in Azure.
  12.  
  13. This runbook also requires an Automation Credential asset be created before the runbook is
  14. run, which stores the username and password of an account with access to the SQL Server.
  15. That credential should be referenced for the SqlCredential parameter of this runbook.
  16.  
  17. .PARAMETER SqlServer
  18. String name of the SQL Server to connect to
  19.  
  20. .PARAMETER SqlServerPort
  21. Integer port to connect to the SQL Server. Default is 1433
  22.  
  23. .PARAMETER Database
  24. String name of the SQL Server database to connect to
  25.  
  26. .PARAMETER Table
  27. String name of the database table to output the number of records of
  28.  
  29. .PARAMETER SqlCredentialAsseet
  30. Credential asset name containing a username and password with access to the SQL Server
  31.  
  32. .EXAMPLE
  33. Use-SqlCommandSample -SqlServer "somesqlserver.databases.windows.net" -SqlServerPort 1433 -Database "SomeDatabaseName" -Table "SomeTableName" -SqlCredentialAsset sqluserCredentialAsset
  34.  
  35. .NOTES
  36. AUTHOR: System Center Automation Team <---- Original Author #>
  37.  
  38. param(
  39. [parameter(Mandatory=$True)]
  40. [string] $SqlServer = '',
  41.  
  42. [parameter(Mandatory=$False)]
  43. [int] $SqlServerPort = 1433,
  44.  
  45. [parameter(Mandatory=$True)]
  46. [string] $Database = '',
  47.  
  48. [parameter(Mandatory=$True)]
  49. [string] $Table = 'log',
  50.  
  51. [parameter(Mandatory=$True)]
  52. [string] $SqlCredentialAsset = ''
  53. )
  54.  
  55. $SqlCredential = Get-AutomationPSCredential -Name $SqlCredentialAsset
  56.  
  57. if ($SqlCredential -eq $null)
  58. {
  59. throw "Could not retrieve '$SqlCredentialAsset' credential asset. Check that you created this first in the Automation service."
  60. }
  61. # Get the username and password from the SQL Credential
  62. $SqlUsername = $SqlCredential.UserName
  63. $SqlPass = $SqlCredential.GetNetworkCredential().Password
  64.  
  65. # Define the connection to the SQL Database
  66. $Conn = New-Object System.Data.SqlClient.SqlConnection("Server=tcp:$SqlServer,$SqlServerPort;Database=$Database;User ID=$SqlUsername;Password=$SqlPass;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;")
  67.  
  68. # Open the SQL connection
  69. $Conn.Open()
  70.  
  71. # Define the SQL command to run. In this case we are getting the number of rows in the table
  72. $Cmd=new-object system.Data.SqlClient.SqlCommand("delete from $Table where date < DATEADD(day, -180, GETDATE())", $Conn)
  73. $Cmd.CommandTimeout=120
  74.  
  75. # Execute the SQL command
  76. $Ds=New-Object system.Data.DataSet
  77. $Da=New-Object system.Data.SqlClient.SqlDataAdapter($Cmd)
  78. [void]$Da.fill($Ds)
  79.  
  80. # Output the count
  81. $Ds.Tables.Column1
  82.  
  83. # Close the SQL connection
  84. $Conn.Close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement