Solved

execute powershell with parameters in sql agent job

Posted on 2014-09-25
8
540 Views
Last Modified: 2014-10-27
Hello,

I can execute the following script in cmdexec window. But when I add it on a step in SQL AGENT Job, the job executed successfully but do nothing :

powershell -nologo -command "& {&'c:\script\backup_disk.ps1' -I SRVTEST -T F -R 24 -D \\nas$\srvtest -L C:\script\logs}"

Thanks

Regards
0
Comment
Question by:bibi92
  • 3
  • 3
  • 2
8 Comments
 
LVL 29

Expert Comment

by:becraig
ID: 40347216
Are you saying you run the powershell script under sql agent and nothing happens ?

Can you maybe put a start and stop transcript into your backup_disk.ps1 script and take a look at the output.
It might be an issue with a parameter or some other input.
0
 
LVL 26

Expert Comment

by:Zberteoc
ID: 40347484
In the step properties(in edit window) make sure that the type of command is Powershell instead of Transact-SQL.
0
 

Author Comment

by:bibi92
ID: 40348446
It's an cmdexec step and not transact-sql.
0
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 26

Expert Comment

by:Zberteoc
ID: 40348584
There is also  Powershell command.
0
 

Author Comment

by:bibi92
ID: 40350364
ok I know but I have to replace powershell command by cmdexec because there are only two threads powershell for SQL AGENT.
0
 
LVL 29

Expert Comment

by:becraig
ID: 40350377
Have you actually tried seeing what happens when your script is run by sql agent ?

Try adding the Executionpolicy to your powershell command, it may be an execution restriction issue.

powershell -Executionpolicy Bypass -nologo -command "& {&'c:\script\backup_disk.ps1' -I SRVTEST -T F -R 24 -D \\nas$\srvtest -L C:\script\logs}"
0
 

Author Comment

by:bibi92
ID: 40362532
Have you actually tried seeing what happens when your script is run by sql agent ?

Yes, I have add output log in the step but the file is empty

I have tested , but same result :
powershell -Executionpolicy Bypass -nologo -command "& {&'c:\script\backup_disk.ps1' -I SRVTEST -T F -R 24 -D \\nas$\srvtest -L C:\script\logs}"

Thanks
0
 
LVL 29

Accepted Solution

by:
becraig earned 500 total points
ID: 40362628
Please edit c:\script\backup_disk.ps1

Add the following lines at the start of the script:
start-transcript -path c:\somefile.txt -noclobber


Then add  the line below to the end of the script:
stop-transcript

We can then open "c:\somefile.txt and see what is really happening when powershell calls c:\script\backup_disk.ps1
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
A brief introduction to what I consider to be the best editor for PowerShell.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question