MS SQL Server 無法執行 PowerShell 指令 | Powershell Error Scripts Is Disabled On This System



                                       


今日檢查 Job 運作的紀錄時,突然看到一個很正常的 Job 出錯了!?

syspolicy_purge_history

咦?這不是系統內建的 JOB 嗎?安裝好 SQL Server 後就存在了啊!

怎麼會報錯呢?

查看了一下錯誤內容顯示:

Executed as user: NT Service\SQLSERVERAGENT. 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: 'File C:\Program Files (x86)\Microsoft SQL Server\120\Tools\PowerShell\Modules\SQLPS\Sqlps.ps1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at http://go.microsoft.com/fwlink/?LinkID=135170.  File C:\Program Files (x86)\Microsoft SQL Server\120\Tools\PowerShell\Modules\SQLPS\Sqlps.ps1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at http://go.microsoft.com/fwlink/?LinkID=135170.  '.  Process Exit Code -1.  The step failed.
錯誤內容顯示 Sqlps.ps1 無法被載入,而這檔案經查詢路徑是存在的。

問題是其他台安裝後都可以正常運作,為什麼會出現這個問題呢?

... ... ...

經過誠心問神後,發現這跟 PowerShell 的安全性政策有關係,

單獨執行 PowerShell 環境是正常的,不過在PS環境裡面若執行 SQLSP 指令就會出錯!



看起來問題就出在 SQLPS 這個執行檔案上面。而上面顯示一個重要的訊息:

UnauthorizedAccess,Microsoft.PowerShell.Commands.ImportModuleCommand
未授權的存取?都是在本機中耶。

利用 PS 指令:get-executionpolicy -list

可以查詢 PS執行的政策(Execution Policy) 設定。


可以看到: LocalMachine    RemoteSigned

這是屬於正確的安全性設定。

看起來應該是少了針對 SQLPS 的執行政策設定。

此時必須從登錄檔中下手,該設定路徑如下:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\
從正常的機器中可以看到



可以看到多了 [Microsoft.SqlServer.Management.PowerShell.sqlps120] 這個項目。

看起來就是少了這個才會導致。下圖是有問題機器的登錄檔資訊:


立馬從正常的機器匯出該項設定,然後到問題機器上匯入。

Register 內容:(適用 SQL Server 2014)

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.SqlServer.Management.PowerShell.sqlps120]

"Path"="C:\\Program Files (x86)\\Microsoft SQL Server\\120\\Tools\\Binn\\SQLPS.exe"

"ExecutionPolicy"="RemoteSigned"

執行匯入後,再次啟動JOB,執行就馬上正常了。

Job

打完收工。

等等!但問題成因是什麼呢???

推測可能是套用到不正確的GPO了。因為機器是有加入網域的,

所以可能是網域的GPO沒有設定正確或者其他因素導致原本加入的設定被清除了!

基本上這個設定應該是安裝好 SQL Server 後就會自動加入的。

只得後續再觀察看看了。

注意的是:SQLPS 未來的 Microsoft SQL Server 版本將移除這項功能。 請避免在新的開發工作中使用這項功能,並規劃修改目前使用這項功能的應用程式。




真的打完收工了。



--- --- 
參考:



All rights reserved.



------------------------ Powershell Sqlps Disabled On This System sqlps 公用程式 XML UML Microsoft SQL Server and MySQL Business InsightsTechnical TrackPythian Life Best Practices #Job Fail #Step 3 #Subsystem #PowerShell #CmdShell ------------------------

留言

這個網誌中的熱門文章

[工具] Excel 工具 - 檔案分拆|Excel File Splitting Into Small Files

[Linux] Ubuntu 安裝新酷音輸入法後,選字框不正常情況的解決方法

[分享] 設計模式速查表|23種設計模式類別圖 UML PDF檔案 | Design Pattern