大数跨境

将PowerShell脚本注册为Windows系统服务

将PowerShell脚本注册为Windows系统服务 云驰纳联
2025-01-22
2

1、编写GetUser.ps1脚本示例,内容如下:

while ($True){get-date |Out-File D:\Operations\Scripts\date.txt -AppendSleep -Seconds 2}

2、登录Http://nssm.cc,下载nssm程序,将其放在目录(D:\Operations\Tools下,然后通过PowerShell命令将脚本GetUser.ps1注册为系统服务,如下:

#将PowerShell脚本安装为Windows服务$ServiceName = "GetUser"  #指定服务名称$PSPath = (Get-Command powershell).Source$PSScriptPath = "D:\Operations\Scripts\GetUser.ps1"  #指定脚本路径$PSargs = '-ExecutionPolicy bypass -NoProfile -File "{0}"' -f $PSScriptPath#通过nssm安装服务D:\Operations\Tools\nssm.exe install $ServiceName $PSPath $PSargs
Get-Service $ServiceName #获取服务状态Start-Service $ServiceName  #启动服务#删除服务D:\Operations\Tools\nssm.exe remove $ServiceName

3、通过图形化界面注册服务

nssm常用命令:


【声明】内容源于网络
0
0
云驰纳联
北京云驰纳联官方
内容 0
粉丝 0
云驰纳联 北京云驰纳联官方
总阅读0
粉丝0
内容0