1

I have a simple PowerShell script that has 1 line. It logs into a database,and runs a sql script of queries, that deletes old data.

sqlcmd -U myUser -P myPassword -S localHost -i .\deleteOldData.sql

When I run the script manually in PowerShell window, it works perfectly fine. It will login to the database and clear the old data. But when I created a basic task in the task scheduler and run it from there, it says it completes successfully, but it isn't look like its doing anything. It does not delete any of the old data.

My basic task is setup like

Action: Start a program

Program/script: PowerShell

Add Arguments: C:\Users\Administrator\Desktop\script.ps1
humzaqureshi
  • 39
  • 2
  • 4
  • For your arguments you should use -command "C:\Users\Administrator\Desktop\script.ps1" – Shamus Berube Mar 06 '20 at 18:51
  • Try one of two things, either set the start path in the task scheduler to the same directory of your sql script, or adjust your powershell script to know exactly where the sql script is. – AutomatedOrder Mar 06 '20 at 18:58
  • Agree with others it is a path issue. On another note, keeping a plaintext password in a file and passing it as an argument on the command line is a bad idea, security wise. – Dusty Vargas Mar 07 '20 at 07:24

1 Answers1

0

Windows 10 64-bit. PowerShell 5.1

If it is a security / permissions problem:

When running the task, use the following user account so you can watch the task in action: "Run only when user is logged on" and "Run with the highest privileges". Look for error messages.

screenshot task scheduler"Run only when user is logged on"

Or, create a logfile of the task so you can look for errors. When you create a logfile this way you will see reduced / no output in the console window. The logfile will be locked until the action completes. Repeatedly open and close the logfile to see the progress of the action if you can not wait until the action completes.

-file C:\Users\Administrator\Desktop\script.ps1> %userprofile%\desktop\logfile.txt

PowerShell script actions:

-file C:\Users\Administrator\Desktop\script.ps1
somebadhat
  • 744
  • 1
  • 5
  • 17