Connect-MSSQL-IPSQLAuth


posted by Richard Giles
10-06-2008

Downloads: 480
File size: 2.8kB
Views: 1,694

Embed
Connect-MSSQL-IPSQLAuth
  1. ## ===================================================================== 
  2. ## Title       : Connect-MSSQL-IPSQLAuth 
  3. ## Description : Connect to $ServerName using SQL Server authentication. 
  4. ##               This connection is not encrypted.  
  5. ##               User ID and Password are transmitted in plain text. 
  6. ## Author      : Idera 
  7. ## Date        : 6/27/2008 
  8. ## Input       : -ipAddress < xxx.xxx.xxx.xxx | xxx.xxx.xxx.xxx\instance > 
  9. ##               -verbose  
  10. ##               -debug     
  11. ## Output      : Database names and owners 
  12. ## Usage            : PS> . Connect-MSSQL-IPSQLAuth -ipAddress 127.0.0.1 -v -d 
  13. ## Notes         :  
  14. ##    Tag            : MSSQL, connect, IP, SQL Authentication 
  15. ## Change Log  : 
  16. ## ===================================================================== 
  17.  
  18. param 
  19.       [string]$ipAddress = "127.0.0.1"
  20.     [switch]$verbose
  21.     [switch]$debug 
  22.  
  23. function main() 
  24.     if ($verbose) {$VerbosePreference = "Continue"
  25.     if ($debug) {$DebugPreference = "Continue"
  26.     Connect-MSSQL-IPSQLAuth $ipAddress 
  27.  
  28. function Connect-MSSQL-IPSQLAuth($ipAddress
  29.     trap [Exception]  
  30.     
  31.         write-error $("TRAPPED: " + $_.Exception.Message); 
  32.         continue
  33.     
  34.      
  35.     # Load SMO assemblies 
  36.     [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo"
  37.     [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SqlEnum"
  38.     [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoEnum"
  39.     [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.ConnectionInfo"
  40.      
  41.     # Instantiate a server object 
  42.     $smoServer = New-Object -typename Microsoft.SqlServer.Management.Smo.Server
  43.         -argumentlist "$ipAddress" 
  44.      
  45.     # The connection will use SQL Authentication, so set LoginSecure to FALSE 
  46.     $smoServer.ConnectionContext.set_LoginSecure($FALSE
  47.      
  48.     # Pop a credentials box to get User Name and Password 
  49.     $LoginCredentials = Get-Credential 
  50.      
  51.     # If the user does not specify a domain, UserName will begin with a slash. 
  52.     # Remove leading slash from UserName 
  53.     $Login = $LoginCredentials.UserName -replace("\\",""
  54.      
  55.     # Set properties of ConnectionContext 
  56.     $smoServer.ConnectionContext.set_EncryptConnection($FALSE
  57.     $smoServer.ConnectionContext.set_Login($Login
  58.     $smoServer.ConnectionContext.set_SecurePassword($LoginCredentials.Password
  59.      
  60.     # The connection is established the first time you access the server's properties. 
  61.     cls 
  62.     Write-Host Your connection string contains these values: 
  63.     Write-Host 
  64.     Write-Host $smoServer.ConnectionContext.ConnectionString.Split(";"
  65.     Write-Host 
  66.      
  67.     # List info about databases on the instance. 
  68.     Write-Host "Databases on $ipAddress " 
  69.     Write-Host 
  70.     foreach ($Database in $smoServer.Databases)  
  71.     
  72.         write-host "Database Name : " $Database.Name 
  73.         write-host "Owner         : "    $Database.Owner 
  74.         write-host 
  75.     
  76.  
  77. main 

Connect to a SQL Server using SQL Server authentication. This connection is not encrypted. User ID and Password are transmitted in plain text.

Concentrated Tech NSoftware Dell Compellent Sponsored by Idera and Concentrated Tech and NSoftware and Dell Compellent
Copyright 2011 PowerShell.com. All rights reserved.