如何使用PowerShell来执行.sql文件?如何使用PowerShell来执行.sql文件?(H

2019-05-17 07:53发布

我有一个 。 sql文件。 我试图通过PowerShell脚本来传递连接字符串的细节和调用.sql文件。

我正在寻找,并与相关cmdlet的上前Invoke-sqlcmd 。 当我试图找到对应的SQL模块,我没有找到我的计算机中的任何一个。

我应该在我的机器上安装任何东西(机器已经有了SQL Server Management Studio中2008 R2),以获得模块或有没有简单的方法来执行.sql使用PowerShell文件?

Answer 1:

尝试看看是否SQL管理单元都存在:

get-pssnapin -Registered

Name        : SqlServerCmdletSnapin100
PSVersion   : 2.0
Description : This is a PowerShell snap-in that includes various SQL Server cmdlets.

Name        : SqlServerProviderSnapin100
PSVersion   : 2.0
Description : SQL Server Provider

如果是这样

Add-PSSnapin SqlServerCmdletSnapin100 # here lives Invoke-SqlCmd
Add-PSSnapin SqlServerProviderSnapin100

那么你可以这样做:

invoke-sqlcmd -inputfile "c:\mysqlfile.sql" -serverinstance "servername\serverinstance" -database "mydatabase" # the parameter -database can be omitted based on what your sql script does.


Answer 2:

从引用导入SQLPS模块 MSDN上,

从PowerShell的管理SQL Server推荐的方法是将SQLPS模块导入到Windows PowerShell 2.0环境。

所以,是的,你可以使用Add-PSSnapin由基督教详细的方法,但它也很有欣赏推荐SQLPS模块的方法。

最简单的情况下,假设你有SQL Server 2012中:SQLPS包括在安装,所以你只需加载模块像任何其他(在通常的个人资料通过) Import-Module sqlps 。 您可以检查模块是你的系统与Get-Module -ListAvailable

如果您没有SQL Server 2012中,那么所有你需要做的就是下载SQLPS模块到你的模块目录中,以便获取-模块/导入模块会找到它。 奇怪的是,微软并没有使该模块提供下载! 然而,乍得米勒慷慨打包必要的片,提供该模块的下载 。 将它解压缩在你的...文档\ WindowsPowerShell \ Modules目录,并与进口继续。

有趣的是,要注意的是模块的方法和管理单元的方式并不相同。 如果您加载snapins然后运行Get-PSSnapin不含 -注册参数,只显示已加载的),你会看到SQL snapins。 如果,另一方面,您加载SQLPS模块Get-PSSnapin将不会显示加载snapins,使测试的各种博客条目Invoke-Sqlcmd ,仅通过snapins小命令可以让假阴性结果。

2012年10月6日更新

有关SQLPS模块主场迎战SQLPS迷你外壳与SQL Server的管理单元的完整的故事,看看我的两个部分组成的迷你系列为SQL Server开发人员和DBA实用的PowerShell最近发表了Simple-Talk.com在那里我有,根据一个读者的评论,成功地将“反混淆”的问题。 :-)



Answer 3:

if(Test-Path "C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS") { #Sql Server 2012
    Import-Module SqlPs -DisableNameChecking
    C: # Switch back from SqlServer
} else { #Sql Server 2008
    Add-PSSnapin SqlServerCmdletSnapin100 # here live Invoke-SqlCmd
}

Invoke-Sqlcmd -InputFile "MySqlScript.sql" -ServerInstance "Database name" -ErrorAction 'Stop' -Verbose -QueryTimeout 1800 # 30min


Answer 4:

下面是我在装载SQL snapins我PowerShell配置文件中的函数:

function Load-SQL-Server-Snap-Ins
{
    try 
    {
        $sqlpsreg="HKLM:\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.SqlServer.Management.PowerShell.sqlps"

        if (!(Test-Path $sqlpsreg -ErrorAction "SilentlyContinue"))
        {
            throw "SQL Server Powershell is not installed yet (part of SQLServer installation)."
        }

        $item = Get-ItemProperty $sqlpsreg
        $sqlpsPath = [System.IO.Path]::GetDirectoryName($item.Path)

        $assemblyList = @(
            "Microsoft.SqlServer.Smo",
            "Microsoft.SqlServer.SmoExtended",
            "Microsoft.SqlServer.Dmf",
            "Microsoft.SqlServer.WmiEnum",
            "Microsoft.SqlServer.SqlWmiManagement",
            "Microsoft.SqlServer.ConnectionInfo ",
            "Microsoft.SqlServer.Management.RegisteredServers",
            "Microsoft.SqlServer.Management.Sdk.Sfc",
            "Microsoft.SqlServer.SqlEnum",
            "Microsoft.SqlServer.RegSvrEnum",
            "Microsoft.SqlServer.ServiceBrokerEnum",
            "Microsoft.SqlServer.ConnectionInfoExtended",
            "Microsoft.SqlServer.Management.Collector",
            "Microsoft.SqlServer.Management.CollectorEnum"
        )

        foreach ($assembly in $assemblyList)
        { 
            $assembly = [System.Reflection.Assembly]::LoadWithPartialName($assembly) 
            if ($assembly -eq $null)
                { Write-Host "`t`t($MyInvocation.InvocationName): Could not load $assembly" }
        }

        Set-Variable -scope Global -name SqlServerMaximumChildItems -Value 0
        Set-Variable -scope Global -name SqlServerConnectionTimeout -Value 30
        Set-Variable -scope Global -name SqlServerIncludeSystemObjects -Value $false
        Set-Variable -scope Global -name SqlServerMaximumTabCompletion -Value 1000

        Push-Location

         if ((Get-PSSnapin -Name SqlServerProviderSnapin100 -ErrorAction SilentlyContinue) -eq $null) 
        { 
            cd $sqlpsPath

            Add-PsSnapin SqlServerProviderSnapin100 -ErrorAction Stop
            Add-PsSnapin SqlServerCmdletSnapin100 -ErrorAction Stop
            Update-TypeData -PrependPath SQLProvider.Types.ps1xml
            Update-FormatData -PrependPath SQLProvider.Format.ps1xml
        }
    } 

    catch 
    {
        Write-Host "`t`t$($MyInvocation.InvocationName): $_" 
    }

    finally
    {
        Pop-Location
    }
}


Answer 5:

与2008年Server 2008和2008 R2

Add-PSSnapin -Name SqlServerCmdletSnapin100, SqlServerProviderSnapin100

与2012年和2014年

Push-Location
Import-Module -Name SQLPS -DisableNameChecking
Pop-Location


文章来源: How to execute .sql file using powershell?