powershell 使用代理 - CSDN

2020-12-24 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/

相關焦點

  • 專題系列分享之PowerShell5.X與WMI的集成
    通過這些演示,我們就可以了解到在一 些特定的場景當中,powershell必須依賴WMI才能去完成自己的任務。通過 DMTF制定的標準,軟體開發人員或者IT運維人員就可以使用同樣的方法去獲取到這一臺服務 器它的品牌和型號,以及作業系統的類型和應用程式的信息。這樣我們就屏蔽底層系統或者硬 件的差異 ,而通過同一種方式去獲取到這些目標的基本信息。
  • 內網穿透FRP+IP代理SS實現遠程連接無公網的電腦並使用代理IP入網
    2.2 再在目錄下創建start.bat,填入:3、操作機上啟動SS前景提要前提條件:無公網IP的PC(windows,目標機)、有公網IP的伺服器(中繼)、另一臺PC(windows,當前操作機)本人使用情況
  • 對抗學習專題及常見問題 - CSDN
    物理世界中的對抗樣本,有列印重照、亮度對比度等調整)【https://blog.csdn.net/u010710787/article/details/78916762】進度中:Towards the Science of Security and Privacy in Machine Learning (survey of 對抗學習)【ml:三大類ml模型,三種收集數據的用途,劃分為訓練與推理(使用
  • android開發 自我優勢 - CSDN
    Android中View的繪製,動畫,事件分發等熟練使用優秀開源框架:項目主體:retrofit2 + rxjava + dagger2 + MVP(模式)作為項目主體架構單項功能:gradle(圖片加載),greendao(資料庫),MPAndroid(圖表), netty(網絡) 等等...
  • 什麼是匿名IP代理,為什麼使用匿名IP代理?
    什麼是匿名IP代理?在之前的文章中Lum Proxy已經為大家分享過很多關於代理IP的知識了,大家都知道代理IP可分為可以分為透明代理、普通匿名IP代理和高級匿名IP代理三種。其中,最受歡迎的IP代理就是高級匿名IP代理。
  • 滲透測試神器Cobalt Strike使用教程
    Cobalt Strike已經不再使用MSF而是作為單獨的平臺使用,它分為客戶端與服務端,服務端是一個,客戶端可以有多個,可被團隊進行分布式協團操作。Cobalt Strike集成了埠轉發、掃描多模式埠Listener、Windows exe程序生成、Windows dll動態連結庫生成、java程序生成、office宏代碼生成,包括站點克隆獲取瀏覽器的相關信息等。
  • 如何高效使用爬蟲IP代理?
    如何高效使用爬蟲代理?如何高效使用爬蟲IP代理?一、高效的爬蟲程序高效的數據採集離不開優秀的爬蟲程序,想要爬蟲程序穩定運行,網絡環境和電腦系統的穩定性一定要到位。由於使用了代理伺服器,要兼顧到代理伺服器的穩定性,爬蟲程序要有自己相應的容錯機制。當然,想要正常爬取還需要一個好用的轉化存儲系統,這樣才能確保程序爬取到的數據能夠正常存儲使用。
  • react 漏洞 - CSDN
    谷歌推出軟體訂閱服務「Google Play Pass」:每月4.99美元據The Verge報導,谷歌推出了一項名為「Google Play Pass」的軟體訂閱服務,每月收費4.99美元,Android用戶可以使用超過350款遊戲和應用程式,這些遊戲和應用程式將沒有廣告以及應用程式內購買。
  • 【乾貨分享】如何使用代理伺服器上Facebook
    突破限制:有的時候網絡供應商會對上網用戶的埠,目的網站,協議,遊戲,即時通訊軟體等的限[]制,使用代理伺服器都可以突破這些限制。代理伺服器主要類型HTTP代理:做簡單的1種代理形式,能夠代理客戶機的HTTP訪問,上網瀏覽網頁使用的都是HTTP協議,通常的HTTP代理埠為80、3128或8080埠。
  • 如何使用代理IP軟體修改IP上網
    當我們在上網時,訪問某些網站打不開時,可以考慮使用代理IP試一試,也許會有不一樣的驚喜。那麼,如何使用代理IP軟體修改IP上網呢?一、使用代理IP軟體修改局部IP上網1、線程IP池軟體這款軟體操作起來非常簡單,下載壓縮包後,雙擊「線程IP池.exe」打開並登錄,選擇一個線程IP右鍵「設置該線程IP到IE瀏覽器」即可。
  • 代理ip使用前你需要了解什麼呢?
    網際網路的快速發展,讓代理IP的應用範圍也越來越廣泛,但在使用代理IP之前,需要了解一些基本概念,這樣才能避免一些不必要的錯誤,提高工作效率。一、代理協議常用的代理協議一般有三種,分別是http協議,https協議和socks5協議。
  • 為什麼在使用代理IP爬蟲時會出現超時?
    相信很多朋友在使用代理IP爬蟲時都遇到過這類狀況:做了充足的準備,剛剛開始一天的爬蟲工作時,就出現提示「訪問網站地址請求超時」,使用免費代理IP時這種情況更為頻發。為什麼在使用代理IP爬蟲時會出現超時?那麼為什麼在使用代理IP爬蟲時會出現超時呢?
  • 如何使用瀏覽器來測試代理IP是否可用
    在使用代理IP進行工作的時候,經常會遇到很多問題。有的問題可以很快地找出原因,進而解決;有的問題不能直接找出原因,無從下手。例如,在軟體裡使用,報錯信息僅為「沒有可用IP」、「當前IP不可用」等;在程序代碼裡,沒有報錯信息,僅僅返回空白等信息。
  • 《未來戰警》假如有代理機器人,你願意使用嗎?
    想起曾看的科幻片《未來戰警》,對裡面的代理機器人印象尤為深刻。代理機器人可以代替使用者進行一切諸如工作,娛樂等活動,使用者只需要遁隱在家中用意念操控機器人即可。代理機器人為使用者解決了很多問題,比如殘疾,衰老,意外事故等……因而風靡全球,高達10億人在使用!街上充斥著形形色色光鮮亮麗的代理機器人,共同構築了一個烏託邦的虛擬世界!
  • android 監聽屏幕鎖屏專題及常見問題 - CSDN
    > 鎖屏聽音樂(音頻),沒有鎖屏看視頻Android系統亮屏、鎖屏、屏幕解鎖事件(解決部分手機亮屏後未解鎖即進入resume狀態)- http://blog.csdn.net/oracleot/article/details/20378453Android 實現鎖屏的較完美方案- https://segmentfault.com/a/1190000003075989
  • |CSDN 博文精選
    使用mRecyclerView.setLayoutManager(stackLayoutManager = new StackLayoutManager(this));跟系統的LinearLayoutManager使用方式一致,文本只是簡單的Demo,功能單一,主要講解流程與步驟,請根據特定的需求修改。
  • 如何使用獨享IP池,與其他代理IP有什麼不同
    大多數代理IP套餐開通後,只需要在後臺管理面板綁定下IP白名單,然後生成API提取連結,就可以開始使用了。但在開通站大爺的獨享IP池後,按照這樣的操作是提取不到IP的,我們一起來看看獨享IP池如何使用。
  • 育碧更新Uplay服務條款 禁止玩家利用bug及使用代理
    育碧更新Uplay服務條款 禁止玩家利用bug及使用代理 時間:2020-05-15 10:15:00