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

时间:2012-06-05 09:20:58

标签: sql sql-server powershell powershell-module

我有一个。sql文件。我试图通过Powershell脚本传递连接字符串详细信息并调用.sql文件。

我正在搜索并想出了一个与Invoke-sqlcmd相关的cmdlet。当我试图找到与SQL相对应的模块时,我在我的机器中找不到任何模块。

我应该在我的机器上安装任何东西(机器已经安装了SQL Server Management Studio 2008)来获取模块,还是有简单的方法使用Powershell执行.sql文件?

6 个答案:

答案 0 :(得分:89)

尝试查看是否存在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.

答案 1 :(得分:41)

在MSDN上从Import the SQLPS Module引用,

  

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

所以,是的,你可以使用Christian详细介绍的Add-PSSnapin方法,但是欣赏推荐的sqlps模块方法也很有用。

最简单的情况假设您拥有SQL Server 2012: sqlps 包含在安装中,因此您只需通过{{1}加载模块就像任何其他模块一样(通常在您的profile中) }。您可以使用Import-Module sqlps检查模块是否在系统上可用。

如果您没有SQL Server 2012,那么您只需将 sqlps 模块下载到您的modules目录中,这样Get-Module / Import-Module就能找到它。奇怪的是,微软使这个模块可供下载!然而,Chad Miller亲切地打包了必要的部分并提供了this module download。将其解压缩到您的... Documents \ WindowsPowerShell \ Modules目录下,然后继续导入。

值得注意的是,模块方法和snapin方法并不相同。如果您加载了快照,则运行Get-Module -ListAvailable不带 -Registered参数,仅显示已加载的内容),您将看到SQL管理单元。另一方面,如果加载sqlps模块Get-PSSnapin将不会显示加载的snapins,那么仅通过检查snapins来测试Get-PSSnapin cmdlet的各种博客条目可能会给出错误的否定结果

2012.10.06更新

有关sqlps模块与sqlps mini-shell与SQL Server管理单元的完整故事,请查看我最近在Simple-Talk.com上发布的两部分迷你系列Practical PowerShell for SQL Server Developers and DBAs根据一位读者的评论,我所拥有的地方成功地“解除了混淆”这个问题。 : - )

答案 2 :(得分:5)

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

答案 3 :(得分:4)

这是我在PowerShell配置文件中用于加载SQL snapins的函数:

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
    }
}

答案 4 :(得分:0)

使用2008 Server 2008和2008 R2

Add-PSSnapin -Name SqlServerCmdletSnapin100, SqlServerProviderSnapin100

2012年和2014年

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

答案 5 :(得分:0)

这是一种用于简单脚本的轻量级方法,不需要额外的工具/设置/PowerShell 附加组件。

hibernate.search.default.directory_provider = filesystem
hibernate.search.default.indexBase = /var/lucene/indexes
hibernate.search.default.reader.strategy = async
hibernate.search.default.reader.async_refresh_period_ms = 8000
相关问题