I've got a strange issue where a sql agent job doesnt really fail but sometimes it runs and other times it does not run which makes it all the more difficult to figure out what is going on.
One of the steps in this job is to call a powershell script, I use the OperatingSystem command as the sql agent job step and do something like powershell.exe -file "\sharedfolder\myscript.ps" to run it.
Running myscript.ps outside of sql agent job always works correctly, running it within sql server agent job works as mentioned sometimes (never erroring but just sitting there waiting sometimes).
The history of the sql agent job step shows the following:
Executed as user: Domain\Administrator. New-Object : Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 8000401a The server process could not be started because the configured identity is incorrect. Check the username and password. (Exception from HRESULT: 0x8000401A). At \myserver\Interfaces\Fingerprint\Tools\DeleteExcelColumns.ps1:1 char:10 + $excel = New-Object -ComObject Excel.Application +
ResourceUnavailable: (:) [New-Object], COMException + FullyQualifiedErrorId : NoCOMClassIdentified,Microsoft.PowerShell.Commands.NewObjectCommandYou cannot call a method on a null-valued expression. At \\myserver\Interfaces\Fingerprint\Tools\DeleteExcelColumns.ps1:8 char:2 + $workbook = $excel.Workbooks.Open( $filesLocation + $file) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull You cannot call a method on a null-valued expression. At \\myserver\Interfaces\Fingerprint\Tools\DeleteExcelColumns.ps1:24 char:3 + $workbook.Close($true) + ~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull You cannot call a method on a null-valued expression. At \\myserver\Interfaces\Fingerprint\Tools\DeleteExcelColumns.ps1:8 char:2 + $workbook = $excel.Workbooks.Open( $filesLocation + $file) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull You cannot call a method on a null-valued expression. At \\myserver\Interfaces\Fingerprint\Tools\DeleteExcelColumns.ps1:24 char:3 + $workbook.Close($true) + ~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull You cannot call a method on a null-valued expression. At \\myserver\Interfaces\Fingerprint\Tools\DeleteExcelColumns.ps1:27 char:1 + $excel.Quit() + ~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNullException calling "ReleaseComObject" with "1" argument(s): "Object reference not set to an instance of an object." At \\myserver\Interfaces\Fingerprint\Tools\DeleteExcelColumns.ps1:28 char:1 + [void][System.Runtime.Interopservices.Marshal]::ReleaseComObject($exc ... +
- CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : NullReferenceException Remove-Variable : Cannot find a variable with the name 'excel'. At \myserver\Interfaces\Fingerprint\Tools\DeleteExcelColumns.ps1:29 char:1 + Remove-Variable excel + ~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo
: ObjectNotFound: (excel:String) [Remove-Variable], ItemNotFoundException + FullyQualifiedErrorId : VariableNotFound,Microsoft.PowerShell.Commands.RemoveVariableCommand. Process Exit Code 0. The step succeeded.
The powershell script simply opens up excel files and deletes certain columns:
$excel = New-Object -ComObject Excel.Application
$filesLocation = "\\myserver\Interfaces\Fingerprint\Inbound\"
$files = Get-ChildItem -af $filesLocation
$ColumnsToKeep = 14,61,62,63
ForEach ($file in $files)
{
$workbook = $excel.Workbooks.Open( $filesLocation + $file)
try {
$sheet = $workbook.Sheets.Item(1)
$maxColumns = $sheet.UsedRange.Columns.Count
$ColumnsToRemove = Compare-Object $ColumnsToKeep (1..$maxColumns) | Where-Object{$_.SideIndicator -eq "=>"} | Select-Object -ExpandProperty InputObject
0..($ColumnsToRemove.Count - 1) | %{$ColumnsToRemove[$_] = $ColumnsToRemove[$_] - $_}
$ColumnsToRemove | ForEach-Object{
[void]$sheet.Cells.Item(1,$_).EntireColumn.Delete()}
#save the file
$workbook.Save()
}
catch {}
finally{
$workbook.Close($true)
}
}
$excel.Quit()
[void][System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel)
Remove-Variable excel
The powershell script is on the same server as where our sql instance sits.
User contributions licensed under CC BY-SA 3.0