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 12»»

Call powershell in SQL agent job Expand / Collapse
Author
Message
Posted Friday, March 29, 2013 10:04 AM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Yesterday @ 11:07 AM
Points: 1,783, Visits: 3,231
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


--------------------------------------------------------------------------------
Post #1437001
Posted Friday, March 29, 2013 10:58 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Saturday, September 27, 2014 12:01 PM
Points: 184, Visits: 352
What is error message? I assume you considered the agent account and it's access on the target sql server.
Post #1437022
Posted Friday, March 29, 2013 11:31 AM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Yesterday @ 11:07 AM
Points: 1,783, Visits: 3,231
Sorry, it works now.
I found out the path for the file is wrong.

Thanks much
Post #1437032
Posted Monday, April 1, 2013 4:12 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Monday, September 29, 2014 9:27 PM
Points: 13,776, Visits: 28,178
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
SQL Server 2012 Query Performance Tuning
SQL Server 2008 Query Performance Tuning Distilled
and
SQL Server Execution Plans

Product Evangelist for Red Gate Software
Post #1437391
Posted Monday, April 1, 2013 1:10 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: 2 days ago @ 7:44 PM
Points: 7,107, Visits: 12,661
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
Post #1437585
Posted Monday, April 1, 2013 2:23 PM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Yesterday @ 11:07 AM
Points: 1,783, Visits: 3,231
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.
Post #1437606
Posted Monday, April 1, 2013 3:48 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 11:18 PM
Points: 35,267, Visits: 31,759
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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1437642
Posted Tuesday, November 19, 2013 2:10 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, September 24, 2014 2:45 PM
Points: 17, Visits: 129
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.
Post #1515774
Posted Tuesday, November 19, 2013 3:20 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 11:18 PM
Points: 35,267, Visits: 31,759
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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1515813
Posted Tuesday, November 19, 2013 3:22 PM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Yesterday @ 11:07 AM
Points: 1,783, Visits: 3,231
No, we don't. Sorry for the late response.
Post #1515814
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse