Guest User

Untitled

a guest
Sep 30th, 2018
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. #Requires -Version 5.0
  2.  
  3. <#
  4. .SYNOPSIS
  5. Connect to a SQL Server Database Engine and return the server object.
  6.  
  7. .PARAMETER SQLServer
  8. String containing the host name of the SQL Server to connect to.
  9.  
  10. .PARAMETER SQLInstanceName
  11. String containing the SQL Server Database Engine instance to connect to.
  12.  
  13. .PARAMETER SetupCredential
  14. PSCredential object with the credentials to use to impersonate a user when connecting.
  15. If this is not provided then the current user will be used to connect to the SQL Server Database Engine instance.
  16.  
  17. .PARAMETER LoginType
  18. If the SetupCredential is set, specify with this parameter, which type
  19. of credentials are set: Native SQL login or Windows user Login. Default
  20. value is 'WindowsUser'.
  21. #>
  22. function Connect-SQL
  23. {
  24. [CmdletBinding()]
  25. param
  26. (
  27. [Parameter()]
  28. [ValidateNotNullOrEmpty()]
  29. [System.String]
  30. $ServerName = $env:COMPUTERNAME,
  31.  
  32. [ValidateNotNullOrEmpty()]
  33. [System.String]
  34. $SQLInstanceName = "MSSQLSERVER",
  35.  
  36. [AllowNull()]
  37. [System.Management.Automation.PSCredential]
  38. $SetupCredential,
  39.  
  40. [Parameter()]
  41. [ValidateSet("WindowsUser", "SqlLogin")]
  42. [System.String]
  43. $LoginType = "WindowsUser"
  44. )
  45.  
  46. Try {
  47.  
  48. Write-Verbose "ServerName: $ServerName"
  49. Write-Verbose "SQLInstanceName: $SQLInstanceName"
  50. Write-Verbose "SetupCredential: $SetupCredential"
  51. Write-Verbose "LoginType: $LoginType"
  52.  
  53. switch ($SQLInstanceName) {
  54. ## check for default instance name.
  55. {$_ -eq "MSSQLSERVER"} {
  56. $serverInstance = $ServerName
  57. }
  58. ## check for use of port number.
  59. {$_ -match "^\d+$"}{
  60. $serverInstance = "$ServerName,$SQLInstanceName"
  61. }
  62. ## assume named instance is required.
  63. default {
  64. $serverInstance = "$ServerName\$SQLInstanceName"
  65. }
  66. }
  67. Write-Verbose "Create connection on: $serverInstance"
  68.  
  69. ## open t-sql endpoint with target server.
  70. Write-Verbose "Creating SQL Server Management Object."
  71. $SQLServer = [Microsoft.SqlServer.Management.Smo.Server]::New()
  72. Write-Verbose "Server Management Object created."
  73.  
  74. if ($SetupCredential) {
  75.  
  76. if ($LoginType -eq "SqlLogin") {
  77. $SQLServer.ConnectionContext.LoginSecure = $false
  78. $SQLServer.ConnectionContext.Login = $SetupCredential.UserName
  79. $SQLServer.ConnectionContext.set_SecurePassword($SetupCredential.Password)
  80. }
  81.  
  82. if ($LoginType -eq "WindowsUser") {
  83. $SQLServer.ConnectionContext.ConnectAsUser = $true
  84. $SQLServer.ConnectionContext.ConnectAsUserName = $SetupCredential.UserName
  85. $SQLServer.ConnectionContext.ConnectAsUserPassword = $SetupCredential.GetNetworkCredential().Password
  86. }
  87. }
  88.  
  89. $SQLServer.ConnectionContext.ServerInstance = $serverInstance
  90.  
  91. Write-Verbose "Connecting to SQL instance: $serverInstance."
  92. $SQLServer.ConnectionContext.Connect()
  93.  
  94. ## ALL DONE
  95. if ( $SQLServer.Status -match "^Online$" ) {
  96. Write-Verbose "Connected to: $serverInstance."
  97. Return $SQLServer
  98. }
  99. else {
  100. $message = "Failed to connect to server: $serverInstance."
  101. throw $message
  102. }
  103.  
  104. }
  105. Catch [System.Exception] {
  106. $PSCmdlet.ThrowTerminatingError($PSItem)
  107. }
  108.  
  109. }
Add Comment
Please, Sign In to add comment