简体   繁体   English

使用数组向 ssms 中的用户列表添加权限

[英]Use an array to add permission to a list of users in ssms

I run several classrooms and lab environments.我经营着几个教室和实验室环境。 The students in one of the classes have to log into the interface and connect with my server, which then creates their account in SSMS.其中一个班级的学生必须登录界面并连接到我的服务器,然后在 SSMS 中创建他们的帐户。 Then I have to go in and manually add the permissions they need to do what they need to do for class.然后我必须在 go 中手动添加他们需要为 class 做他们需要做的事情的权限。 When my server "re-syncs" with the other online server, they lose their SSMS permissions.当我的服务器与其他在线服务器“重新同步”时,它们会失去 SSMS 权限。 (I'm being purposefully vague, don't focus on why they lose perms. I'm not doing anything wrong). (我故意含糊其辞,不要专注于他们为什么会失去烫发。我没有做错任何事)。

I can create the list of users and save it to an array.我可以创建用户列表并将其保存到数组中。 I know the ssms commands I need to run.我知道我需要运行的 ssms 命令。 Where I'm drawing a blank is how do I get my PS1 into the SSMS command.我在空白处是如何让我的 PS1 进入 SSMS 命令。 I'm fairly sure I need to do a foreach loop and save it to a.sql file and run that, but my google skills are failing me.我相当确定我需要做一个 foreach 循环并将其保存到一个 .sql 文件并运行它,但我的谷歌技能让我失望了。 Any help would be appreciated, Also my first time posting on this website.任何帮助将不胜感激,也是我第一次在这个网站上发帖。 so I'm sorry if this looks silly.所以如果这看起来很傻,我很抱歉。

$Response = 'y'
$User = $Null
$UserList = @()

#Get a list of users#
Do
{
    $User = Read-Host -Prompt 'Input user name'
    $Response = Read-Host 'Would you like to add any 
additional users? y/n'
    if ($User -ne '') {$UserList += $User}
    }
Until ($Response -eq 'n')

#Loop through the list of users and add to a sql file#
foreach ($User in $UserList) {
    
    }

#The SQL command to add user perms in SQL#
#sqlcmd
#use <db>
#exec sp_addrolemember 'group1','$User'
#exec sp_addrolemember 'group2','$User'
#go

You could do something like the following to create file.sql that you can run with Invoke-SqlCmd or from TSQL.您可以执行以下操作来创建file.sql ,您可以使用Invoke-SqlCmd或 TSQL 运行该文件。

$template = { @"
exec sp_addrolemember 'group1','$user'
exec sp_addrolemember 'group2','$user'
"@
}

'use <db>' | Set-Content file.sql
$SqlCommands = foreach ($user in $userlist) {
    & $template
}

$SqlCommands | Add-Content file.sql
'go' | Add-Content file.sql

Explanation:解释:

Using a script block ( {} surrounding code), you can specify variables that are only evaluated when the script block is called.使用脚本块( {}周围的代码),您可以指定仅在调用脚本块时评估的变量。 You can simply call a script block in a child scope using & operator.您可以使用&运算符简单地调用子 scope 中的脚本块。

@""@ denotes a here-string . @""@表示here-string

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何更改用户权限以通过 ssms 中的 powershell 连接到数据库引擎? - How do change a users permission to connect to database engine through powershell in ssms? 使用Powershell将用户添加到组中 - Use powershell to add users to a group 将数组添加到数组列表 - Add an array to an array list 将从列表中检索到的用户添加到另一个用户列表中 - Add users retrieved from a list to another list of users 用于列出用户、角色和将用户添加到 Azure SQL DB 的 PowerShell 脚本 - PowerShell script for list users, role, and add users to Azure SQL DB 将 DisplayName 列表中的用户添加到安全组 - Add users from DisplayName list to security group 哪些 NameSpace、NameSpaceID、Token 和/或位将授予项目管理员添加/删除用户的权限(任何权限/AzureDevops 大师?)) - What NameSpace, NameSpaceID, Token and/or bit will give permission to Project Admins to add/Remove users (Any Permissions/AzureDevops gurus?)) Powershell / dsquery将一组用户数组中的用户列表拉成一个具有唯一用户的数组 - Powershell / dsquery Pull list of users in an array of groups make an array with the unique users 将目录中的文件列表添加到数组列表 - Add list of files in a directory to array list 从文件夹中删除用户权限 - Remove a users permission from folder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM