【问题标题】:Using sql query on ssis server sql job using powershell使用 powershell 在 ssis 服务器 sql 作业上使用 sql 查询
【发布时间】:2021-09-13 17:08:16
【问题描述】:

我想使用 powershell 脚本在 ssis 服务器中的 sql 作业上调用 sql 查询/SP,但我不知道该怎么做。我已经使用 powershell 在 sql 数据库上运行 sql 查询,但没有在 ssis 上运行作业。这是我到目前为止所拥有的-

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

查询可能是获取作业的详细信息或对其执行某些操作。这也足够了,因为我们只是在此处提供服务器名称$sqlJob = $ssisServer.JobServer.Jobs[$existing_job_name] 以获取特定文件夹-> 项目-> 工作中的工作?我还不能尝试这个,也没有找到太多的资源。请给我一些帮助。

【问题讨论】:

  • 不清楚你在找我什么。您问题中的示例是否已经为您工作,并且您想添加对工作数据的查询?有几种方法可以获取作业对象,但它可能取决于您正在执行的身份验证类型等,因此最好从一些工作代码开始。尝试在您的示例中运行代码并添加有关您的作业返回的数据的更多信息。
  • 对于您的第二段,使用像$sqlJob = Get-ChildItem SQLSERVER:\SSIS\localhost\Default\Catalogs\SSISDB\Folders\MyFolder\Projects\MyProject\Packages\ | Where Name -eq 'MyPackage.dtsx' 这样的 sql server 提供程序可以更轻松地获得特定工作。

标签: sql powershell ssis sql-job


【解决方案1】:

我没有使用 SSIS 服务器。但是从 SQL 运行 SQL 查询的原理非常简单。

你应该有proper module或者你可以使用snapin of SQL

我会从第二个继续。

# 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"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-07
    • 1970-01-01
    • 2015-04-21
    • 1970-01-01
    • 1970-01-01
    • 2020-02-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多