1

我使用 Procrun 创建了一个 java 应用程序(.jar)作为 Windows 服务。当我使用批处理(.bat)文件时,此服务已安装并成功运行。但我有使用 windows powerShell 创建相同服务的要求。

当我使用 PowerShell 时,安装了服务但无法启动我在 Windows 事件查看器中检查的服务,它显示为“功能不正确”谁能告诉我这可能是什么原因。

这是我在 PowerShell 脚本中用于安装 Windows 服务的字符串

$CmdInstall=@'

--Description="$Description"
--DisplayName="$DisplayName"
--Install="$DAEMON"
--Startup=auto
--Type=
--DependsOn=
--Environment=
--User=
--Password=
--ServiceUser=
--ServicePassword=
--LibraryPath=
--JavaHome
--Jvm=auto
--JvmOptions=-Xmx1024M
--Classpath=server.jar
--JvmMs=
--JvmMx=
--JvmSs=
--StartMode=jvm
--StartImage=
--StartPath=
--StartClass=at.mrdevelopment.esl.server.Server
--StartMethod=main
--StartParams=
--StopMode=jvm
--StopImage=
--StopPath=
--StopClass=at.mrdevelopment.esl.server.ServerServiceStarter
--StopMethod=stop
--StopParams=
--StopTimeout=120
--LogPath=$LogPath
--LogPrefix=$InstanceName
--LogLevel=DEBUG
--LogJniMessages=
--StdOutput=auto
--StdError=auto
--PidFile=${InstanceName}.pid
'@

任何帮助,将不胜感激。

这是我使用的 PowerShell 脚本。

  #region Parameters
   [CmdletBinding(SupportsShouldProcess=$true)]
param(
   [ValidateNotNullOrEmpty()]
   [string]$Action="//IS"
   ,
   [ValidateNotNullOrEmpty()]
   [string]$ServiceName="//ESL_SERVICE"
   ,
   [ValidateNotNullOrEmpty()]
   [string]$DisplayName="ESL_SERVICE"
   ,
   [ValidateNotNullOrEmpty()]
   [string]$Description="ESL Service"
   ,
   [ValidateNotNullOrEmpty()]
   [string]$InstanceName="server.jar"
   ,
   [ValidateNotNullOrEmpty()]
   [string]$LogPath='C:\Apachelogs'
   ,
   [string]$Pause=60
)
#endregion

#region Main

$CmdInstall=@'

--Description="$Description"
--DisplayName="$DisplayName"
--Install="$DAEMON"
--Startup=auto
--Type=
--DependsOn=
--Environment=
--User=
--Password=
--ServiceUser=
--ServicePassword=
--LibraryPath=
--JavaHome
--Jvm=auto
--JvmOptions=-Xmx1024M
--Classpath=server.jar
--JvmMs=
--JvmMx=
--JvmSs=
--StartMode=jvm
--StartImage=
--StartPath=
--StartClass=at.mrdevelopment.esl.server.Server
--StartMethod=main
--StartParams=
--StopMode=jvm
--StopImage=
--StopPath=
--StopClass=at.mrdevelopment.esl.server.ServerServiceStarter
--StopMethod=stop
--StopParams=
--StopTimeout=120
--LogPath=$LogPath
--LogPrefix=$InstanceName
--LogLevel=DEBUG
--LogJniMessages=
--StdOutput=auto
--StdError=auto
--PidFile=${InstanceName}.pid
'@

$DAEMON_HOME         = "C:\imagotag\server"
$DAEMON              = "$DAEMON_HOME\prunsrv_64.exe"                                               
$ESL_HOME            = "C:\imagotag\server"  

$CmdArgsDict=@{}

$CmdArgsDict.Add('//IS',      "$Action$ServiceName $CmdInstall")

