【Windows Server脚本自动化部署nssm】

内容分享3天前发布
0 0 0

本文章所测试环境为windows server 2019,可在内网环境下部署。

1. nssm简介

NSSM​​(N​​on-​​S​​ucking ​​S​​ervice ​​M​​anager)是一款免费开源的 Windows 实用工具,它的核心功能是​​将任何普通的可执行程序(如 .exe、.bat 脚本)安装为 Windows 服务​​。

2. nssm包下载

nssm官方下载地址

NSSM – the Non-Sucking Service Manager

【Windows Server脚本自动化部署nssm】

3. nssm脚本一键安装

将下载的nssm压缩包放到指定目录,无需解压。

创建脚本

该脚本需要用powsershell执行。使用时,使用管理员权限通过windows powershell ISE打开。

【Windows Server脚本自动化部署nssm】

执行结果

【Windows Server脚本自动化部署nssm】

检查nssm是否部署成功

cmd中输入nssm,展示出版本和命令提示则表示安装成功。

【Windows Server脚本自动化部署nssm】

4. 附上完整脚本

注意替换压缩包路径、解压目录为实际路径。



# 定义 nssm 压缩包路径
$nssmZipPath = "C:UsersAdministratorDesktop	est
ssm-2.24.zip"  # 替换为实际的 nssm 压缩包路径
 
# 定义解压目录
$extractDir = "C:UsersAdministratorDesktop	est
ssm-2.24"  # 替换为实际的解压目录
 
# 定义 nssm 可执行文件路径
$nssmExePath = "$extractDir
ssm-2.24win64
ssm.exe"
 
# 检查 nssm 压缩包是否存在
if (-Not (Test-Path -Path $nssmZipPath)) {
    Write-Host "nssm 压缩包未找到: $nssmZipPath" -ForegroundColor Red
    exit 1
}
 
# 创建解压目录
if (-Not (Test-Path -Path $extractDir)) {
    New-Item -ItemType Directory -Path $extractDir -Force
}
 
# 解压 nssm 压缩包
Write-Host "正在解压 nssm 压缩包..."
Expand-Archive -Path $nssmZipPath -DestinationPath $extractDir -Force
 
# 检查 nssm.exe 是否存在
if (-Not (Test-Path -Path $nssmExePath)) {
    Write-Host "nssm.exe 未找到: $nssmExePath" -ForegroundColor Red
    exit 1
}
 
# 获取当前的 PATH 环境变量
$currentPath = [System.Environment]::GetEnvironmentVariable("PATH", [System.EnvironmentVariableTarget]::Machine)
 
# 检查 nssm 目录是否已添加到 PATH
$nssmDir = Split-Path -Parent $nssmExePath
if (-Not ($currentPath -like "*$nssmDir*")) {
    # 将 nssm 目录添加到 PATH
    $newPath = "$currentPath;$nssmDir"
    [System.Environment]::SetEnvironmentVariable("PATH", $newPath, [System.EnvironmentVariableTarget]::Machine)
    Write-Host "已将 nssm 目录添加到 PATH: $nssmDir" -ForegroundColor Green
} else {
    Write-Host "nssm 目录已存在于 PATH 中: $nssmDir" -ForegroundColor Yellow
}
 
Write-Host "nssm 配置完成。"

© 版权声明

相关文章

暂无评论

none
暂无评论...