Friday, December 13, 2019

"syspolicy_purge_history" SQL Agent Job is failing with error "Cannot find drive A drive with the name 'SQLSERVER' does not exist"


The SQL Agent Job "syspolicy_purge_history" was failing in our environment since we have upgraded our SQL server instance from SQL 2008R2 to SQL2012. The job was failing at step-3 named as "Erase Phantom System Health Records" We have tried each and every article available over internet, but nothing was helpful in our case. After a lot of troubleshooting my friend Desh found the solution for it. 

There are still lot of DBA's searching the fix of same issue. So, I want to share the all steps which fixed the issue. You can try it in your case and let us know if this works.

Issue Details:

SQL Server version: SQL Server 2012
Job Name: syspolicy_purge_history

Error:
Executed as user: "XXXXX". The job script encountered the following errors. 
These errors did not stop the script:  A job step received an error at line 1 in a PowerShell script. The corresponding line is 'import-module SQLPS  -DisableNameChecking'. Correct the script and reschedule the job. The error information returned by PowerShell is: 'The specified module 'SQLPS' was not loaded because no valid module file was found in any module directory.  '  A job step received an error at line 2 in a PowerShell script. The corresponding line is '(Get-Item SQLSERVER:\SQLPolicy\ServerName$a).EraseSystemHealthPhantomRecords()'. Correct the script and reschedule the job. The error information returned by PowerShell is: 'Cannot find drive. A drive with the name 'SQLSERVER' does not exist.  '  A job step received an error at line 2 in a PowerShell script. The corresponding line is '(Get-Item SQLSERVER:\SQLPolicy\



Steps to Fix the issue:

The first step is to check if SQLPS folder exists to below location:

C:\Program Files\Microsoft SQL Server\110\Tools\PowerShell\Modules




Now Open Computer Properties and then click on advance system settings> environment variables> Copy the environment path to a notepad file




%ProgramFiles%\WindowsPowerShell\Modules;
%SystemRoot%\system32\WindowsPowerShell\v1.0\Modules

Now copy the SQLPS folder from location “C:\Program Files\Microsoft SQL Server\110\Tools\PowerShell\Modules“ to below locations which we found under environment variables:

 %ProgramFiles%\WindowsPowerShell\Modules
                                           %SystemRoot%\system32\WindowsPowerShell\v1.0\Modules

You can Open the above two locations via start>run> and then paste location and then Enter.
Once this folder is copied to these locations. Try to run the below command from Powershell.

Open Powershell  as admin and then execute the below command.
import-module SQLPS  -DisableNameChecking


Once import module is done. Open the Group Policy Object Editor as below:
  1.  Start > run > "mmc"
  2.  File > Add/Remove Snap-In...
  3.  Under Available snap-ins, select "Group Policy Object Editor" and click Add, then Finish.
  4.  Click OK to snap-in the GPO Editor

Now navigate to the specific policy:
  1.  Console Root > Local Computer Policy > Computer Configuration > Administrative Templates > Windows Components > Windows Powershell
  2.  Open-up the "Turn on Script Execution" policy setting
  3.  Enable Script Execution and in Options select “ Allow All Scripts”. Apply the setting and click on OK.
  4.  Exit from this window

Below is the screen shot for above steps for your reference:









Exit from this window and during save pop-up dialog click on No.


Now Try to run the Job it should be running without any issue.




Hope this works in your case.

6 comments:

  1. Thanks Vimal, I have applied this fix successfully to my SQL Server! The steps are very clear and helpful!

    ReplyDelete
  2. What can I do if the PowerShell is Blocked by Security Policy at universe of servers, This is a corporate policy, is there another way to solve this issue?

    ReplyDelete
  3. thanks but if the folder SQLPS is not available at location C:\Program Files\Microsoft SQL Server\110\Tools

    ReplyDelete
  4. Hi,
    Did you get a solution for this? I don't have that folder either. Were you able to get a path on how to proceed?

    ReplyDelete