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

时间:2014-05-15 10:34:32

标签: powershell windows-services access-denied user-permissions

我在这里遇到问题。 所以我创建了一个Windows服务,制作了安装脚本并在Windows上注册了它。我正在使用我在同一个脚本中创建的自定义帐户,并使用Carbon库授予其“作为服务登录”的权限,以便能够从PowerShell执行此操作(在“设置或授予”下描述here用户通过Powershell登录作为服务权“”

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

以下是我创建用户的方式

net user MyServiceAccount MyPassword /add /expires:never /passwordchg:no

以下是我授予其作为服务登录的权限

$Identity = "MyServiceAccount"
$privilege = "SeServiceLogonRight" 
$CarbonDllPath = $PSScriptRoot + "\Carbon.dll"
 
[Reflection.Assembly]::LoadFile($CarbonDllPath)
[Carbon.Lsa]::GrantPrivileges( $Identity, $privilege )

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

更新

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

1 个答案:

答案 0 :(得分:0)

好的,所以我设法通过使用不同的服务安装例程来解决我的问题。它似乎是一个安装问题而不是与用户权限相关的问题。以下是任何人感兴趣的工作脚本示例:

# 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"
}