Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Looking for help with PS script to insert into SQL.

$
0
0

The following PS script was written by somone at MS, to enable searching each database in a SQL server, from a list of servers, for SQL database roles that are nested. 

#***********************
#    For each server runs a query against each database
#    
#    $InstanceList file should be a file of SQL instances in either server, server,port or server\instancename
#    
#************************

param
(
    [string]$InstanceList = "C:\PowerShell\SQLServerList.txt"
)


## Get current date-time and assign to variable
$currentDT = Get-Date

# Load SMO assembly
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | Out-Null;

#Initialize Array to hold new database objects
$iDatabases = @()

#Loop over each instance provided
foreach ($instance in $(get-content $InstanceList))
{
     try
    {        	
		"Connecting to $instance" | Write-Host -ForegroundColor Blue

                $srv = New-Object "Microsoft.SqlServer.Management.SMO.Server" $instance;
	        #How many seconds to wait for instance to respond
        	$srv.ConnectionContext.ConnectTimeout = 5        
		$srv.get_BuildNumber() | out-Null
    }
    catch
    {"Instance Unavailable - Could Not connect to $instance." | Write-Host -ForegroundColor Red
        continue
    }

        $srv.ConnectionContext.StatementTimeout = $QueryTimeout

        foreach($Database in $srv.Databases)
    {
	        #create object with all string properties
	        #populate object with known values
		$db = $database.Name
		$server = $srv.Name

		$Screen = "Server Name: " + $server +    "         Database Name: " + $db
		$Screen | ft

		$SqlConnection = New-Object System.Data.SqlClient.SqlConnection 
		$ConnectHere = "Server=" + $server + ";Database=" + $db + ";Integrated Security=True"
		$SqlConnection.ConnectionString = $ConnectHere 

		$SqlCmd = New-Object System.Data.SqlClient.SqlCommand 
		

		#$SqlCmd.CommandText = "SELECT [Name] FROM [" + $db + "].[sys].[database_principals]"

		$q = "SELECT r.name, r.type, r.type_desc FROM [" + $db
		$q = $q + "].[sys].[database_role_members] j"
		$q = $q + " INNER JOIN [" + $db
		$q = $q + "].[sys].[database_principals] r"
		$q = $q + " ON j.role_principal_id = r.principal_id "
		$q = $q + " INNER JOIN [" + $db
		$q = $q + "].[sys].[database_principals] m"
		$q = $q + " ON j.member_principal_id = m.principal_id "
		$q = $q + " WHERE m.type = 'R' "

		# Proofing: Uncomment the following line to view the query created in the variable $q
		#$q | ft

		$SqlCmd.CommandText = $q
		

		$SqlCmd.Connection = $SqlConnection 
		$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter 
		$SqlAdapter.SelectCommand = $SqlCmd 
		$DataSet = New-Object System.Data.DataSet 
		$SqlAdapter.Fill($DataSet) 
		$SqlConnection.Close() 
		$DataSet.Tables[0] 
		$DataSet | FOREACH-OBJECT { [string]$_.Name  }

                #add the iDatabase object to the array of iDatabase objects
	        $iDatabases += $iDatabase
	    }

}



I would like to insert the following information from the script in rows in a SQL Server table:

  • SQL Server\Instance  (Server1\SQL1) --same format from the text file
  • Database Name
  • Role Name
  • Role Type
  • Role Type Desc
  • $currentDT

I've done this with other scripts (with help) but I can't see how to get the values from this one.


-Al H


Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>