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" 
} 
相關問題