简体   繁体   English

Powershell 不会使用 nssm 启动或停止 Windows 服务

[英]Powershell will not start or stop Windows service with nssm

i have a script made in powershell and i am using nssm to create as a service to be executed every "x" time, however when starting the service it generates error and does not execute.我有一个在 powershell 中制作的脚本,我正在使用 nssm 创建作为每“x”时间执行的服务,但是在启动服务时它会生成错误并且不执行。 I have full administrator rights and I even tried to run PowerShell as an administrator without success.我拥有完全的管理员权限,我什至尝试以管理员身份运行 PowerShell 但没有成功。

If I run the script directly it works, however using nssm it is not working.如果我直接运行脚本它可以工作,但是使用 nssm 它不起作用。

The error that happens is this:发生的错误是这样的:

Start-Service: Service 'nice (nice)' start failed. Start-Service: 服务'nice (nice)' 启动失败。 At C: \ Program Files \ NICE Systems \ nssm.ps1: 10 char: 14在 C:\Program Files\NICE Systems\nssm.ps1:10 char:14

  • Start-Service <<<< $ serviceName启动服务 <<<< $ serviceName
    • CategoryInfo: OpenError: (System.ServiceProcess.ServiceController: ServiceController) [Start-Service], ServiceCommandException CategoryInfo: OpenError: (System.ServiceProcess.ServiceController: ServiceController) [Start-Service], ServiceCommandException
    • FullyQualifiedErrorId: StartServiceFailed, Microsoft.PowerShell.Commands.StartServiceCommand fullyQualifiedErrorId:StartServiceFailed,Microsoft.PowerShell.Commands.StartServiceCommand

nssm.ps1 nssm.ps1

$nssm = (Get-Command nssm.exe).Definition
$serviceName = 'nice'
$powershell = (Get-Command powershell.exe).Definition
$scriptPath = 'C:\Program Files\NICE Systems\script_delecao.ps1'
$arguments = '-ExecutionPolicy Bypass -NoProfile -File "{0}"' -f $scriptPath
& $nssm install $serviceName $powershell $arguments
& $nssm status $serviceName
Start-Service $serviceName
Get-Service $serviceName

script_delecao.ps1 script_delecao.ps1

$logPath = "C:\Program Files\NICE Systems\Logs\*\Archive\*"
    
    # -------------------------------------------------------------------------------------------
    # SET $NDAYS WITH THE NUMBER OF DAYS TO KEEP IN LOG FOLDER.
    $nDays = 180
    
    # -------------------------------------------------------------------------------------------
    # SET $EXTENSIONS WITH THE FILE EXTENSION TO DELETE.
    # YOU CAN COMBINE MORE THAN ONE EXTENSION: "*.LOG, *.TXT,"
    
    $Extensions = "*.log*"
    # -------------------------------------------------------------------------------------------
    # PAY ATTENTION! IF YOU COMBINE MORE THAN ONE LOG PATH AND EXTENSIONS,
    # MAKE SURE THAT YOU ARE NOT REMOVING FILES THAT CANNOT BE DELETED 
    # -------------------------------------------------------------------------------------------
    $PathDelete = "C:\Program Files\NICE Systems\Delecoes"
    
    while ($true) {
    
        If(!(test-path $PathDelete))
        {
              New-Item -ItemType Directory -Force -Path $PathDelete
        }
    
        $LogDate = (Get-Date).ToString("dd_MM_yyyy")
        $DateTime = (Get-Date).ToString("yyy-MM-ddThh:mm:ss")
    
        $Files = Get-Childitem $LogPath -Include $Extensions -Recurse | Where `
        {$_.LastWriteTime -le (Get-Date).AddDays(-$nDays)}
    
        foreach ($File in $Files) 
        {
            if ($File -ne $NULL)
            {
                $Log = $DateTime + " - O arquivo " + $File + " foi deletado "
                $Log | Out-File -Append $PathDelete\DeleteLogFile_$LogDate.log
                Remove-Item $File.FullName| out-null
            }
        }
    
      # Add a sleep at the end of the loop to prevent the script from eating
      # too much CPU time
      $Log = $DateTime + " FINAL DO ARQUIVO "
      $Log | Out-File -Append $PathDelete\DeleteLogFile_$LogDate.log
      Start-Sleep -Seconds 300
    }

I believe I have a similar scenario where I cannot back-up Bamboo file system while it's running.我相信我有类似的情况,我无法在 Bamboo 文件系统运行时备份它。 My back-up executes from a rundeck server via Remote PowerShell, and even though the user has local admin rights it cannot stop and start services using NSSM.我的备份通过远程 PowerShell 从 rundeck 服务器执行,即使用户具有本地管理员权限,它也无法使用 NSSM 停止和启动服务。 So I use this function to run the command elevated所以我使用这个 function 来运行提升的命令

ELEVAT "nssm stop bamboo"
tar --exclude=./logs --exclude=./temp --exclude=*.log --exclude=*.jar --verbose -czf E:\dropfolder\bamboo-home.tar.gz --directory=E:\bamboo-home .
ELEVAT "nssm start bamboo"

the function itself... function 本身...

function ELEVAT ($command) {
    $scriptBlock = [scriptblock]::Create($command)
    configuration elevated {
        Import-DscResource -ModuleName 'PSDesiredStateConfiguration'
        Set-StrictMode -Off
        Node localhost {
            Script execute {
                SetScript = $scriptBlock
                TestScript = {
                    if (([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
                        Write-Verbose "Verified Elevated Session"
                        return $false
                    } else {
                        Write-Verbose "Not an Elevated Session!"
                        exit 9996
                    }
                }
                GetScript = { return @{ 'Result' = 'RUN' } }
            }
        }
    }
    $mof = elevated
    Start-DscConfiguration ./elevated -Wait -Verbose -Force
    if ( $error ) { Write-Host "[ELEVAT][WARN] `$Error[] = $Error" ; $Error.clear() 
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Powershell中使用NSSM安装服务 - Installing service with NSSM in powershell Powershell 脚本使用 S.no 停止和启动 windows 服务 - Powershell script to Stop and start windows Service using S.no 使用Powershell在Azure VM上启动和停止Windows服务 - Start and stop windows service on a Azure VM using powershell 无法使用 NSSM 在 window 10 中启动服务 - Unable to start service in window 10 by using NSSM 使用Powershell停止服务,重新启动并启动服务 - Stop the Service, Restart and Start the service using Powershell Powershell - 使用参数启动Windows服务 - Powershell - Start Windows service with a parameter 对于 dotnetcore 3.1 Windows 服务如何使用 powershell 脚本 5.1 停止、卸载、安装和启动服务 - For dotnetcore 3.1 Windows service how to stop, uninstall, install and start the service with a powershell script 5.1 使用Powershell停止/启动Microsoft Windows群集角色 - Stop/Start Microsoft Windows Cluster roles with Powershell 如何使用PowerShell-Windows 2008和提示输入凭据在远程服务器上启动/停止服务? - How to start/stop a service on a remote server using PowerShell - Windows 2008 & prompt for credentials? Powershell 脚本停止/启动 IIS 在 Windows 10 - Powershell Script to stop/start IIS in Windows 10
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM