設置數據庫所有者是足夠簡單:
PS SQLSERVER:\SQL\MyServer\MyInstance\Databases\MyDatabase> $db = get-item .
PS SQLSERVER:\SQL\MyServer\MyInstance\Databases\MyDatabase> $db.SetOwner('sa')
PS SQLSERVER:\SQL\MyServer\MyInstance\Databases\MyDatabase> $db.alter()
#These two steps aren't necessary, but if you want to see the change
PS SQLSERVER:\SQL\MyServer\MyInstance\Databases\MyDatabase> $db.Refresh()
PS SQLSERVER:\SQL\MyServer\MyInstance\Databases\MyDatabase> $db.Owner
分配權限是有點棘手,當然有多種方式來分配權限。如果描述了要分配的權限(數據庫,對象,內置角色,服務器級別,模式等),這將有所幫助。以下是創建數據庫用戶和分配對象權限的方法:
$user = new-object ('Microsoft.SqlServer.Management.Smo.User') 'MyDatabase', 'MyUser'
$user.Login = 'MyUser'
$user.DefaultSchema = 'dbo'
$user.Create()
PS SQLSERVER:\SQL\MyServer\MyInstance\Databases\MyDatabase\tables> $tbl = get-childitem | where {$_.name -eq 'mytable'}
$tbl.Grant('SELECT','myUser')
這真的很有幫助。服務器場帳戶將需要內容數據庫的DBO權限。我大概可以弄清楚我需要從樣本中做些什麼。 – Charlie 2011-01-14 10:33:12