powershell 使用代理 - CSDN

2020-12-16 CSDN技術社區


問題 (Problem)

One of the things that a DBA needs to do is to monitor the status of SQL ServerAgent Jobs. This can be done instance by instance, but this takes time.  Inthis article we look at how to collect the last run information for all of the SQLServer instances that are being monitored with this solution.

DBA需要做的一件事是監視SQL Server代理作業的狀態。 可以逐個實例完成此操作,但這需要時間。 在本文中,我們研究如何為使用此解決方案監視的所有SQL Server實例收集上次運行信息。

(Solution)

This module will collect the information of the very last execution status ofeach SQL Server Agent Job for all SQL Server instances within the inventory.MasterServerListtable.

此模塊將為清單.MasterServerList表中的所有SQL Server實例收集每個SQL Server代理作業的最後執行狀態的信息。

先決條件 (Prerequisites)

In order to use this module, you need to createthe core objects found here and also setupthis module to collect SQL Agent job information.

為了使用此模塊,您需要創建在此處找到的核心對象 ,還需要設置此模塊以收集SQL Agent作業信息 。

資料庫對象 (Database Objects)

One new table will be created and it is a table to store the information of thelast execution of the SQL Server Agent Jobs from each instance.

將創建一個新表,該表將存儲每個實例中最後一次執行SQL Server代理作業的信息。

This is the structure of the table, so you can get an idea of what data willbe stored.

這是表的結構,因此您可以了解將存儲哪些數據。

Note: If you want to add more fields to this table, make sure to adjust the structurewithin the PowerShell script and adapt the respective logic that will handle theadditional fields.

注意:如果要向該表添加更多欄位,請確保調整PowerShell腳本中的結構並調整將處理其他欄位的相應邏輯。

桌子 (Tables)

  • monitoring.Jobs

    監視工作

    • serverId - serverid ties back to inventory.MasterServerList
    • serverId -serverid 綁定到清單。MasterServerList
    • job_name - name of the SQL Agent jobjob_name -SQL代理作業的名稱
    • last_run_date_time - last run date and timelast_run_date_time-上次運行的日期和時間
    • last_run_duration - length of the runlast_run_duration-運行時間
    • last_run_status - status of the last runlast_run_status-上次運行的狀態
    • last_run_status_message - any messages from the runlast_run_status_message-運行中的任何消息
    • next_run_date_time - next date and time the job willrunnext_run_date_time-作業將運行的下一個日期和時間
    • data_collection_timestamp - when data was last collected.data_collection_timestamp-上次收集數據的時間。

PowerShell腳本 (PowerShell Script)

The PowerShell script that creates the above object and inserts data into themonitoring.Jobs table is called:

創建上述對象並將數據插入到監視中的PowerShell腳本稱為Jobs表:

The script has some validations that will help you check if some key elementsare missing for the script to run successfully. For instance, it will confirm thatthe inventory.MasterServerList table exists and that it has at least 1 active instanceregistered to be able to have something to work with, and it will also check thatthe inventory.Jobs table exists within the central repository database.

該腳本具有一些驗證,可以幫助您檢查是否缺少某些關鍵元素才能使腳本成功運行。 例如,它將確認stocking.MasterServerList表存在並且至少註冊了一個活動實例以便可以使用某些東西,並且還將檢查中央存儲庫資料庫中是否存在stock.Jobs表。 。

If you have followed along the other modules, you will notice that we have beenstoring all objects in "C:\temp", but you can use any folder you want.If you make a change to the central folder location, you will need to edit the firstline in the following PowerShell script to specify the new folder location.

如果您遵循了其他模塊,則會注意到我們已經將所有對象存儲在「 C:\ temp」中,但是您可以使用所需的任何文件夾。 如果更改中央文件夾位置,則需要在以下PowerShell腳本中編輯第一行以指定新文件夾位置。

如何使用 (How to Use)

Navigate to the folder where you created the files and you can run the PowerShellscript as follows:

導航到創建文件的文件夾,然後可以運行PowerShell腳本,如下所示:

選項1 (Option 1)

