我有VB.Net子程序,我试图转换为Powershell。我不是很擅长.Net所以我需要一些帮助,但我找到了VB代码的基础并设法让它工作。我正在编写数据库中的所有对象,并需要获取角色的权限。这是VB代码:
Public Sub ObjectPermissions(ByVal dbrname As String)
' dbrname = database role name
' this code will enumerate permissions on objects for a role - need to exclude fixed and public roles
Dim dbrp As ObjectPermissionInfo()
dbrp = db.EnumObjectPermissions(dbrname)
Dim opi As ObjectPermissionInfo
If db.Roles(dbrname).IsFixedRole = False And db.Roles(dbrname).Name <> "public" Then
For Each opi In dbrp
fs.WriteLine("GRANT " + opi.PermissionType.ToString + " ON [" + opi.ObjectSchema + "].[" + opi.ObjectName + "] TO [" + opi.Grantee + "]")
fs.Flush()
Next
End If
End Sub
答案 0 :(得分:0)
这是对PowerShell的几乎直接翻译:
# substitute the assembly version to suit your SQL Server SMO version
Add-Type -AssemblyName 'Microsoft.SqlServer.Smo, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91'
$Server = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Server -ArgumentList 'YourServer'
$Database = $Server.Databases['YourDatabase']
$DbrName = 'YourRole'
$Role = $Database.Roles[$DbrName]
if (-not $Role.IsFixedRole -and $Role.Name -ne 'public') {
$Database.EnumObjectPermissions($DbrName) |
ForEach-Object {
Write-Output ('GRANT {0} ON [{1}].[{2}] TO [{3}]' -f $_.PermissionType, $_.ObjectSchema, $_.ObjectName, $_.Grantee)
}
}