簡體   English   中英

如何將 PowerShell 腳本作為服務運行?

[英]How do I run a PowerShell script as a service?

我創建了下面的腳本來檢查我的應用程序的端口 2025 並記錄連接數。

我需要這個腳本在 Windows 上作為服務運行,名稱為netstat_2025 有誰知道是否有這種可能性?

我不想使用任務計划程序,而是在 Windows 上將腳本作為服務運行。

腳本 SCTT521CTO.ps1

$startTime =  (Get-Date).ToString("dd_MM_yyyy")
$LogDate = ((get-date).ToLocalTime()).ToString("yyyy-MM-ddTHH:mm:ss.fff")
$hostname = hostname

$portTServer = 8000
$FileTserver = netstat -ano | findstr "8000"

$LogTserver = $LogDate + " - Quantidade de Conexoes na porta " + $portTServer + ": " + $FileTserver.count + " - Servidor: " + $hostname
$LogTserver | Out-File -Append D:\SCTT521CTO\netstat_$startTime.log

$limit = (Get-Date).AddDays(-5)
$path = "D:\SCTT521CTO\*"
Get-ChildItem -Path $path -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | Remove-Item -Force

腳本服務.ps1

# Desired name of the service
$serviceName = 'netstat_2025'

# Get the full path to powershell.exe
$powershellPath = ( Get-Command powershell ).Source

# The path to the script you want to run as a service
$serviceScriptPath = D:\scripts\SCTT521CTO.ps1

# The arguments to pass to the powershell executable each time the service starts
$args = '-ExecutionPolicy Bypass -NoProfile -File "{0}"' -f $serviceScriptPath

# Install the service using nssm
nssm install $serviceName $powershellPath $args

# See that the service is registered and check its status
Get-Service $serviceName

我原來的回答沒有考慮到您仍然需要實現powershell.exe沒有實現的服務控制接口。 但是,我確實研究了將 PowerShell 腳本作為服務運行的其他一些方法。

我遇到的為您執行此操作的一種更簡單的工具是nssm您可以使用nssm (Non-Sucking Service Manager)來注冊新服務並讓它運行您的 PowerShell 腳本。 您需要確保腳本的主要邏輯在無限循環中運行(就像大多數長時間運行的程序或服務一樣),然后您可以使用nssm注冊將運行您的 PowerShell 腳本的新服務。 下面是將代碼放入不終止的主循環的示例:

while( $true ) {
  $startTime =  (Get-Date).ToString("dd_MM_yyyy")
  $LogDate = ((get-date).ToLocalTime()).ToString("yyyy-MM-ddTHH:mm:ss.fff")
  $hostname = hostname

  $portTServer = 8000
  $FileTserver = netstat -ano | findstr "8000"

  $LogTserver = $LogDate + " - Quantidade de Conexoes na porta " + $portTServer + ": " + $FileTserver.count + " - Servidor: " + $hostname
  $LogTserver | Out-File -Append D:\SCTT521CTO\netstat_$startTime.log

  $limit = (Get-Date).AddDays(-5)
  $path = "D:\SCTT521CTO\*"
  Get-ChildItem -Path $path -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | Remove-Item -Force

  # Add a sleep at the end of the loop to prevent the script from eating
  # too much CPU time
  Start-Sleep -Seconds 60
}

要將您的腳本注冊為 PowerShell 服務,您可以使用以下 PowerShell 代碼(請注意,如果您使用Chocolatey安裝,則nssm將已經在PATH ,不確定是否在您手動安裝時):

# Desired name of the service
$serviceName = 'netstat_2025'

# Get the full path to powershell.exe
$powershellPath = ( Get-Command powershell ).Source

# The path to the script you want to run as a service
$serviceScriptPath = C:\path\to\service\script.ps1

# The arguments to pass to the powershell executable each time the service starts
$args = '-ExecutionPolicy Bypass -NoProfile -File "{0}"' -f $serviceScriptPath

# Install the service using nssm
nssm install $serviceName $powershellPath $args

# See that the service is registered and check its status
Get-Service $serviceName

您的服務現在應該已安裝並能夠像任何其他 Windows 服務一樣進行控制。 這種工作方式不是直接將powershell.exe注冊為服務,而是將nssm.exe注冊為服務可執行文件,它確實實現了正確的服務控制處理程序,然后運行您為此服務配置的任何程序(在在這種情況下,使用powershell.exe調用您的腳本)。

你可以直接從 nssm.cc 下載 NSSM:

  1. 在命令行中運行 nssm:nssm install YourServiceName

  2. 設置信息:

  • 路徑:Powershell 路徑
  • 啟動目錄:你的腳本目錄
  • 選項:.\\yourscript.ps1 -arg1 值 -arg2 值 -arg3 值

在此處輸入圖片說明

就是這樣。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM