如何将 PowerShell 脚本作为服务运行?

Lea*_*tos 6 powershell netstat

我创建了下面的脚本来检查应用程序的端口 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
Run Code Online (Sandbox Code Playgroud)

脚本服务.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
Run Code Online (Sandbox Code Playgroud)

小智 5

您可以从 nssm.cc 下载 NSSM:

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

  2. 设置信息:

  • 路径:Powershell路径
  • 启动目录:您的脚本目录
  • 选项: .\yourscript.ps1 -arg1 值 -arg2 值 -arg3 值

在此输入图像描述

就是这样。


Ben*_*est 2

我原来的答案没有考虑到你仍然需要实现服务控制接口,而它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
}
Run Code Online (Sandbox Code Playgroud)

要将您的脚本注册为 PowerShell 服务,您可以使用以下 PowerShell 代码(注意,如果您使用 来安装Chocolateynssm则已经在 上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
Run Code Online (Sandbox Code Playgroud)

您的服务现在应该已安装并且可以像任何其他 Windows 服务一样进行控制。它的工作方式是不是直接注册powershell.exe为服务,nssm.exe而是注册为服务可执行文件,这确实实现了正确的服务控制处理程序,然后运行您为此服务配置的任何程序(在本例中,调用您的脚本和powershell.exe)。

  • 您无法直接将 PowerShell 脚本作为服务运行,因为“powershell.exe”不是服务可执行文件。调度程序中的所有任务如何以及为何自动删除?任务计划程序是操作系统的核心组件。解决该问题并作为任务运行,或者使用其他可以安排程序运行的工具。 (2认同)