1

我在这里遇到问题。 所以我创建了一个Windows服务,创建了安装脚本并将它注册到了Windows中。我使用的是我在同一脚本中创建的自定义帐户,并授予其使用碳库“登录为服务”的权利,以便能够从PowerShell中执行此操作(描述为here“设置或授予用户登录为A通过Powershell服务“)Windows服务无法启动“错误5:访问被拒绝”

在启动服务(手动和通过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) 

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

更新

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

+0

可能访问被拒绝的错误是与一些注册表项而不是文件相比引发的。 – Vesper

+0

@Vesper有没有办法找出答案?我的意思是,我怎样才能知道它可能会失败的注册表键? –

+0

您的应用程序应该提供日志和/或其他调试信息,以便它不会在堆栈跟踪之上出现原始0xC0000005。如果你在控制它的代码,确保你有任何外部操作的尝试,以便它不会最终陷入恐慌。如果没有,请使用Sysinternals Process Monitor,regmon和filemon工具记录注册表活动并调试应用程序。 – Vesper

回答

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" 
} 
相关问题