How to execute .sql file using powershell?

后端 未结 5 539
一生所求
一生所求 2020-11-28 04:22

I have a .sql file. I am trying to pass connection string details through a Powershell script and invoke an .sql file.

I was searching and

5条回答
  •  一整个雨季
    2020-11-28 05:22

    Here is a function that I have in my PowerShell profile for loading 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
        }
    }
    

提交回复
热议问题