PowerShell: Changing MS SQL Server Login User Mapping - sql-server

I need to modify the User Mapping of a specific MS SQL Server Login using PowerShell. I need the Login to map to a specific database, while also selecting db_owner as a Database role membership. The only piece of code I have so far is code that will connect to MS SQ Server.
$svc_Obj = Get-WmiObject Win32_Service -filter "name='MSSQLSERVER'"
However, I'm not sure how I would be able to modify the Login's User Mapping with this. Could someone show me how to do this with either this code or with something else?

There are lots of examples all over the web on how to do this. A quick search using you favorite engine, using your posts' heading would give you a decent list on the topic. Even searching directly on this site, would give you a decent list.
For example:
Connect to SQL Server Database from PowerShell
I have looked around online for a while now and found many similar
problems but for some reason I can't seem to get this working.
I am just trying to connect to a SQL server database and output the
query results to a file - See PowerShell script below. What I am
uncertain about is how to integrate the User ID and Password into the
connection string.
Connect to SQL Server Database from PowerShell
Powershell - User Mapping SQL Server 2012
I am trying to script User Mapping for different Login accounts. I
have scripted the creation of users and individual server roles, but I
can't figure out how to set User Mapping with Powershell, I will also
need to set the Database Role membership, in Particular,
db_backupoperator
Powershell - User Mapping SQL Server 2012
Connect to SQL Server via Windows PowerShell with SQL Server
authentication
Problem In previous tips on Windows PowerShell with SQL Server, you've
seen how you can use Windows PowerShell and SMO to administer SQL
Server databases. Most of the examples have used Windows
authentication to connect to SQL Server. How can I connect to SQL
Server via Windows PowerShell using mixed mode authentication?
https://www.mssqltips.com/sqlservertip/1947/connect-to-sql-server-via-windows-powershell-with-sql-server-authentication/
SQL Server: Change the Login Password using PowerShell
Here’s a quick Article detailing a PowerShell script that can be used
to change the password for a SQL Server Login.
https://social.technet.microsoft.com/wiki/contents/articles/22767.sql-server-change-the-login-password-using-powershell/rss.aspx

Related

Is there a possibility to connect from MS Access to SQL Server using alternative user Windows authentication?

I'm trying to create a linked table connection in MS Access to a SQL Server instance table through Linked Table Manager in the External Data ribbon.
To grant access to this SQL Server instance in READONLY mode, a DBA in the organization I work for create a new Domain User Account different from my personal one.
In other applications, Windows authentication as different user in case of Excel and Powerbi and Run as different for SSMS works fine.
In MS Access there is no -Use Alternative Windows credential- term of authentication and all the others that I have tried do not function (Active Directory Password/Integrated/Interactive/servicePrincipal).
Does anyone know a possible solution to access the SQL Server from MS Access using a different Windows Domain user?
open the command prompt in administrative mode and run the below command
replace <<domain\username>> with user details.
update the msaccess.exe location("C:\Program Files\Microsoft Office\root\Office16\MSACCESS.EXE") if it differs in your system.
C:\Windows\System32\runas.exe /netonly /user:<<domain\username>> "C:\Program Files\Microsoft Office\root\Office16\MSACCESS.EXE"
by this way ms-access will be opened using alternate user credential,
then click the "Linked Table Manager" and connect to SQL server, it will connect to SQL server using alternate user credential.

SSIS cannot connect to SSMS using SQL Server Authentication

I'm trying to set up a connection to SSMS from SSIS project I'm working on with SQL Server Authentication.
I set up a cube_user login and gave it permission to my database transactions
Here is how I set up my cube_user:
Login Name, password. I also chose the default DB below to transactions, the appropriate DB.
Then in User Mapping tab, I gave user owner permissions and I checked in the appropriate DB
Having done all that, it seems like it should work. I then started an SSIS project and tried to connect to it like this:
As you can the drop down that is supposed to display databases, is blank. However, If i tried to connect to it using Windows Authentication, I am able to see all DBs....
Is there something wrong I'm doing. Can you not connect to SSMS using SQL Server Authentication?
Please check the following things:
In properties of cube_user - that it has at least guest rights on the RDBMS (SQL Server itself). User rights on specific DB will not give the permission to connect to SQL Server. The properties are in SSMS - Security\Logins - specific Login properties - Server Roles. See example below
On the third screenshot where you specify user name and password - specify those of cube_user, not admin.
The reason why you see list of DBs with Windows Auth - you do it under your account which presumably has DBA user rights on the SQL Server.

