简体   繁体   中英

Running Powershell via SQL Server Job

I have a very basic script which runs fine on my local PC. It simply backs up some folders and their contents then adds a date stamp on the folder name.

The script backs up folders on two different servers (Server2 & Server3).

Set-ExecutionPolicy -Scope Process -ExecutionPolicy RemoteSigned

#Part 1
Copy-Item -Path "\\Server3\Example Location" -Destination "\\Server3\Example Location_$(get-date -f yyyyMMdd)" -Recurse

#Part 2
Copy-Item -Path "\\Server2\Example Location" -Destination "\\Server2\Example Location_$(get-date -f yyyyMMdd)" -Recurse

It runs both parts perfectly in the below environments:

  • On my local PC
  • When I remotely connect to Server 2 and right click > Run with PowerShell
  • When I remotely connect to Server 2 and edit > Run script

However, when I try to automate this and create a SQL Server Agent Job (again, on Server 2) only Part 2 actually backs up. The job successfully completes, but Part 1 appears to get ignored (ie running on Server 2, backing up on Server 3).

Any ideas why running as a SQL job would cause this?

nb The job is set to run as 'SQL Server Agent Service Account'.

Couple things...

If you run your PS script as a step type Operating system (CmdExec) then there will never be an error message returned to the SQL-Server Job.

With that being said, if your SQL-Server Agent Service account does not have access to the \\Server3... folders, the copy will fail and as above said, no error message will be transferred to the calling SQL-Agent Job.

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