我正在尝试使用PowerGUI运行此PowerShell脚本

时间:2013-12-06 19:26:25

标签: powershell

# Script all tables,triggers,views, stored procedures and udf in the database
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | out-null

$Server = $args[0]

$s = new-object ('Microsoft.SqlServer.Management.Smo.Server') $Server

$scrp = new-object ('Microsoft.SqlServer.Management.Smo.Scripter') ($s)

$date = get-date -format "M-d-yyyy"

$scrp.Options.AppendToFile = $True
$scrp.Options.ClusteredIndexes = $True
$scrp.Options.DriAll = $True
$scrp.Options.ScriptDrops = $False
$scrp.Options.IncludeHeaders = $True
$scrp.Options.ToFileOnly = $True
$scrp.Options.Indexes = $True
$scrp.Options.WithDependencies = $True
$scrp.Options.ScriptSchema = $True
$scrp.Options.Triggers = $True
$scrp.Options.ContinueScriptingOnError = $True
IF ( $args[1] )

{ 
$db=$s.Databases[$args[1]]
IF ($db.ID -gt 4)
    {
        $formatname = $db.name
        $FileName = "H:\0\"+ $Server + "_" + $formatname + "_" + $date + ".SQL" 
        echo "Scripting from $Server. $args[1] database objects to $FileName"
        $scrp.Options.FileName = $FileName
        echo "Scripting Tables ..."
#       $scrp.script($($db.Tables))
        echo "Scripting Triggers ..."
#       foreach ($tables in $db.Tables)
#       {
#           foreach ($Trigger in $tables.Triggers)
#           {
#               $scrp.Script($Trigger)  
#           }
#       }
        echo "Scripting views ..."
        $views = $db.Views | where {$_.IsSystemObject -eq $false}
        if ($views -ne $null)
        {
            $scrP.Script($views)
        }
        Echo "Scripting StoredProcedures ..."
        $StoredProcedures = $db.StoredProcedures | where {$_.IsSystemObject -eq     $false}
        if ($StoredProcedures -ne $null)
        {
            $scrp.Script($StoredProcedures)
        }
        echo "Scripting UDFs ..."
        $UserDefinedFunctions = $db.UserDefinedFunctions | where {$_.IsSystemObject -   eq $false}
        if ($UserDefinedFunctions -ne $null)
        {
            $scrp.Script($UserDefinedFunctions)
        }   
    }
}
ELSE
{

foreach ($db in $s.Databases)
{
    IF ($db.ID -gt 4)
    {
        $formatname = $db.name
        $FileName = "H:\scripts\" + $Server + "_" + $formatname + "_" + $date +    ".SQL" 
        echo "Scripting database objects to $FileName"
        echo "Scripting from $Server.$db.name database"
        $scrp.Options.FileName = $FileName
        echo "Scripting Tables ..."

        $scrp.script($($db.Tables))
        echo "Scripting Triggers ..."
        foreach ($tables in $db.Tables)
        {
            foreach ($Trigger in $tables.Triggers)
            {
                $scrp.Script($Trigger)  
            }
        }
        echo "Scripting views ..."
        $views = $db.Views | where {$_.IsSystemObject -eq $false}
        if ($views -ne $null)
        {
            $scrP.Script($views)
        }
        Echo "Scripting StoredProcedures ..."
        $StoredProcedures = $db.StoredProcedures | where {$_.IsSystemObject -eq   $false}
        if ($StoredProcedures -ne $null)
        {
            $scrp.Script($StoredProcedures)
        }
        echo "Scripting UDFs ..."
        $UserDefinedFunctions = $db.UserDefinedFunctions | where {$_.IsSystemObject -  eq $false}
        if ($UserDefinedFunctions -ne $null)
        {
            $scrp.Script($UserDefinedFunctions)
        }
    }
}   
}

echo "Done"

我收到此错误:

  

使用“1”参数调用“Script”的异常:“Server'corpbillingdata'的脚本失败了。”    来自Travis \ scriptdatabase1 \ scriptdatabase1.ps1的C:\ Users \ jlauf \ Documents \ SQL助手:77 char:16       + $ scrp.script<<<< ($($ db.Tables))       + CategoryInfo:NotSpecified:(:) [],MethodInvocationException       + FullyQualifiedErrorId:DotNetMethodException

1 个答案:

答案 0 :(得分:0)

  • 使用C:\ ScriptsDBObjects.ps1
  • 将此PowerShell保存在计算机上
  • 然后在PowerGUI的Powershell consol C:\ScriptsDBObjects.ps1 ServerName
  • 上运行

enter image description here