Home Forums Programming Powershell how in powershell (smo and scripter or other object )list tables with dependencies,indexes,FK,constrains RE: how in powershell (smo and scripter or other object )list tables with dependencies,indexes,FK,constrains

  • Found this at https://technet.microsoft.com/en-us/library/ms162153%28v=sql.105%29.aspx

    Scripting Out the Dependencies for a Database in PowerShell

    This code example shows how to discover the dependencies and iterate through the list to display the results.

    # Set the path context to the local, default instance of SQL Server.

    CD \sql\localhost\default

    # Create a Scripter object and set the required scripting options.

    $scrp = New-Object -TypeName Microsoft.SqlServer.Management.SMO.Scripter -ArgumentList (Get-Item .)

    $scrp.Options.ScriptDrops = $false

    $scrp.Options.WithDependencies = $true

    $scrp.Options.IncludeIfNotExists = $true

    # Set the path context to the tables in AdventureWorks2008.

    CD Databases\AdventureWorks2008R2\Tables

    foreach ($Item in Get-ChildItem)

    {

    $scrp.Script($Item)

    }