Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Powershell script being run as SQL agent job Expand / Collapse
Author
Message
Posted Wednesday, July 10, 2013 11:53 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: 2 days ago @ 1:11 PM
Points: 229, Visits: 1,163
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
Post #1472276
Posted Wednesday, July 10, 2013 12:22 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Thursday, October 9, 2014 1:02 PM
Points: 6,032, Visits: 5,284
I can't answer the question you asked, sorry, but I would have done this with SSIS instead.. YMMV

CEWII
Post #1472292
Posted Wednesday, July 10, 2013 7:55 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: 2 days ago @ 7:06 PM
Points: 7,125, Visits: 12,720
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
Post #1472410
Posted Wednesday, July 10, 2013 11:13 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: 2 days ago @ 1:11 PM
Points: 229, Visits: 1,163
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.
Post #1472430
Posted Wednesday, July 10, 2013 11:57 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Thursday, October 9, 2014 10:42 AM
Points: 6,731, Visits: 8,480
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


Don't drive faster than your guardian angel can fly ...
but keeping both feet on the ground won't get you anywhere

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


- 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
Post #1472436
Posted Thursday, July 11, 2013 1:49 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: 2 days ago @ 1:11 PM
Points: 229, Visits: 1,163
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 ?

Post #1472459
Posted Thursday, July 11, 2013 1:50 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: 2 days ago @ 1:11 PM
Points: 229, Visits: 1,163
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 ?

Post #1472460
Posted Friday, July 12, 2013 8:27 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: 2 days ago @ 7:06 PM
Points: 7,125, Visits: 12,720
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
Post #1473069
Posted Friday, July 12, 2013 8:41 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: 2 days ago @ 1:11 PM
Points: 229, Visits: 1,163
thanks , I will check and report back
Post #1473075
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse