Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

Struggling to execute PowerShell commands in SqlServer Agent job of MS SQL Server 2012 R2 (Windows Server 2008R2, Windows 7 Prof)

I need to delete (.bak files older, than, say, 7 days in remote file share.

Before inserting the command into SqlServerAgent job, I tried to execute it from Windows command line:

gives the error:
"ERROR: UNC paths (\machine\share) are not supported"

The latter deletes the required remote files when used from Windows PowerShell command prompt but when executed in/from an SqlServerAgent job - fails

The history of such SqlServerAgent jobs shows, quoting:

  • The job succeeded. The job was invoked by User domain\domainAccountName. The last step to run was step 1

    • Executed as user: localHostName\sqlbackup. The job script encountered the following errors. These errors did not stop script: A job step received an error at line in a PowerShellScript. The corresponding line is 'dir "\\RemoteHostName\sharedFolderName" |? {$_.CreationTime -lt (get-date).AddDays(-7)} | del'. Correct the script and reschedule the job. The error information returned by PowerShell is: 'Cannot find path '\\RemoteHostName\sharedFolderName' because it does not exist. ' Process Exit Code 0. The step succeeded.

    enter image description here

Note the green check-marks on failed job and job steps

Again, zoomed-in the text output of the result:

enter image description here

Also, when executed this SqlServerAgent job step shows: "processing", then "Success"

enter image description here

though it is persistingly failing to delete required files.

The question(s):

  1. How to make the job and step, containing errors, to show "Failed" instead of of "Succeeded"?

  2. Why does the purging of older files using Windows PowerShell commands succeedes from command line prompt and fails from within SqlServerAgent job&step?

    2a. How succeed with removing of older remote files on schedule from SQL Server 2012?

PS
I am using SQL Server Agent Job proxy according to this answer using local Windows user accounts on remote/target and local/source hosts with the same/equal names sqlbackup and the passwords

The same story with scheduling in Windows Task Scheduler - the job and steps are "succeeding" without deleting anything though copying to remote location (out of description here) by previous steps are successful. And I am interested to centralize all in SQL Server SqlServerAgent job (but not outside)

Struggling to execute PowerShell commands in SqlServer Agent job of MS SQL Server 2012 R2 (Windows Server 2008R2, Windows 7 Prof)

I need to delete (.bak files older, than, say, 7 days in remote file share.

Before inserting the command into SqlServerAgent job, I tried to execute it from Windows command line:

  • using forfiles :
    forfiles /P "\\RemoteHostName\sharedFolderName" /s /m *.bak /D -7 /C "cmd /c del @path"

gives the error:
"ERROR: UNC paths (\machine\share) are not supported"

The latter deletes the required remote files when used from Windows PowerShell command prompt but when executed in/from an SqlServerAgent job - fails

The history of such SqlServerAgent jobs shows, quoting:

  • The job succeeded. The job was invoked by User domain\domainAccountName. The last step to run was step 1

    • Executed as user: localHostName\sqlbackup. The job script encountered the following errors. These errors did not stop script: A job step received an error at line in a PowerShellScript. The corresponding line is 'dir "\\RemoteHostName\sharedFolderName" |? {$_.CreationTime -lt (get-date).AddDays(-7)} | del'. Correct the script and reschedule the job. The error information returned by PowerShell is: 'Cannot find path '\\RemoteHostName\sharedFolderName' because it does not exist. ' Process Exit Code 0. The step succeeded.

    enter image description here

Note the green check-marks on failed job and job steps

Again, zoomed-in the text output of the result:

enter image description here

Also, when executed this SqlServerAgent job step shows: "processing", then "Success"

enter image description here

though it is persistingly failing to delete required files.

The question(s):

  1. How to make the job and step, containing errors, to show "Failed" instead of of "Succeeded"?

  2. Why does the purging of older files using Windows PowerShell commands succeedes from command line prompt and fails from within SqlServerAgent job&step?

    2a. How succeed with removing of older remote files on schedule from SQL Server 2012?

PS
I am using SQL Server Agent Job proxy according to this answer using local Windows user accounts on remote/target and local/source hosts with the same/equal names sqlbackup and the passwords

The same story with scheduling in Windows Task Scheduler - the job and steps are "succeeding" without deleting anything though copying to remote location (out of description here) by previous steps are successful. And I am interested to centralize all in SQL Server SqlServerAgent job (but not outside)

Struggling to execute PowerShell commands in SqlServer Agent job of MS SQL Server 2012 R2 (Windows Server 2008R2, Windows 7 Prof)

I need to delete (.bak files older, than, say, 7 days in remote file share.

Before inserting the command into SqlServerAgent job, I tried to execute it from Windows command line:

  • using forfiles :
    forfiles /P "\\RemoteHostName\sharedFolderName" /s /m *.bak /D -7 /C "cmd /c del @path"

gives the error:
"ERROR: UNC paths (\machine\share) are not supported"

The latter deletes the required remote files when used from Windows PowerShell command prompt but when executed in/from an SqlServerAgent job - fails

The history of such SqlServerAgent jobs shows, quoting:

  • The job succeeded. The job was invoked by User domain\domainAccountName. The last step to run was step 1

    • Executed as user: localHostName\sqlbackup. The job script encountered the following errors. These errors did not stop script: A job step received an error at line in a PowerShellScript. The corresponding line is 'dir "\\RemoteHostName\sharedFolderName" |? {$_.CreationTime -lt (get-date).AddDays(-7)} | del'. Correct the script and reschedule the job. The error information returned by PowerShell is: 'Cannot find path '\\RemoteHostName\sharedFolderName' because it does not exist. ' Process Exit Code 0. The step succeeded.

    enter image description here

Note the green check-marks on failed job and job steps

Again, zoomed-in the text output of the result:

enter image description here

Also, when executed this SqlServerAgent job step shows: "processing", then "Success"

enter image description here

though it is persistingly failing to delete required files.

The question(s):

  1. How to make the job and step, containing errors, to show "Failed" instead of of "Succeeded"?

  2. Why does the purging of older files using Windows PowerShell commands succeedes from command line prompt and fails from within SqlServerAgent job&step?

    2a. How succeed with removing of older remote files on schedule from SQL Server 2012?

PS
I am using SQL Server Agent Job proxy according to this answer using local Windows user accounts on remote/target and local/source hosts with the same/equal names sqlbackup and the passwords

The same story with scheduling in Windows Task Scheduler - the job and steps are "succeeding" without deleting anything though copying to remote location (out of description here) by previous steps are successful. And I am interested to centralize all in SQL Server SqlServerAgent job (but not outside)

replaced http://dba.stackexchange.com/ with https://dba.stackexchange.com/
Source Link

Struggling to execute PowerShell commands in SqlServer Agent job of MS SQL Server 2012 R2 (Windows Server 2008R2, Windows 7 Prof)

I need to delete (.bak files older, than, say, 7 days in remote file share.

Before inserting the command into SqlServerAgent job, I tried to execute it from Windows command line:

  • using forfiles :
    forfiles /P "\\RemoteHostName\sharedFolderName" /s /m *.bak /D -7 /C "cmd /c del @path"

gives the error:
"ERROR: UNC paths (\machine\share) are not supported"

The latter deletes the required remote files when used from Windows PowerShell command prompt but when executed in/from an SqlServerAgent job - fails

The history of such SqlServerAgent jobs shows, quoting:

  • The job succeeded. The job was invoked by User domain\domainAccountName. The last step to run was step 1

    • Executed as user: localHostName\sqlbackup. The job script encountered the following errors. These errors did not stop script: A job step received an error at line in a PowerShellScript. The corresponding line is 'dir "\\RemoteHostName\sharedFolderName" |? {$_.CreationTime -lt (get-date).AddDays(-7)} | del'. Correct the script and reschedule the job. The error information returned by PowerShell is: 'Cannot find path '\\RemoteHostName\sharedFolderName' because it does not exist. ' Process Exit Code 0. The step succeeded.

    enter image description here

Note the green check-marks on failed job and job steps

Again, zoomed-in the text output of the result:

enter image description here

Also, when executed this SqlServerAgent job step shows: "processing", then "Success"

enter image description here

though it is persistingly failing to delete required files.

The question(s):

  1. How to make the job and step, containing errors, to show "Failed" instead of of "Succeeded"?

  2. Why does the purging of older files using Windows PowerShell commands succeedes from command line prompt and fails from within SqlServerAgent job&step?

    2a. How succeed with removing of older remote files on schedule from SQL Server 2012?

PS
I am using SQL Server Agent Job proxy according to this answerthis answer using local Windows user accounts on remote/target and local/source hosts with the same/equal names sqlbackup and the passwords

The same story with scheduling in Windows Task Scheduler - the job and steps are "succeeding" without deleting anything though copying to remote location (out of description here) by previous steps are successful. And I am interested to centralize all in SQL Server SqlServerAgent job (but not outside)

Struggling to execute PowerShell commands in SqlServer Agent job of MS SQL Server 2012 R2 (Windows Server 2008R2, Windows 7 Prof)

I need to delete (.bak files older, than, say, 7 days in remote file share.

Before inserting the command into SqlServerAgent job, I tried to execute it from Windows command line:

  • using forfiles :
    forfiles /P "\\RemoteHostName\sharedFolderName" /s /m *.bak /D -7 /C "cmd /c del @path"

gives the error:
"ERROR: UNC paths (\machine\share) are not supported"

The latter deletes the required remote files when used from Windows PowerShell command prompt but when executed in/from an SqlServerAgent job - fails

The history of such SqlServerAgent jobs shows, quoting:

  • The job succeeded. The job was invoked by User domain\domainAccountName. The last step to run was step 1

    • Executed as user: localHostName\sqlbackup. The job script encountered the following errors. These errors did not stop script: A job step received an error at line in a PowerShellScript. The corresponding line is 'dir "\\RemoteHostName\sharedFolderName" |? {$_.CreationTime -lt (get-date).AddDays(-7)} | del'. Correct the script and reschedule the job. The error information returned by PowerShell is: 'Cannot find path '\\RemoteHostName\sharedFolderName' because it does not exist. ' Process Exit Code 0. The step succeeded.

    enter image description here

Note the green check-marks on failed job and job steps

Again, zoomed-in the text output of the result:

enter image description here

Also, when executed this SqlServerAgent job step shows: "processing", then "Success"

enter image description here

though it is persistingly failing to delete required files.

The question(s):

  1. How to make the job and step, containing errors, to show "Failed" instead of of "Succeeded"?

  2. Why does the purging of older files using Windows PowerShell commands succeedes from command line prompt and fails from within SqlServerAgent job&step?

    2a. How succeed with removing of older remote files on schedule from SQL Server 2012?

PS
I am using SQL Server Agent Job proxy according to this answer using local Windows user accounts on remote/target and local/source hosts with the same/equal names sqlbackup and the passwords

The same story with scheduling in Windows Task Scheduler - the job and steps are "succeeding" without deleting anything though copying to remote location (out of description here) by previous steps are successful. And I am interested to centralize all in SQL Server SqlServerAgent job (but not outside)

Struggling to execute PowerShell commands in SqlServer Agent job of MS SQL Server 2012 R2 (Windows Server 2008R2, Windows 7 Prof)

I need to delete (.bak files older, than, say, 7 days in remote file share.

Before inserting the command into SqlServerAgent job, I tried to execute it from Windows command line:

  • using forfiles :
    forfiles /P "\\RemoteHostName\sharedFolderName" /s /m *.bak /D -7 /C "cmd /c del @path"

gives the error:
"ERROR: UNC paths (\machine\share) are not supported"

The latter deletes the required remote files when used from Windows PowerShell command prompt but when executed in/from an SqlServerAgent job - fails

The history of such SqlServerAgent jobs shows, quoting:

  • The job succeeded. The job was invoked by User domain\domainAccountName. The last step to run was step 1

    • Executed as user: localHostName\sqlbackup. The job script encountered the following errors. These errors did not stop script: A job step received an error at line in a PowerShellScript. The corresponding line is 'dir "\\RemoteHostName\sharedFolderName" |? {$_.CreationTime -lt (get-date).AddDays(-7)} | del'. Correct the script and reschedule the job. The error information returned by PowerShell is: 'Cannot find path '\\RemoteHostName\sharedFolderName' because it does not exist. ' Process Exit Code 0. The step succeeded.

    enter image description here

Note the green check-marks on failed job and job steps

Again, zoomed-in the text output of the result:

enter image description here

Also, when executed this SqlServerAgent job step shows: "processing", then "Success"

enter image description here

though it is persistingly failing to delete required files.

The question(s):

  1. How to make the job and step, containing errors, to show "Failed" instead of of "Succeeded"?

  2. Why does the purging of older files using Windows PowerShell commands succeedes from command line prompt and fails from within SqlServerAgent job&step?

    2a. How succeed with removing of older remote files on schedule from SQL Server 2012?

PS
I am using SQL Server Agent Job proxy according to this answer using local Windows user accounts on remote/target and local/source hosts with the same/equal names sqlbackup and the passwords

The same story with scheduling in Windows Task Scheduler - the job and steps are "succeeding" without deleting anything though copying to remote location (out of description here) by previous steps are successful. And I am interested to centralize all in SQL Server SqlServerAgent job (but not outside)

replaced http://serverfault.com/ with https://serverfault.com/
Source Link

Struggling to execute PowerShell commands in SqlServer Agent job of MS SQL Server 2012 R2 (Windows Server 2008R2, Windows 7 Prof)

I need to delete (.bak files older, than, say, 7 days in remote file share.

Before inserting the command into SqlServerAgent job, I tried to execute it from Windows command line:

  • using forfiles :
    forfiles /P "\\RemoteHostName\sharedFolderName" /s /m *.bak /D -7 /C "cmd /c del @path"

gives the error:
"ERROR: UNC paths (\machine\share) are not supported"

The latter deletes the required remote files when used from Windows PowerShell command prompt but when executed in/from an SqlServerAgent job - fails

The history of such SqlServerAgent jobs shows, quoting:

  • The job succeeded. The job was invoked by User domain\domainAccountName. The last step to run was step 1

    • Executed as user: localHostName\sqlbackup. The job script encountered the following errors. These errors did not stop script: A job step received an error at line in a PowerShellScript. The corresponding line is 'dir "\\RemoteHostName\sharedFolderName" |? {$_.CreationTime -lt (get-date).AddDays(-7)} | del'. Correct the script and reschedule the job. The error information returned by PowerShell is: 'Cannot find path '\\RemoteHostName\sharedFolderName' because it does not exist. ' Process Exit Code 0. The step succeeded.

    enter image description here

Note the green check-marks on failed job and job steps

Again, zoomed-in the text output of the result:

enter image description here

Also, when executed this SqlServerAgent job step shows: "processing", then "Success"

enter image description here

though it is persistingly failing to delete required files.

The question(s):

  1. How to make the job and step, containing errors, to show "Failed" instead of of "Succeeded"?

  2. Why does the purging of older files using Windows PowerShell commands succeedes from command line prompt and fails from within SqlServerAgent job&step?

    2a. How succeed with removing of older remote files on schedule from SQL Server 2012?

PS
I am using SQL Server Agent Job proxy according to this answer using local Windows user accounts on remote/target and local/source hosts with the same/equal names sqlbackup and the passwords

The same story with scheduling in Windows Task Scheduler - the job and steps are "succeeding" without deleting anything though copying to remote location (out of description here) by previous steps are successful. And I am interested to centralize all in SQL Server SqlServerAgent job (but not outside)

Struggling to execute PowerShell commands in SqlServer Agent job of MS SQL Server 2012 R2 (Windows Server 2008R2, Windows 7 Prof)

I need to delete (.bak files older, than, say, 7 days in remote file share.

Before inserting the command into SqlServerAgent job, I tried to execute it from Windows command line:

  • using forfiles :
    forfiles /P "\\RemoteHostName\sharedFolderName" /s /m *.bak /D -7 /C "cmd /c del @path"

gives the error:
"ERROR: UNC paths (\machine\share) are not supported"

The latter deletes the required remote files when used from Windows PowerShell command prompt but when executed in/from an SqlServerAgent job - fails

The history of such SqlServerAgent jobs shows, quoting:

  • The job succeeded. The job was invoked by User domain\domainAccountName. The last step to run was step 1

    • Executed as user: localHostName\sqlbackup. The job script encountered the following errors. These errors did not stop script: A job step received an error at line in a PowerShellScript. The corresponding line is 'dir "\\RemoteHostName\sharedFolderName" |? {$_.CreationTime -lt (get-date).AddDays(-7)} | del'. Correct the script and reschedule the job. The error information returned by PowerShell is: 'Cannot find path '\\RemoteHostName\sharedFolderName' because it does not exist. ' Process Exit Code 0. The step succeeded.

    enter image description here

Note the green check-marks on failed job and job steps

Again, zoomed-in the text output of the result:

enter image description here

Also, when executed this SqlServerAgent job step shows: "processing", then "Success"

enter image description here

though it is persistingly failing to delete required files.

The question(s):

  1. How to make the job and step, containing errors, to show "Failed" instead of of "Succeeded"?

  2. Why does the purging of older files using Windows PowerShell commands succeedes from command line prompt and fails from within SqlServerAgent job&step?

    2a. How succeed with removing of older remote files on schedule from SQL Server 2012?

PS
I am using SQL Server Agent Job proxy according to this answer using local Windows user accounts on remote/target and local/source hosts with the same/equal names sqlbackup and the passwords

The same story with scheduling in Windows Task Scheduler - the job and steps are "succeeding" without deleting anything though copying to remote location (out of description here) by previous steps are successful. And I am interested to centralize all in SQL Server SqlServerAgent job (but not outside)

Struggling to execute PowerShell commands in SqlServer Agent job of MS SQL Server 2012 R2 (Windows Server 2008R2, Windows 7 Prof)

I need to delete (.bak files older, than, say, 7 days in remote file share.

Before inserting the command into SqlServerAgent job, I tried to execute it from Windows command line:

  • using forfiles :
    forfiles /P "\\RemoteHostName\sharedFolderName" /s /m *.bak /D -7 /C "cmd /c del @path"

gives the error:
"ERROR: UNC paths (\machine\share) are not supported"

The latter deletes the required remote files when used from Windows PowerShell command prompt but when executed in/from an SqlServerAgent job - fails

The history of such SqlServerAgent jobs shows, quoting:

  • The job succeeded. The job was invoked by User domain\domainAccountName. The last step to run was step 1

    • Executed as user: localHostName\sqlbackup. The job script encountered the following errors. These errors did not stop script: A job step received an error at line in a PowerShellScript. The corresponding line is 'dir "\\RemoteHostName\sharedFolderName" |? {$_.CreationTime -lt (get-date).AddDays(-7)} | del'. Correct the script and reschedule the job. The error information returned by PowerShell is: 'Cannot find path '\\RemoteHostName\sharedFolderName' because it does not exist. ' Process Exit Code 0. The step succeeded.

    enter image description here

Note the green check-marks on failed job and job steps

Again, zoomed-in the text output of the result:

enter image description here

Also, when executed this SqlServerAgent job step shows: "processing", then "Success"

enter image description here

though it is persistingly failing to delete required files.

The question(s):

  1. How to make the job and step, containing errors, to show "Failed" instead of of "Succeeded"?

  2. Why does the purging of older files using Windows PowerShell commands succeedes from command line prompt and fails from within SqlServerAgent job&step?

    2a. How succeed with removing of older remote files on schedule from SQL Server 2012?

PS
I am using SQL Server Agent Job proxy according to this answer using local Windows user accounts on remote/target and local/source hosts with the same/equal names sqlbackup and the passwords

The same story with scheduling in Windows Task Scheduler - the job and steps are "succeeding" without deleting anything though copying to remote location (out of description here) by previous steps are successful. And I am interested to centralize all in SQL Server SqlServerAgent job (but not outside)

added 13 characters in body
Source Link
Loading
added 176 characters in body
Source Link
Loading
added 312 characters in body
Source Link
Loading
added 127 characters in body
Source Link
Loading
added 127 characters in body
Source Link
Loading
Loading