首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Windows服务无法启动“错误5:访问被拒绝”

Windows服务无法启动“错误5:访问被拒绝”
EN

Stack Overflow用户
提问于 2014-05-15 10:34:32
回答 1查看 6.6K关注 0票数 1

我在努力解决一个问题。所以我创建了一个Windows服务,制作了安装脚本,并在windows中注册了它。我正在使用我在同一脚本中创建的自定义帐户,并授予它使用碳库“作为服务登录”的权限,以便能够从PowerShell (在“通过Powershell设置或授予用户登录为服务的权限”下面描述这里 )。

在启动服务(手动或通过cmd)时,我得到“错误5:访问被拒绝”错误。我不明白为什么,我甚至尝试给整个C:\驱动器完全的帐户权限。

下面是我如何创建用户

代码语言:javascript
复制
net user MyServiceAccount MyPassword /add /expires:never /passwordchg:no

下面是我如何授予它作为服务登录的权限

代码语言:javascript
复制
$Identity = "MyServiceAccount"
$privilege = "SeServiceLogonRight" 
$CarbonDllPath = $PSScriptRoot + "\Carbon.dll"
 
[Reflection.Assembly]::LoadFile($CarbonDllPath)
[Carbon.Lsa]::GrantPrivileges( $Identity, $privilege )

(登录服务权限似乎是有效的,因为在此之前,它因有关该问题的错误而失败)我已经阅读了大量关于该主题的帖子,但无法解决这个问题。所以,我的问题是:是什么导致访问被拒绝的错误?

更新

尝试在管理员帐户(登录为.)下运行它,它做同样的事情--拒绝访问。EventLog除了“MonitoringService服务由于以下错误而无法启动:访问被拒绝”之外什么都没有。系统事件日志中的消息。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-05-16 12:45:24

好的,所以我已经通过对服务使用不同的安装例程来解决我的问题。这似乎是一个安装问题,而不是与用户权利有关的问题。下面是一个工作脚本的示例,以防有人感兴趣:

代码语言:javascript
复制
# ALISE MONITORING SERVICE INSTALLATION #

$service_fullname = "Alise.MonitoringService"
$username = ".\AliseMonitoring"
$password = "mypassword"
$pause = 0
$exeName = "MonitoringService.exe"

$ErrorActionPreference = "Stop"
$nl = [Environment]::NewLine

$dir = Split-Path $MyInvocation.MyCommand.Path
$service_path = join-path $dir "$exeName"
$service_path = resolve-path $service_path


Write-Host $nl
Write-Host "Service name: $service_fullname" -foregroundcolor green
Write-Host "Service logon identity: $username" -foregroundcolor green
Write-Host "Service installation path: $service_path" -foregroundcolor green
Write-Host $nl

Write-Host "Creating user if necessary..." -foregroundcolor green
start-process create_user.bat -Wait
Write-Host "Granting user log on as service rights..." -foregroundcolor green
$PSScriptRoot = Split-Path -Parent -Path $MyInvocation.MyCommand.Definition
$Identity = "AliseMonitoring"
$privilege = "SeServiceLogonRight" 
$CarbonDllPath = $PSScriptRoot + "\Carbon.dll"
[Reflection.Assembly]::LoadFile($CarbonDllPath)
[Carbon.Lsa]::GrantPrivileges( $Identity, $privilege )
Write-Host $nl

$service = Get-WmiObject -Class Win32_Service -Filter "Name = '$service_fullname'"
if ($service -ne $null) 
{ 
    Write-Host "Service already exists, attempting stop and delete:" -foregroundcolor green
    Write-Host "Stop service $service_fullname..."
    $service | stop-service
    Write-Host "Delete service $service_fullname..."
    $service.Delete()
    Write-Host "Service $service_fullname deleted." 
    Write-Host $nl
}

Write-Host $nl

Write-Host "Registering service $service_fullname for $service_path ..." -foregroundcolor green
New-Service -Name $service_fullname -BinaryPathName $service_path -DisplayName $service_fullname -Description "Alise monitoring serviss." -StartupType Automatic
$service = Get-WmiObject -Class Win32_Service -Filter "Name = '$service_fullname'"
Write-Host "Service registred."

$res = sc.exe config $service_fullname obj= $username password= $password

if ($LASTEXITCODE -ne 0)
{
   Write-Host "username: $username password: $password" -foregroundcolor green
   throw $res
}

#Event log and source registration
Write-Host $nl
Write-Host "Registering event source Alise.MonitoringService" -foregroundcolor green
if ([system.diagnostics.eventlog]::SourceExists("Alise.MonitoringService") -eq $false) 
{
  [system.diagnostics.eventlog]::CreateEventSource("Alise.MonitoringService", "Alise")
  Write-Host "Created event source: Alise.MonitoringService"
}
else
{
    Write-Host "Event source Alise.MonitoringService allready exists."
}

Write-Host $nl
Write-Host "Starting service..." -foregroundcolor green
Start-Service $service_fullname
Write-Host "Service started!" -foregroundcolor green

if ($pause -eq 1)
{
    read-host -prompt "Press enter to exit"
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23675759

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档