SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Powershell script being run as SQL agent job


Powershell script being run as SQL agent job

Author
Message
MickyD
MickyD
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1133 Visits: 1527
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
Elliott Whitlow
Elliott Whitlow
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24828 Visits: 5314
I can't answer the question you asked, sorry, but I would have done this with SSIS instead.. YMMV

CEWII
Orlando Colamatteo
Orlando Colamatteo
SSC-Forever
SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)

Group: General Forum Members
Points: 41154 Visits: 14413
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
MickyD
MickyD
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1133 Visits: 1527
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.
ALZDBA
ALZDBA
SSC-Dedicated
SSC-Dedicated (30K reputation)SSC-Dedicated (30K reputation)SSC-Dedicated (30K reputation)SSC-Dedicated (30K reputation)SSC-Dedicated (30K reputation)SSC-Dedicated (30K reputation)SSC-Dedicated (30K reputation)SSC-Dedicated (30K reputation)

Group: General Forum Members
Points: 30743 Visits: 8986
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


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


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


press F1 for solution, press shift+F1 for urgent solution :-D


Need a bit of Powershell? How about this

Who am I ? Sometimes this is me Alien but most of the time this is me Hehe
MickyD
MickyD
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1133 Visits: 1527
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
MickyD
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1133 Visits: 1527
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 ?


Orlando Colamatteo
Orlando Colamatteo
SSC-Forever
SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)

Group: General Forum Members
Points: 41154 Visits: 14413
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
MickyD
MickyD
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1133 Visits: 1527
thanks , I will check and report back
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search