Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Call powershell in SQL agent job


Call powershell in SQL agent job

Author
Message
sqlfriends
sqlfriends
SSCrazy
SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)

Group: General Forum Members
Points: 2045 Visits: 3859
I use SQL server agent job to call a powershell script. I am using Type operating system(Cmdexec)

In the job command window, I type:

powershell.exe "D:\PowershellScripts\Write-VolToDb.ps1 'MySQLserver\v2012' dba"
OR
powershell.exe "& D:\PowershellScripts\Write-VolToDb.ps1 MySQLserver\v2012 dba"

It is not working. The scipt self is OK, but I just cannot figure out what is the correct syntax to call it.

The 'MySQLserver\v2012' is my server name\instanceName, dba is the database name. They are two parameter of the powershell.

What is correct syntax of it,

Thanks


--------------------------------------------------------------------------------
Seraj Alam-256815
Seraj Alam-256815
SSC-Enthusiastic
SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)

Group: General Forum Members
Points: 186 Visits: 373
What is error message? I assume you considered the agent account and it's access on the target sql server.
sqlfriends
sqlfriends
SSCrazy
SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)

Group: General Forum Members
Points: 2045 Visits: 3859
Sorry, it works now.
I found out the path for the file is wrong.

Thanks much
Grant Fritchey
Grant Fritchey
SSCoach
SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)

Group: General Forum Members
Points: 17586 Visits: 32263
Glad to hear it's fixed.

Just curious though, why not use a PowerShell type of step in SQL Agent for a PowerShell script?

----------------------------------------------------
The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood...
Theodore Roosevelt

The Scary DBA
Author of: SQL Server Query Performance Tuning and SQL Server Execution Plans
Product Evangelist for Red Gate Software
Orlando Colamatteo
Orlando Colamatteo
SSCrazy Eights
SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)

Group: General Forum Members
Points: 8239 Visits: 14368
Grant Fritchey (4/1/2013)
Glad to hear it's fixed.

Just curious though, why not use a PowerShell type of step in SQL Agent for a PowerShell script?

Depending on what your script does sometimes you just can't. The SQL Agent PowerShell Step Type in 2008 R2 executes the code it is handed within a closed shell named sqlps.exe (referred to as mini-shell all over the net). There are tons of limitations but the most prominent one I could think of is that the PowerShell Step Type does not implement a default output host meaning that any CmdLet that uses Write-Host, analog to T-SQL PRINT, will throw an error like this:

Cannot invoke this function because the current host does not implement it.

Some of the in-built CmdLets (e.g. Remove-Item iirc) assume there will be an output host and in some cases will try to output information messages so it is simply not safe to use those in a PowerShell Step Type.

I would need to verify but in SQL 2012 I think PowerShell Steps were changed to use a full-blown PowerShell v2 shell, i.e. no more of the limitations imposed by 2008 R2's use of sqlps.exe.

This article explains that in 2008 R2 developing sqlps.exe solved some implementation concerns for the SQL Server team, namely locking us into a closed shell would allow them to lock down user scenarios and avoid problems with third-party snap-ins. This was great for the SQL Team in terms of being able to deliver a stable product but it created many other limitations for people like myself that like to forego T-SQL and use PowerShell for tasks that need to interact with more than just a single database engine, e.g. syncing the Server Logins from a primary instance to a DR-instance, and run them from a SQL Agent Job step.

In 2008 R2 I end up doing the same as the OP, namely executing my ps1 scripts using powershell.exe in a CmdExec step type. Once I get more into figuring out how the changes in SQL 2012 have changed the UX I may change my approach.

__________________________________________________________________________________________________
There are no special teachers of virtue, because virtue is taught by the whole community. --Plato
sqlfriends
sqlfriends
SSCrazy
SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)

Group: General Forum Members
Points: 2045 Visits: 3859
Grant Fritchey (4/1/2013)
Glad to hear it's fixed.

Just curious though, why not use a PowerShell type of step in SQL Agent for a PowerShell script?


I found it runs much faster using cmdexe type vs powshell type to run the jobs.
I read an article telling what is the difference , it seems they are using different powershell module to run the job. I cannot find the article now by doing some search.
For my cases, I can use both, but it takes much shorter time for using cmdexec to trigger the job and finish the job.
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45066 Visits: 39904
sqlfriends (4/1/2013)
Grant Fritchey (4/1/2013)
Glad to hear it's fixed.

Just curious though, why not use a PowerShell type of step in SQL Agent for a PowerShell script?


I found it runs much faster using cmdexe type vs powshell type to run the jobs.
I read an article telling what is the difference , it seems they are using different powershell module to run the job. I cannot find the article now by doing some search.
For my cases, I can use both, but it takes much shorter time for using cmdexec to trigger the job and finish the job.


:-)

Shifting gears, do you have any auditing in place to audit your PowerShell runs at the OS Level? Has nothing to do with your current problem. I'm just curious. Thanks for any feedback on this.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
jobudzin
jobudzin
Grasshopper
Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)

Group: General Forum Members
Points: 17 Visits: 139
I have a similar problem. I am attempting to assign a value to a variable and then execute the powershell script using that variable. The SQL agent job step is CmdExec and it executes or completes successfully but does not update the underlying table like it does in using the powershell window alone. Here is the code from the job step:

powershell.exe "$dt = D:\PSscripts\Invoke-Sqlcmd2.ps1 -ServerInstance 'JAXSQLMON01' -Database DBACentral -Query 'Select server_name from server_instance' | foreach-object {D:\PSscripts\Invoke-Sqlcmd2.ps1 -ServerInstance $_.server_name -Database master -InputFile ./D:\PSscripts\get-dbspace.sql -As 'DataRow'}"

powershell.exe "D:\PSscripts\Write-DataTable.ps1 -ServerInstance 'JAXSQLMON01' -Database 'DBACentral' -TableName 'db_Space' -Data $dt"


Any help would be greatly appreciated. When I run this using regular powershell window it creates the variable and then populates the table with the file sizes which I am trying to collect.
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45066 Visits: 39904
Jeff Moden (4/1/2013)
sqlfriends (4/1/2013)
Grant Fritchey (4/1/2013)
Glad to hear it's fixed.

Just curious though, why not use a PowerShell type of step in SQL Agent for a PowerShell script?


I found it runs much faster using cmdexe type vs powshell type to run the jobs.
I read an article telling what is the difference , it seems they are using different powershell module to run the job. I cannot find the article now by doing some search.
For my cases, I can use both, but it takes much shorter time for using cmdexec to trigger the job and finish the job.


:-)

Shifting gears, do you have any auditing in place to audit your PowerShell runs at the OS Level? Has nothing to do with your current problem. I'm just curious. Thanks for any feedback on this.


I guess I'll have to take the silence as a "No". Thanks anyway.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
sqlfriends
sqlfriends
SSCrazy
SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)

Group: General Forum Members
Points: 2045 Visits: 3859
No, we don't. Sorry for the late response.
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