SQL Server Slideshow

  1. cls 
  2. $server = 'ENTER SERVER NAME HERE' 
  3.  
  4. [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | Out-Null 
  5.  
  6. #this only looks at the default instance 
  7. $cn = new-object system.data.SqlClient.SqlConnection("Data Source=MSSQLSERVER;Integrated Security=SSPI;Initial Catalog=master"); 
  8. $s = new-object ('Microsoft.SqlServer.Management.Smo.Server') $server 
  9.      
  10. #$val =$s.Logins | select Name, DefaultDatabase  
  11.  
  12.  
  13. function getRoles($roles){ 
  14.     if($roles.EnumRoles().Count -eq 0){'<h4><b>NO ROLES<b/></h4>'
  15.      
  16.     foreach($role in $roles.EnumRoles()){ 
  17.         $role + '<br/>' 
  18.     
  19.  
  20.  
  21. function buildADRow($rp){ 
  22.     $mep = $r | select  Login  
  23.     $inGroup = (Get-QADGroupMember $mep.Login -Enabled -Indirect -SizeLimit 18000 ) 
  24.     foreach($account in $inGroup){ 
  25.         '<tr>' 
  26.             '<td></td>' 
  27.             '<td>' + $account.NTAccountName + '</td>' 
  28.             '<td>' + (getRoles($rp)) + '</td>' 
  29.         '</tr>' 
  30.     
  31.  
  32. function buildRow($rp){ 
  33.     $t= $rp | select  Name, Login 
  34.     '<tr>' 
  35.         '<td  colspan=2>'  
  36.             if($t.Login -eq ''){ 
  37.                 $t.Name + '</td>' 
  38.             }else
  39.                 $t.Login +'</td>' 
  40.             
  41.         '<td>' + (getRoles($rp)) + '</td>' 
  42.     '</tr>'     
  43.  
  44.  
  45. $dbs = $s.Databases 
  46. '<html><style type="text/css">h4 {color:red;}</style><body>' 
  47.     foreach ($db in $dbs) { 
  48.         '<br/><br/><br/>' 
  49.            $dbname = $db.Name 
  50.            $users = $db.Users 
  51.         '<table border=1 width=100%>' 
  52.         '<tr> 
  53.             <td colspan=3 bgcolor=#C0C0C0><b>' + $dbname +'</b></td> 
  54.         </tr>' 
  55.         '<tr> 
  56.             <td width=25%><b>SQL Account</b></td> 
  57.             <td width=25%><b>Group Member</b></td> 
  58.             <td width=25%><b>Rights</b></td> 
  59.         </tr>'  
  60.          
  61.             if($users.get_Count() -cge 1){ 
  62.                    foreach($r in $users){ 
  63.                            if($r.get_HasDBAccess() -eq 'True'){ 
  64.                                   if($r.LoginType -contains 'WindowsGroup'){ 
  65.                                     buildRow($r
  66.                                     buildADRow($r
  67.                                
  68.                                else
  69.                                        buildRow($r
  70.                                
  71.                         
  72.                    
  73.                
  74.         '</table>'     
  75.     
  76. '</body></html>' 
 
Loading...
Concentrated Tech NSoftware Dell Compellent Sponsored by Idera and Concentrated Tech and NSoftware and Dell Compellent
Copyright 2011 PowerShell.com. All rights reserved.