Your technology certification is waiting. Enroll in Cloud Class ®
// 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
This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.
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.