Connect-MSSQL-IPWindowsAuth


posted by Richard Giles
10-06-2008

Downloads: 571
File size: 2.3kB
Views: 2,127

Embed
Connect-MSSQL-IPWindowsAuth
  1. ## ===================================================================== 
  2. ## Title       : Connect-MSSQL-IPWindowsAuth 
  3. ## Description : Connect to SQL Server using IP address, instance and  
  4. ##                  Windows authentication 
  5. ## Author      : Idera 
  6. ## Date        : 9/1/2008 
  7. ## Input       : -ipAddress < xxx.xxx.xxx.xxx | xxx.xxx.xxx.xxx\instance > 
  8. ##               -verbose  
  9. ##               -debug     
  10. ## Output      : Database names and owners 
  11. ## Usage            : PS> . Connect-MSSQL-IPWindowsAuth -ipAddress 127.0.0.1 -v -d 
  12. ## Notes         :  
  13. ##    Tag            : MSSQL, connect, IP, Windows Authentication 
  14. ## Change Log    : 
  15. ## ===================================================================== 
  16.  
  17. param 
  18.       [string]$ipAddress = "127.0.0.1"
  19.     [switch]$verbose
  20.     [switch]$debug 
  21.  
  22. function main() 
  23.     if ($verbose) {$VerbosePreference = "Continue"
  24.     if ($debug) {$DebugPreference = "Continue"
  25.     Connect-MSSQL-IPWindowsAuth $ipAddress 
  26.  
  27. function Connect-MSSQL-IPWindowsAuth($ipAddress
  28.     trap [Exception]  
  29.     
  30.         write-error $("TRAPPED: " + $_.Exception.Message); 
  31.         continue
  32.     
  33.      
  34.     # Load-SMO assemblies 
  35.     [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo"
  36.     [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SqlEnum"
  37.     [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoEnum")  
  38.     [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.ConnectionInfo")  
  39.      
  40.     # Instantiate a server object 
  41.     # TIP: using PowerShell "`" to signify line continuation 
  42.     Write-Debug "Creating SMO Server object..." 
  43.     $smoServer = New-Object -typename Microsoft.SqlServer.Management.Smo.Server
  44.         -argumentlist "$ipAddress" 
  45.      
  46.     # Use Windows Authentication by setting LoginSecure to TRUE 
  47.     Write-Debug "Setting Windows Authentication mode..." 
  48.     $smoServer.ConnectionContext.set_LoginSecure($TRUE
  49.      
  50.     # Clear the screen 
  51.     # TIP: cls will clear the PowerShell console 
  52.     cls 
  53.     Write-Host Your connection string contains these values: 
  54.     Write-Host 
  55.     $smoServer.ConnectionContext.ConnectionString.Split(";"
  56.     Write-Host 
  57.      
  58.     # List information about the databases 
  59.     Write-Host "Databases on " $ipAddress 
  60.     Write-Host 
  61.     foreach ($db in $smoServer.Databases)  
  62.     
  63.         write-host "Database Name : " $db.Name 
  64.         write-host "Owner         : " $db.Owner 
  65.         write-host 
  66.     
  67.  
  68. main 

Connect to SQL Server using IP address, instance and Windows authentication.

Copyright 2012 PowerShell.com. All rights reserved.