選項2 (Option 2)

  • Open a command window and navigate to the folder where you saved the abovefiles and run

    打開命令窗口,然後導航到保存上述文件的文件夾並運行

powershell "C:\temp\Monitor-MSSQL-Instance-Jobs.ps1"

選項3 (Option 3)

  • Schedule this as a SQL Server Agent Job to run the PowerShell script on apredefined basis.

    將其安排為SQL Server代理作業,以在預定義的基礎上運行PowerShell腳本。

選項4 (Option 4)

  • Schedule this as a Windows Task Scheduler job to run the PowerShell scripton a predefined basis.

    將其調度為Windows Task Scheduler作業,以在預定義的基礎上運行PowerShell腳本。

檢查資料庫和對象的創建 (Check Creation of Database and Objects)

After running the PowerShell script, we can see the objects that are created.

運行PowerShell腳本後,我們可以看到創建的對象。

If we query both the inventory.Jobs table and monitoring.Jobs table, we can seethe data that has been collected.

如果我們同時查詢庫存。作業表和監視。作業表,則可以看到已收集的數據。

Note: The PowerShell script will store only the informationfrom the last execution, no historical data is retained. If you』d like to keep information from previousexecutions, you would have to modify the script and adapt it to your particularuse case.

注意: PowerShell腳本將僅存儲上一次執行的信息,不保留任何歷史數據。 如果您想保留以前執行的信息,則必須修改腳本並將其調整為適合您的特定用例。

檢查錯誤 (Checking for Errors)

To check for errors query the monitoring.ErrorLog table using the following query:

要檢查錯誤,請使用以下查詢查詢Monitoring.ErrorLog表:

SELECT *FROM monitoring.ErrorLogWHERE script = Monitor-MSSQL-Instance-Jobs'

If you』d like to know the SQL Server instance that got the errors, youwould have to issue the query like this:

如果您想知道出現錯誤SQL Server實例,則必須發出如下查詢:

