简体   繁体   English

使用powershell在ssis服务器sql作业上使用sql查询

[英]Using sql query on ssis server sql job using powershell

I want to invoke a sql query/SP on sql job in a ssis server using a powershell script but I don't know how to do that.我想使用 powershell 脚本在 ssis 服务器中的 sql 作业上调用 sql 查询/SP,但我不知道该怎么做。 I've used powershell to run sql queries on sql database but not jobs on ssis.我已经使用 powershell 在 sql 数据库上运行 sql 查询,但没有在 ssis 上运行作业。 Here is what I have till now-这是我到目前为止所拥有的-

$ssisServer = New-Object -TypeName  Microsoft.SQLServer.Management.Smo.Server($name_of_server) 
$IntegrationServices = New-Object "Microsoft.SqlServer.Management.IntegrationServices.IntegrationServices" $ssisServer
$catalog = $IntegrationServices.Catalogs[$catalog]
$folder = $catalog.Folders[$folder]    
$project = $folder.Projects[$project]
$sqlJob = $ssisServer.JobServer.Jobs[$existing_job_name]
query= " some sql query "
if ($sqlJob) {
$sqlJob.CurrentRunStatus()
   # here I need to run a query on the job
}

The query could be to get details of the job or perform some action on it.查询可能是获取作业的详细信息或对其执行某些操作。 Also is this enough as we are just giving the server name here $sqlJob = $ssisServer.JobServer.Jobs[$existing_job_name] to get the job which in a particular folder->project->job?这也足够了,因为我们只是在此处提供服务器名称$sqlJob = $ssisServer.JobServer.Jobs[$existing_job_name]以获取特定文件夹->项目->作业中的作业? I couldn't try this yet and haven't found much resources on it.我还不能尝试这个,也没有找到很多资源。 Please give me some help to work with.请给我一些帮助。

I didn't work with the SSIS server.我没有使用 SSIS 服务器。 But the principle of running SQL queries from SQL is pretty simple.但是从 SQL 运行 SQL 查询的原理非常简单。

You should have proper module or you can use snapin of SQL .您应该有适当的模块,或者您可以使用SQL 的管理单元

I will continue from the second one.我将从第二个开始。

# If you have an SQL server on your server, so this is the possible path where they can be.
$PossiblePaths = 'C:\Program Files\Microsoft SQL Server','C:\Program Files (x86)\Microsoft SQL Server'

# Lets check where we have PSProvider.dll and PSSnapins.dll
$PossiblePaths | ForEach-Object {
    Test-Path -Path $_
    {
        $SQLPSProvider = (Get-ChildItem -Filter "Microsoft.SqlServer.Management.PSProvider.dll" -Path $_ -Recurse).FullName
        $SQLPSSnapIn = (Get-ChildItem -Filter "Microsoft.SqlServer.Management.PSSnapins.dll" -Path $_ -Recurse).FullName
    }
}

# Lets find Install Utility to add them with it.
$InstallUtil = (Get-ChildItem -Filter "InstallUtil.exe" -Path "$env:windir\Microsoft.NET\Framework64\v2.0*" -Recurse).FullName

if (($null -eq $SQLPSProvider) -or ($null -eq $SQLPSSnapIn))
{
    Write-Host "Sorry, SQL PowerShell SnapIn or PowerShell Provider not found." -ForegroundColor Red
}
else
{
    # Adding them to our system.
    Start-Process -FilePath $InstallUtil -ArgumentList "-i $SQLPSProvider"
    Start-Process -FilePath $InstallUtil -ArgumentList "-i $SQLPSSnapIn"
}

# Now they should be in the system and we can add them to our PowerShell session.
Add-PSSnapin -Name SqlServerCmdletSnapin100
Add-PSSnapin -Name SqlServerProviderSnapin100

# Now we should have Invoke-Sqlcmd like if we had SQLServer module.
$SQLServer = "SQL2012"
$SQLInstance = "JustForExample"
$ServerInstance = $SQLServer + '\' + $SQLInstance

# So you typing query for example like attaching DB.
$Query = "CREATE DATABASE ExamleDB ON (FILENAME = `'C:\DBs\ExamleDB.mdf`'), (FILENAME = `'C:\DBs\ExamleDB_log.ldf`') FOR ATTACH"

# And then executing it with Invoke-Sqlcmd like that.
Invoke-Sqlcmd -ServerInstance $ServerInstance -Username 'sa' -Password 'Abcde12345' -Query "Query"

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

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