我创建了下面的脚本来检查我的应用程序的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
运行服务脚本:service.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