Experts Exchange connects you with the people and services you need so you can get back to work.
// reades content of file and sets to $a
$a = Get-Content C:\Scripts\Test.txt
//Split the content with lines....
$a.Split("YourLineSeprator") | ForEach
//$b will be array that contains RemoteServerName,DatabaseName,Ad_GroupName,Role
$os = Get-WmiInfo Win32_OperatingSystem
$conn = New-Object System.Data.SqlClient.SqlConnection
//ConnectionString to Remote Server :: "Data Source=ServerName;Initial Catalog=DatabaseName;Integrated Security=SSPI;"
//Conn String if you have credentials :: "Data Source=ServerName;Initial Catalog=DatabaseName;uid=username;pwd=password"
$conn.ConnectionString = "Data Source=$b;Initial Catalog=$b;Integrated Security=SSPI;"
$cmd = New-Object System.Data.SqlClient.SqlCommand
$cmd.connection = $conn
//For below line (?) is Username for which you want to set permission..You haven't specified which in your question.
$cmd.commandtext = "CREATE LOGIN $b\(?) FROM WINDOWS WITH DEFAULT_DATABASE = $b;GO USE $b; CREATE USER (?) FOR LOGIN $b\(?); EXEC sp_addrolemember '$b', '(?)'"
Open in new window
At Springboard, we know how to get you a job in data science. With Springboard’s Data Science Career Track, you’ll master data science with a curriculum built by industry experts. You’ll work on real projects, and get 1-on-1 mentorship from a data scientist.
CREATE LOGIN [domain\user]
WITH DEFAULT_DATABASE = [DBName];
-- Add User to first database
CREATE USER [user] FOR LOGIN [domain\user];
EXEC sp_addrolemember '[DbRole]', '[user]'
Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.
Have a better answer? Share it in a comment.
Please enter a first name
Please enter a last name
Must be at least 4 characters long.
Join and Comment
From novice to tech pro — start learning today.
Premium members can enroll in this course at no extra cost.
Premium members get this course for $24.99.
Premium members get this course for $159.20.
Premium members get this course for $99.99.
Premium members get this course for $37.50.
Premium members get this course for $95.20.