繁体   English   中英

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

[英]I am trying to run this powershell Script using PowerGUI

# 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”自变量的异常调用“脚本”:“服务器'corpbillingdata'的脚本失败。“在Travis \\ scriptdatabase1 \\ scriptdatabase1.ps1:77 C:\\ Users \\ jlauf \\ Documents \\ SQL Helpers中char:16 + $ scrp.script <<<<($($ db.Tables))+ CategoryInfo:未指定:(:) [],MethodInvocationException + FullyQualifiedErrorId:DotNetMethodException

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

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM