簡體   English   中英

Powershell 不會使用 nssm 啟動或停止 Windows 服務

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

我有一個在 powershell 中制作的腳本,我正在使用 nssm 創建作為每“x”時間執行的服務,但是在啟動服務時它會生成錯誤並且不執行。 我擁有完全的管理員權限,我什至嘗試以管理員身份運行 PowerShell 但沒有成功。

如果我直接運行腳本它可以工作,但是使用 nssm 它不起作用。

發生的錯誤是這樣的:

Start-Service: 服務'nice (nice)' 啟動失敗。 在 C:\Program Files\NICE Systems\nssm.ps1:10 char:14

  • 啟動服務 <<<< $ serviceName
    • CategoryInfo: OpenError: (System.ServiceProcess.ServiceController: ServiceController) [Start-Service], ServiceCommandException
    • fullyQualifiedErrorId:StartServiceFailed,Microsoft.PowerShell.Commands.StartServiceCommand

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

$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
    }

我相信我有類似的情況,我無法在 Bamboo 文件系統運行時備份它。 我的備份通過遠程 PowerShell 從 rundeck 服務器執行,即使用戶具有本地管理員權限,它也無法使用 NSSM 停止和啟動服務。 所以我使用這個 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"

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM