![](/img/trans.png)
[英]Send mail task is not working in SSIS when SSIS package is called through windows task scheduler
[英]SSIS Package fails when called from task scheduler via a batch file because of a script task
我有一個從批處理文件中調用的 SSIS 包,我正在嘗試通過任務調度程序對其進行調度。 該包在 Visual Studio 中運行良好,當我執行批處理文件時它可以工作,但是當我通過調度程序運行包時它會失敗。 我已經閱讀了關於這個主題的所有其他帖子,但我沒有看到任何與我相關的內容,問題不在於任務調度程序屬性的配置(即它使用的帳戶、以最高權限運行、在目錄中啟動等)。 )。
我通過任務調度程序成功運行了多個包,沒有任何問題,這個只是碰巧使用了我必須添加程序集引用的 ac# 腳本任務,我認為這就是包通過調度程序運行時導致問題的原因其他包使用 c# 腳本任務沒有問題,但我沒有添加任何程序集。
這是 C# 腳本,用於在填充數據后格式化 Excel 電子表格。
using System;
using System.Runtime.InteropServices;
using Excel = Microsoft.Office.Interop.Excel;
#endregion
namespace ST_2bdf93d5542441248076f053703d32c9
{
[Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]
public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{
public void Main()
{
int lastUsedColumn = 0;
string inputFile = (string)Dts.Variables["RecommendationFileName"].Value;
string RecommendationName = (string)Dts.Variables["RecommendationName"].Value;
Excel.Application ExcelApp = new Excel.Application();
Excel.Workbook ExcelWorkbook = ExcelApp.Workbooks.Open(inputFile);
//ExcelApp.Visible = true; //Use this to show the excel application/spreadsheet while the package is running. Not good for prod, just testing.
ExcelApp.Visible = false;
Excel.Worksheet xlWorkSheetFocus = (Excel.Worksheet)ExcelWorkbook.Worksheets.get_Item(3);
xlWorkSheetFocus.Activate();
xlWorkSheetFocus.Select(Type.Missing);
Excel.Range usedRange = xlWorkSheetFocus.UsedRange;
foreach (Excel.Worksheet ExcelWorksheet in ExcelWorkbook.Sheets)
{
ExcelWorksheet.Columns.AutoFit(); //Autofit the column to width for each worksheet, we adjust some column widths manually later.
if (ExcelWorksheet.Name == "Recommendations")
{
ExcelWorksheet.Cells[1, 4].EntireColumn.ColumnWidth = 125;
ExcelWorksheet.Cells[1, 4].EntireColumn.WrapText = true;
}
if (ExcelWorksheet.Name == "Passed")
{
ExcelWorksheet.Cells[1, 4].EntireColumn.ColumnWidth = 125;
ExcelWorksheet.Cells[1, 4].EntireColumn.WrapText = true;
}
if ((ExcelWorksheet.Name != "Recommendations") & (ExcelWorksheet.Name != "Passed"))
{
// Find the last real column in each worksheet
lastUsedColumn = ExcelWorksheet.Cells.Find("*", System.Reflection.Missing.Value,
System.Reflection.Missing.Value, System.Reflection.Missing.Value,
Excel.XlSearchOrder.xlByColumns, Excel.XlSearchDirection.xlPrevious,
false, System.Reflection.Missing.Value, System.Reflection.Missing.Value).Column;
ExcelWorksheet.Rows["1"].Insert(); //insert empty top row
ExcelWorksheet.Rows["2"].Insert(); //insert empty second row
ExcelWorksheet.Rows["3"].Insert(); //insert empty second row
ExcelWorksheet.Cells[1, 1].Interior.Color = 0x565656; //Row 1 = Dark Gray
ExcelWorksheet.Cells[2, 1].Interior.Color = 0x565656; //Row 2 = Dark Gray
ExcelWorksheet.Cells[3, 1].Interior.Color = 0x3ad7bd; //Row 3 = Green
ExcelWorksheet.Range[ExcelWorksheet.Cells[4, 1], ExcelWorksheet.Cells[4, lastUsedColumn]].Interior.Color = 0xCECECE; //Row 4 = Light Gray
//Bold the Fourth row of each spreadsheet (column headers are here)
ExcelWorksheet.Range["A4"].EntireRow.Font.Bold = true;
//Add a link back to the Recommendations page in row 2
ExcelWorksheet.Hyperlinks.Add(ExcelWorksheet.Cells[2, 1], "#Recommendations!A2", Type.Missing, "Return to Recommendations", "Return to Recommendations");
//Change row 1 to White, Bold, and 12pt font Arial, this is the report Title
ExcelWorksheet.Cells[1, 1].Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.White);
ExcelWorksheet.Cells[1, 1].Font.Bold = true;
ExcelWorksheet.Cells[1, 1].Font.size = 12;
ExcelWorksheet.Cells[1, 1].Font.Name = "Arial";
Excel.Range formatRange;
formatRange = ExcelWorksheet.get_Range("c1", "c1");
}
}
ExcelWorkbook.Save();
GC.Collect();
GC.WaitForPendingFinalizers();
ExcelWorkbook.Close(Type.Missing, Type.Missing, Type.Missing);
Marshal.FinalReleaseComObject(ExcelWorkbook);
ExcelApp.Quit();
Marshal.FinalReleaseComObject(ExcelApp);
}
enum ScriptResults
{
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
};
}
}
以下是我添加到此腳本任務的參考:
我的問題是,知道它與這些引用有關,有誰明白為什么會發生這種情況? 我正在使用本地管理員帳戶運行任務,並且批處理文件位於本地文件系統上,在使用任務調度程序時,包中的所有其他內容都可以工作,直到此腳本任務。 我嘗試將 Excel 互操作 DLL 文件復制到與批處理文件相同的文件夾中,並重新添加了引用以查看這是否可能是無濟於事的問題。 我的另一個腳本任務,我不必添加任何程序集引用即可以這種方式正常工作。
叮叮叮_ _
我必須添加一個程序集引用,我認為這就是導致問題的原因
正確的。 您正在通過 Microsoft.Office.Interop.Excel 使用 Excel 對象模型來構建/修改 Excel 工作簿。 調度程序服務器未安裝 Office,因此包因找不到所需的庫而失敗。 正確的解決方法是在服務器上安裝 Office。
我嘗試(原文如此)將 Excel 互操作 DLL 文件復制到與批處理文件相同的文件夾中
您不想通過將所需程序集復制到調度程序來“解決”問題。 即使您安裝了所有必需的文件,您現在已經讓您的公司面臨審核失敗的風險。
Office 不是免費的,Redmond 的優秀人員建造了它,您的組織會願意為此付費,因為預付費用比審計發現故意違規要便宜得多。 比較和對比這些對話
“哦,是的,我們在這個盒子上安裝了 XYZ 卻忘了它” 審計員:好的,好的,驗證你的許可並為你使用的東西付費。 $
“哦,是的,我們在那邊的庫上進行了鏡像,將它們安裝到 GAC 等” 審計員:所以這不僅僅是一個意外,這是故意的,無知不是一種防御。 您欠我們的許可費和以下罰款。 $$$
我開始意識到 Interop 不會通過代理或任務調度程序無頭工作,所以我切換到 ClosedXML,構建了一個控制台應用程序,並以這種方式執行它並且它可以工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.