SELECT CASE WHEN msl.instance = 'MSSQLSERVER' THEN msl.server_name ELSE CONCAT(msl.server_name,'\',msl.instance) END AS instance, e.script, e.message, e.error_timestampFROM monitoring.ErrorLog eJOIN inventory.MasterServerList msl ON msl.serverId = e.serverIdWHERE e.script = Monitor-MSSQL-Instance-Jobs'

有用的查詢 (Useful Queries)

By collecting all the data related to the execution of jobs across all your instances,you might answer things like:

通過收集所有實例中與作業執行相關的所有數據,您可能會回答以下問題:

哪些作業上次執行失敗? (Which jobs failed the last time that they were executed?)

SELECT CASE WHEN msl.instance = 'MSSQLSERVER' THEN msl.server_name ELSE CONCAT(msl.server_name,'\',msl.instance) END AS instance, m.job_name, m.last_run_date_time, m.last_run_duration, m.last_run_status, m.last_run_status_message, m.next_run_date_timeFROM monitoring.Jobs mJOIN inventory.Jobs i ON m.job_name = i.job_name AND m.serverId = i.serverIdJOIN inventory.MasterServerList msl ON msl.serverId = m.serverIdWHERE i.is_enabled = 1 AND m.last_run_status <> 'Succeeded';

Note: Keep in mind that this will report failures only for the jobs within theinventory.Jobs table that are currently enabled. It really doesn’t make senseto know this information for the disabled jobs, but if that’s what you need,then feel free to make the necessary adjustments.

注意:請記住,這將僅報告當前啟用的清單。作業表中的作業失敗。 真正了解殘疾人的工作信息並沒有任何意義,但是如果您需要此信息,請隨時進行必要的調整。

This is a very powerful one, and I highly recommend that you use this as a steppingstone to build a notification mechanism around this data. You could easily havea way to periodically know which jobs are not succeeding, across your entire infrastructure,without necessarily having to configure an email alert within each individual jobat each individual instance (imagine you have hundreds/thousands of jobs withinyour scope without alerts setup).

這是一個非常強大的功能,我強烈建議您將其用作圍繞此數據建立通知機制的墊腳石。 您可以輕鬆地通過整個基礎架構輕鬆地定期了解哪些作業不成功,而不必在每個單獨的實例中的每個單獨的作業中配置電子郵件警報(假設您的範圍內有成百上千個沒有警報的作業建立)。

哪些工作需要最長時間才能完成? (Which jobs are taking the longest time to complete?)

SELECT TOP 10 *FROM monitoring.JobsORDER BY last_run_duration DESC

Note: If you are able to identify long running jobs, then probably you havea performancetuning opportunity there. It might not be necessarily the case, but you shouldbe able to tell within your own particular environment.

注意:如果您能夠確定長時間運行的作業,則可能在那裡有性能調整的機會。 不一定是這種情況,但是您應該能夠在自己的特定環境中分辨。

下載腳本 (Download Scripts)

Download the scripts for this module

下載此模塊的腳本

下一步 (Next Steps)



Last Updated: 2020-07-27
上次更新時間:2020-07-27









關於作者 (About the author)








翻譯自: https://www.mssqltips.com/sqlservertip/6495/sql-server-agent-job-monitoring-for-last-execution-with-powershell/

相關焦點

  • 無powershell運行powershell方法總結
    今天給大家帶來的是,無powershell運行powershell的一些姿勢的分享,由於powershell的特性,使得它很受滲透測試愛好者的喜愛,當然也催生了像ASMI之類的防禦手段,當然各類殺軟也是把它納入了查殺行列中,比如某套裝,只要你調用PS就會查殺,著實噁心。
  • PowerShell Core 6.0 使用 .NET Core 2.0 作為運行時
    更新內容:從 .NET Framework 移到 .NET CorePowerShell Core 使用 .NET Core 2.0 作為其運行時。.NET Core 2.0 使 PowerShell Core 能夠在多個平臺(Windows,macOS 和 Linux)上工作。
  • PowerShell 7.0 RC 發布
    對於任何構建跨平臺應用程式的 .NET Core 開發人員來說,這都是很棒的,不論平臺是什麼,或者它們是在本地運行還是在 Azure Pipelines 等 CI/CD 解決方案中運行,他們都能夠使用一種腳本語言(PowerShell)編寫其構建、測試和部署腳本。
  • server sql 作業 使用專題及常見問題 - CSDN
    DBA需要做的一件事是監視SQL Server代理作業的狀態。 可以逐個實例完成此操作,但這需要時間。 在本文中,我們研究如何為使用此解決方案監視的所有SQL Server實例收集上次運行信息。 此模塊將為清單.MasterServerList表中的所有SQL Server實例收集每個SQL Server代理作業的最後執行狀態的信息。
  • powershell控制臺的兩種模式
    講解對象:powershell控制臺的兩種模式作者:融水公子 rsgz前言:快速編輯模式和標準模式的切換1 標準模式特點:滑鼠右擊選擇標記後才能實現複製和粘切功能
  • Git如何使用代理
    github不使用代理就可以訪問, 但如何要克隆android的源碼或其他的一些不在github的源碼會非常慢. 就算你電腦掛的科學上網也不行, 因為git並不會自動使用代理.其實要讓git使用代理非常簡單設置代理git config --global http.proxy 代理地址git config --global https.proxy 代理地址取消代理git config --global --unset http.proxygit config --global --unset
  • 滲透測試神器Cobalt Strike使用教程
    Cobalt Strike已經不再使用MSF而是作為單獨的平臺使用,它分為客戶端與服務端,服務端是一個,客戶端可以有多個,可被團隊進行分布式協團操作。本期,ISEC實驗室帶大家實戰操作Cobalt Strike神器的使用。團隊作戰圖
  • postern代理工具使用指南
    設置PosternPostern是Android系統裡一款非常流行的代理/ 虛擬專用網絡管理程序,是一個Android下的全局代理工具,可以說是安卓版surge,它可以添加規則,屏蔽廣告。Postern支持的代理協議有:· SSH隧道· Shadowsocks· SOCKS5代理· HTTPS/HTTP CONNECT method隧道· gfw.press您可以在Google Play商店裡找到這款軟體。
  • github覆蓋本地專題及常見問題 - CSDN
    5 在本地刪除分支場景:在本地不小心使用 git branch -b march2017 誤創建了一個新的march2017分支。現在需要刪除分支,應該如何呢?如下:git branch -d march2017參考 [5] 。
  • 代理ip的獲取及效率最大化使用方法
    大數據的發展使代理ip出現,我們所瀏覽的網站每天都會有大量的數據產生和更新,很多的網站為了能夠正常的讓用戶進行使用,就會設置相應的限制,在這樣的狀況下我們就需要代理的代理ip。那麼穩定代理ip應該怎樣獲得並使用。
  • 一篇文章讓你擁有用不完的ip代理
    所以我們需要使用代理來避免這種情況。正文:網上有一些專門提供代理IP的網站,這裡展現了兩個,如果需要更多可以自行搜索查找。1.西刺免費代理免費IP非常多,這個網站也在很早就有了,但是有一個缺點,我爬取這個網站上的代理的時候爬不了幾頁就被封了!即使我每爬一頁都設置了3秒鐘的間隔。難道我還要用代理來爬取代理?於是我又找其他的代理網站。
  • 怎麼安全地使用代理IP上網
    不論男女老少,都會上網,隨之而來的網絡安全問題也不容忽視,為了保護自己的隱私,為了杜絕安全隱患,越來越多的人開始使用代理IP上網。那麼,怎麼安全的使用代理IP上網呢?一、使用高匿代理IP只有高匿代理IP用起來和沒用沒什麼兩樣,使用透明代理IP會暴露客戶端真實IP,使用普匿代理IP會暴露正在使用代理IP上網。
  • 系統管理員常用的Powershell命令
    同時呢,工欲善其事,必先利其器。幫助文檔需要更新到最新版本。更新幫助文檔很簡單,輸入help-update,就會更新了。知道了這兩個最基本的命令,我們再來看看正題,系統管理常見的30多個操作都可以用powershell實現。網絡基本配置和查看:1.
  • 安卓免費學習的博客,github,CSDN,視頻教程,一網打盡!
    coder-pig(Android入門):http://blog.csdn.net/coder_pig張鴻洋:http://blog.csdn.net/lmj623565791張愛民(愛哥)(自定義View):http://blog.csdn.net/aigestudio羅昇陽(老羅)(Android源碼解析):http://blog.csdn.net
  • 用代理伺服器在國內使用google搜索
    代理伺服器簡潔的說,當我們上網時,我們是和服務端建立了連接(利用我們和對方的IP位址),網絡信息直接從服務端傳遞給了我們客戶端。但是我國不允許我們在國內訪問某些網站,於是我們可以使用代理伺服器。原理就是當我們要訪問某個特殊的網站時,我們可以先和代理伺服器建立連接,再使用代理伺服器訪問那個網站,於是服務端的網絡信息就傳遞給了代理伺服器,然後代理伺服器在把網絡信息傳遞給我們客戶端。這樣就解決了不能訪問某些網站的問題了。一些操作這個方法只能在電腦上使用。
  • 使用代理IP訪問網頁真的安全嗎
    在信息化高速發展的今天,網際網路開始進入大數據時代,網絡安全一直是人們掛在嘴邊的話題,代理IP作為這個時代的產物也應運而生,那麼問題來了,使用代理IP訪問網頁真的安全嗎?有人說,使用代理IP上網當然更安全了,使用代理IP上網可以隱藏自己的真實IP,如此就不會被惡意追蹤和攻擊,如此便會提高上網的安全性。有人說,使用代理IP上網不見得安全到哪裡去,代理IP說不定會暴露你的真實IP,一樣是有安全隱患的。那麼使用代理IP真的會暴露客戶端的真實IP嗎?
  • 【乾貨分享】如何使用代理伺服器上Facebook
    突破限制:有的時候網絡供應商會對上網用戶的埠,目的網站,協議,遊戲,即時通訊軟體等的限[]制,使用代理伺服器都可以突破這些限制。代理伺服器主要類型HTTP代理:做簡單的1種代理形式,能夠代理客戶機的HTTP訪問,上網瀏覽網頁使用的都是HTTP協議,通常的HTTP代理埠為80、3128或8080埠。