简体   繁体   中英

Can't run a Powershell command in a SQL Server Agent Job Step

I tried with "Powershell" option and commands (both don't work):

Get-Item "*.csv" | Out-File "d:\tracking\$(get-date -f yyyyMMdd-hhmmss).txt"

"Get-Item """*.csv""" | Out-File """d:\tracking\$(get-date -f yyyyMMdd-hhmmss).txt""""

And tried with "CmdExec" option and command (it doesn't work):

powershell.exe -Command "Get-Item """*.csv""" | Out-File """d:\tracking\$(get-date -f yyyyMMdd-hhmmss).txt""""

But the last command runs ok on a separate cmd.exe window and the following also runs ok inside the powershell:

Get-Item "*.csv" | Out-File "d:\tracking\$(get-date -f yyyyMMdd-hhmmss).txt"

They create a text file eg. "20220607-112233.txt" containing the directory listing of CSV files

However I can't get this command to work from within the Job Step The step finishes with "Unable to run. Syntax error"

Ok, I found a solution:

I created a batch (.bat file) with the code inside block 2:

powershell.exe -Command "Get-Item """*.csv""" | Out-File """d:\tracking\$(get-date -f yyyyMMdd-HHmmss).txt""""

(I only changed the hh for HH, for 24 hours based time)

Then, I configured the Job Step to run the bat file (setting the option "cmdExec")

That's all

I don't know what was going on, directory permissions were ok, the command of code block #2 above even works if I execute it with xp_cmdshell

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM