Powershell script being run as SQL agent job

  • Hi everyone.

    I have a powershell script that lists all backup status across various servers.

    Script runs fine via Powershell gui (Normally)

    Sometimes I see an issue. Think it is regarding Excel and how it outputs its results.

    I can see that when the spreadsheet is created, it leaves the excel.exe*32 running on my machine.

    Unless I close that , or go into spreadsheet and exit it, subsequent runs also fail.

    When not there , runs fine.

    However when run as SQL agent job it always fails.

    this is the error.

    A job step received an error at line 88 in a PowerShell script.

    The corresponding line is '$Excel.SaveAs("Y:\SQL\Failedjobs.xlsx")'.

    Correct the script and reschedule the job.

    The error information returned by PowerShell is:

    'Exception calling "SaveAs" with "1" argument(s): "SaveAs method of Workbook class failed"

    Any tips on how to correct this , or what I am doing wrong and need to check ?

    many thanks

  • I can't answer the question you asked, sorry, but I would have done this with SSIS instead.. YMMV

    CEWII

  • Is the server 64-bit? Does the server have the Excel Object Model installed either via Office or VSTO? How are you kicking off the script, in a PowerShell step-type or using powershell.exe in a CmdExec step-type?

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato

  • opc.three (7/10/2013)


    Is the server 64-bit? Does the server have the Excel Object Model installed either via Office or VSTO? How are you kicking off the script, in a PowerShell step-type or using powershell.exe in a CmdExec step-type?

    HI , the machine is 64 bit and assume has Excel object model installed as it runs from powershell gui fine. As long as Excel.exe is still not running.

    Trying to Kick it off via a SQL job. Never worked so far , but script does work in gui as long as above is met.

  • with regards to your Powershell script:

    do you actually use an xlsx template to fill out with your data?

    do you actually use any excel functionallity at all ?

    if not: just change the powershell to save your data as CSV !

    pro:

    - you don't need to have office parts installed on your sever

    - opened by a client that has excel installed, the csv file will be opened by it.

    con:

    - when opened by excel, it will not format the columns by default ( meaning column width, color, font,...)

    use e.g.

    # export data to csv sorted by ServerName, DbName, Name in ascending order

    $drAllRslt | Sort-Object ServerName, DbName, Name | export-csv -path "D:\SQLAgentResults\Reslut123.csv" -NoTypeInformation

    Johan

    Learn to play, play to learn !

    Dont drive faster than your guardian angel can fly ...
    but keeping both feet on the ground wont get you anywhere :w00t:

    - How to post Performance Problems
    - How to post data/code to get the best help[/url]

    - How to prevent a sore throat after hours of presenting ppt

    press F1 for solution, press shift+F1 for urgent solution 😀

    Need a bit of Powershell? How about this

    Who am I ? Sometimes this is me but most of the time this is me

  • ALZDBA (7/10/2013)


    with regards to your Powershell script:

    do you actually use an xlsx template to fill out with your data?

    do you actually use any excel functionallity at all ?

    if not: just change the powershell to save your data as CSV !

    pro:

    - you don't need to have office parts installed on your sever

    - opened by a client that has excel installed, the csv file will be opened by it.

    con:

    - when opened by excel, it will not format the columns by default ( meaning column width, color, font,...)

    use e.g.

    # export data to csv sorted by ServerName, DbName, Name in ascending order

    $drAllRslt | Sort-Object ServerName, DbName, Name | export-csv -path "D:\SQLAgentResults\Reslut123.csv" -NoTypeInformation

    Johan

    Thanks for that. I am still trying to learn powershell. Could I post the code I am using and then could you show me where I need to change it in order for it work with your suggested changes ?

  • ALZDBA (7/10/2013)


    with regards to your Powershell script:

    do you actually use an xlsx template to fill out with your data?

    do you actually use any excel functionallity at all ?

    if not: just change the powershell to save your data as CSV !

    pro:

    - you don't need to have office parts installed on your sever

    - opened by a client that has excel installed, the csv file will be opened by it.

    con:

    - when opened by excel, it will not format the columns by default ( meaning column width, color, font,...)

    use e.g.

    # export data to csv sorted by ServerName, DbName, Name in ascending order

    $drAllRslt | Sort-Object ServerName, DbName, Name | export-csv -path "D:\SQLAgentResults\Reslut123.csv" -NoTypeInformation

    Johan

    Thanks for that. I am still trying to learn powershell. Could I post the code I am using and then could you show me where I need to change it in order for it work with your suggested changes ?

  • MickyD (7/10/2013)


    opc.three (7/10/2013)


    Is the server 64-bit? Does the server have the Excel Object Model installed either via Office or VSTO? How are you kicking off the script, in a PowerShell step-type or using powershell.exe in a CmdExec step-type?

    HI , the machine is 64 bit and assume has Excel object model installed as it runs from powershell gui fine. As long as Excel.exe is still not running.

    Trying to Kick it off via a SQL job. Never worked so far , but script does work in gui as long as above is met.

    Is the PowerShell GUI a 64-bit app? It might be a 32-bit app in which case it would be accessing 32-bit Excel objects. Whereas when running through SQL Agent it is running as a 64-bit process. Bit-ness differences are a very common reason why apps that use Office objects work great when debugging and fail when run on a server.

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato

  • thanks , I will check and report back

Viewing 9 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic. Login to reply