Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Using SMO with Powershell


Using SMO with Powershell

Author
Message
Aaron C. Sentell
Aaron C. Sentell
SSC-Addicted
SSC-Addicted (405 reputation)SSC-Addicted (405 reputation)SSC-Addicted (405 reputation)SSC-Addicted (405 reputation)SSC-Addicted (405 reputation)SSC-Addicted (405 reputation)SSC-Addicted (405 reputation)SSC-Addicted (405 reputation)

Group: General Forum Members
Points: 405 Visits: 275
I'm new at SMO and new at Powershell. My eventual goal in the script below is to loop through all user databases on a single SQL Server and reverse engineer database object permissions to a file (one file for each database). At this point I'm trying to do this for all user tables, but I can't seem to figure out how to script out only object permissions. Can someone point me in the right direction?

Thanks,
Aaron

Here's the script so far...

---------------------------------------------------------------------------------------------------------
[reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo")
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$ScriptOpts = New-Object Microsoft.SqlServer.Management.Smo.ScriptingOptions

$ScriptOpts.Permission.True
$ScriptOpts.AllowSystemObjects.False
$ScriptOpts.ContinueScriptingOnError.True
$ScriptOpts.PrimaryObject.False


$servers = "SQLServerA"
foreach ($server in $servers)
{
write-host "Details of the Server :" $server
write-host "-----------------------------------"
$Server = new-object ("Microsoft.SqlServer.Management.Smo.Server") "$server"
write-host "Server Version: " $Server.Serverversion
write-host "Server Name: " $Server.Information.VersionString
foreach($database in $Server.databases)
{
write-host "Database:" $database.name
foreach($table in $database.tables)
{
write-host "Table: "$table.name
$table.Script()
}
}
}
cmille19
cmille19
Old Hand
Old Hand (303 reputation)Old Hand (303 reputation)Old Hand (303 reputation)Old Hand (303 reputation)Old Hand (303 reputation)Old Hand (303 reputation)Old Hand (303 reputation)Old Hand (303 reputation)

Group: General Forum Members
Points: 303 Visits: 724
I would say you are new to SMO also to fix your code:

1. You misspelled permission instead of permissions and need to use the Powershell $true builtin variable:

$ScriptOpts.Permissions = $true

2. The ScriptingOptions needs to be referenced in the Script() method constructor:

$table.Script($ScriptOpts)

3. You can use a regular expression to filter out just the permission statements:

$table.Script($ScriptOpts) | where {"$_" -match "GRANT" -or "$_" -match "DENY"}



Aaron C. Sentell
Aaron C. Sentell
SSC-Addicted
SSC-Addicted (405 reputation)SSC-Addicted (405 reputation)SSC-Addicted (405 reputation)SSC-Addicted (405 reputation)SSC-Addicted (405 reputation)SSC-Addicted (405 reputation)SSC-Addicted (405 reputation)SSC-Addicted (405 reputation)

Group: General Forum Members
Points: 405 Visits: 275
Thanks for your help. I haven't had time to try out your changes yet, but I did find another article that I think will be helpful to me as well from DatabaseJournal.com:

Microsoft Windows PowerShell and SQL Server 2005 SMO – Part 9
By Muthusamy Anantha Kumar aka The MAK

Using PowerShell and SMO to Generate an SQL Server Script

http://www.databasejournal.com/features/mssql/article.php/3700711
moff
moff
SSC Journeyman
SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)

Group: General Forum Members
Points: 83 Visits: 35
Setting PrimaryObjects to false works for scripting permissions-only for tables, views and UDFs.

However for sprocs it doesn't - the whole sproc is scripted with the permissions at the end.

Your idea of filtering the piped output is fine - except when scripting directly to file.

Am I misunderstanding what PrimaryObjects really means, or is this a bug?

BTW for completeness there should be REVOKE on your list too Smile
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search