Unable to connect Excel 2016 to Azure SQL Database even though SSMS connects without issue

I have an Azure SQL Server (native, not inside a VM) that I connect to every day via SSMS on my local machine - Windows 10 Pro.
For the first time, I am trying to connect Excel 2016 via Data-> Get Data-> From Database -> From SQL Server Database.
From there I am asked for the Server name, followed by login credentials, which I use the exact username and password I'm using for SSMS. I get an error message that says We couldn't authenticate with the credentials provided. Please try again.
This doesn't make any sense to me. My SSMS work perfectly, and Excel is under the same IP address as SSMS which is whitelisted. Is there something else inside Excel's settings which I'm missing?
FYI, I tried the server name both with and without 1433 after it just to be sure. Same result.
Screenshots below.
I tried using the data, get data, sql server option. It does not understand azure sql database.
If I use the data, new query, azure sql database option, I have the same screens that you have. I am using Excel 2016.
Here is your issue!
There are two tabs on the second screen. You are filling in the window tab. Therefore, it is trying to log into the database with windows (active directory) authentication. Choose the second option on the left pane, database. Use database credentials.
This should work with no issues.
I tested it out on my local machine.
for azure sql you always need to provide the database name, as you do not have rights to list databases on azure. (so this optional is not that optional for azure :)
I could list tables on azure sql from office 2016 on mac, nevertheless the excel query app shows only the tables but does not show any views. Once i made a table the import was hanging while trying to get the data. making a smaller table (just 60K rows) did not help. The SQL server connectivity seems to be broken/very slow on mac excel 2016.

SQL Server Restrict Access to Database by Windows Authentication Mode

I am facing problem regarding database permissions. I have created my database named FCProject. I created a login for that database in SQL Server Management Studio. My login is working fine but all I want to restrict access to FCProject database through users who can log in to SQL Server through Windows authentication mode.
Because I want only FC login to be able to view or modify data of FC database in SQL Server Management Studio.
I have tried creating a login through Management Studio. Is there any problem in creating login? Or something else? Please help
You are mixing up two things that should be kept separate here:
A login is on the server level - it gives an account (a Windows account, or a SQL Server specific account) the permission to connect to this server
Each database can then define user for its own use - based on those logins.
So in your case, what you need to do is to:
create a number of logins on the server-level
create users in your FCLogin database for just those logins
and then you're fine - only those accounts that you've defined explicitly can access your FCLogin database and use it.
See the Stairway to SQL Server security series of articles on SQL Server Central (very well worth registering for their contents!) which explains all the ins and outs in great detail

Access Denied to SQL Server (through Excel VBA) when using SQL User without Login

I'm having some trouble with my SQL Server Logons. I'm building an Excel Spreadsheet that will retrieve data from an SQL Server (2008) and then display it in what ever way is required etc...
I have set up a SQL User without Login 'genericReadOnly' and 'genericReadWrite', it was my intention to then capture the windows login with environ("username") which will then retrieve what reports that person is allowed to access. This was working fine whilst me and my team were working on it, but we are already set up on the SQL Server as 'Windows Users'.
I've now sent the file to someone who isn't also listed as a Windows User and it fails with ACCESS DENIED FOR DOMAIN\USERNAME, even though the connection string uses the genericReadOnly SQL Username.
Any ideas as I cannot add specific usernames for everyone that requires access.
I would suggest creating a domain group and add that group as a windows login to SQL instance with the appropriate permissions.
Then add anyone that needs to use your Excel file just has to be a member of that group and requires no changes to your code or SQL setup.

Resources