$CmdArgs             = $CmdArgsDict[$action]
# Convert backslashes in the paths to java-friendly forward slashes
$CmdArgs             = $CmdArgs -replace "\\","/"
# Variable interpolation: expand embedded variables references (need to call this twice)
$CmdArgs             = $ExecutionContext.InvokeCommand.ExpandString($CmdArgs)
$CmdArgs             = $ExecutionContext.InvokeCommand.ExpandString($CmdArgs)
# Split on newlines to convert to an array of lines
$CmdArgsString       = $CmdArgs -split "`n"
# Convert array of lines into a string
$CmdArgsString       = "$CmdArgsString"


#--- Execute the command
if ($PSCmdlet.ShouldProcess(
    "`n$DAEMON`n$CmdArgs","Manage ESL Service"
))
{
   "$DAEMON $CmdArgsString"

   $p=Start-Process "$DAEMON" `
         -ArgumentList "$CmdArgsString" `
         -Wait `
         -NoNewWindow `
         -PassThru

   $rc = $p.ExitCode
   "`nExit Code: $rc"
}
#endregion

我的 PowerShell 脚本是 TestPS.ps1,我像这样执行脚本

.\TestPS.ps1 //IS
4

2 回答 2

0

您的问题来自配置错误的服务。这来自prunsrv.exe的参数问题。

改行后脚本运行良好

$CmdArgsString       = $CmdArgs -split "`n"

$CmdArgsString       = $CmdArgs -split "`r`n"
于 2018-01-19T14:39:36.350 回答
0

要创建服务,请在 PS 中使用以下 cmdlet:

$username = "Username"
$password = "password"
$secstr = New-Object -TypeName System.Security.SecureString
$password.ToCharArray() | ForEach-Object {$secstr.AppendChar($_)}
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $secstr

new-service -Name [INSERT SERVICE NAME] -DisplayName "[INSERT DISPLAY NAME]" -Description "[INSERT DESCRIPTION]" -BinaryPathName "[INSERT BINARY PATH]" -StartupType Manual -Credential $cred

如果您希望远程执行它,请使用以下功能:

function Install-Service(
    [string]$serviceName = $(throw "serviceName is required"), 
    [string]$targetServer = $(throw "targetServer is required"),
    [string]$displayName = $(throw "displayName is required"),
    [string]$physicalPath = $(throw "physicalPath is required"),
    [string]$userName = $(throw "userName is required"),
    [string]$password = "",
    [string]$startMode = "Automatic",
    [string]$description = "",
    [bool]$interactWithDesktop = $false
)
{
    # can't use installutil; only for installing services locally
    #[wmiclass]"Win32_Service" | Get-Member -memberType Method | format-list -property:*    
    #[wmiclass]"Win32_Service"::Create( ... )        

    # todo: cleanup this section 
    $serviceType = 16          # OwnProcess
    $serviceErrorControl = 1   # UserNotified
    $loadOrderGroup = $null
    $loadOrderGroupDepend = $null
    $dependencies = $null

    # description?
    $params = `
        $serviceName, `
        $displayName, `
        $physicalPath, `
        $serviceType, `
        $serviceErrorControl, `
        $startMode, `
        $interactWithDesktop, `
        $userName, `
        $password, `
        $loadOrderGroup, `
        $loadOrderGroupDepend, `
        $dependencies `

    $scope = new-object System.Management.ManagementScope("\\$targetServer\root\cimv2", `
        (new-object System.Management.ConnectionOptions))
    "Connecting to $targetServer"
    $scope.Connect()
    $mgt = new-object System.Management.ManagementClass($scope, `
        (new-object System.Management.ManagementPath("Win32_Service")), `
        (new-object System.Management.ObjectGetOptions))

    $op = "service $serviceName ($physicalPath) on $targetServer"    
    "Installing $op"
    $result = $mgt.InvokeMethod("Create", $params)    
    Test-ServiceResult -operation "Install $op" -result $result
    "Installed $op"

    "Setting $serviceName description to '$description'"
    Set-Service -ComputerName $targetServer -Name $serviceName -Description $description
    "Service install complete"
}

这是远程安装 Windows 服务的指南

注意:确保您在提升模式下运行所有​​内容。

希望能帮助到你。

于 2017-02-27T15:25:10.687 回答