Showing posts with label SQL Server 2005. Show all posts
Showing posts with label SQL Server 2005. Show all posts

11/29/16

What SQL Server is my SharePoint machine pointed to?

I recently powered on a SharePoint 2016 box that I haven't used in a while, when I powered it on and tried to browse to the default SharePoint site at http://machinename I received the classic .net error:

Server Error in '/' Application.

Runtime Error

I also attempted to open the SharePoint Central Administration page and received the same error.

I opened the web.config for the default SharePoint site at  c:\inetpub\wwwroot\wss\VirtualDirectories\80 and searched for the word 'error' located the xml node...

<customErrors mode="On" /> and changed this setting to 'Off'

Refreshing the http://machinename page now displays the hidden error message...

'This operation can be performed only on a computer that is joined to a server farm by users who have permissions in SQL Server to read from the configuration database. To connect this server to a server farm, use the SharePoint Products Configuration Wizard, located on the Start menu in Microsoft Sharepoint 2016 Products.

Unfortunately, if I attempt to run the SharePoint Products Configuration Wizard it also displays an error message 'Failed to detect if this server is joined to a server farm,,,'

Turns out the SQL server was just turned off but in my environment I have many SharePoint Servers, SQL Servers, Web Servers, etc. I wasn't sure which SQL server this SharePoint machine was pointed to and I only wanted to power on this one SQL Server.

There are probably multiple ways to determine which SQL Server the SharePoint box is pointed to but the way I chose to find out was to just reference a registry key that defined the connection string to the configuration database. The key name is dsn and the Data value is populated with the connection string.



My Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Shared Tools\Web Server Extensions\16.O\Secure\ConfigDb




8/14/11

Call a SQL Stored Procedure from Powershell with a parameter

Recently I had to write a Powershell script that iterates a text file of user names and calls a stored procedure  to delete users from a database. The text file just contains account names on each line and must be in the same directory as the Powershell script.



$conn = new-Object System.Data.SqlClient.SqlConnection("Data Source=databaseservername;User ID=sa; Password=password; Initial Catalog=databasename")
foreach ($user in Get-Content “UserList.txt”)

{
$conn.Open() | out-null
$cmd = new-Object System.Data.SqlClient.SqlCommand("SP_DeleteUser_By_AccountName", $conn)

$cmd.CommandType = [System.Data.CommandType]::StoredProcedure
$cmd.Parameters.Add("@AccountName",[system.data.SqlDbType]::VarChar) | out-Null

$cmd.Parameters['@AccountName'].Direction = [system.data.ParameterDirection]::Input

$cmd.Parameters['@AccountName'].value = $user


$cmd.ExecuteNonQuery()
$conn.Close()


}