Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using powershell, how do I grant "Log on as service" to an account?

Tags:

powershell

I'm trying to use powershell to configure the account credentials, but I need to grant the account "Log on as a service" right in order for it to work. How can I do this in powershell?

like image 856
Jesse Weigert Avatar asked Nov 24 '08 10:11

Jesse Weigert


People also ask

How do I grant a user login as a service?

Sign in with administrator privileges to the computer from which you want to provide Log on as Service permission to accounts. Go to Administrative Tools, click Local Security Policy. Expand Local Policy, click User Rights Assignment. In the right pane, right-click Log on as a service and select Properties.

How do I login as another user in PowerShell?

Then you can right-click on the PowerShell icon, which shows you an option as Windows PowerShell. Hovering over that options, Click Shift and right-click together to open another menu. You can choose Run as Different User from the new menu. Then a different popup would be opened, as shown in the below image.

How do I run a service account in PowerShell?

Open the Services snapin by executing services. msc . Find the PowerShell Universal service and right click it and then click Properties. Click the Log On tab and enter the credentials for the service account.

How do I know if an account has logged as a service rights?

Open Local Security Policy. In the left pane, click Security Settings ►Local Policies►User Rights Assignments. In the right-hand pane, find the policy Log on as a service. Right-click Logon as a service, and then click Properties.


2 Answers

The Powershell script below will grant the SeServiceLogonRight on the host specified by computerName to the user specified by username (the script is an excerpt from here: https://gist.github.com/grenade/8519655):

<# .Synopsis   Grant logon as a service right to the defined user. .Parameter computerName   Defines the name of the computer where the user right should be granted.   Default is the local computer on which the script is run. .Parameter username   Defines the username under which the service should run.   Use the form: domain\username.   Default is the user under which the script is run. .Example   Usage:   .\GrantSeServiceLogonRight.ps1 -computerName hostname.domain.com -username "domain\username" #> param(   [string] $computerName = ("{0}.{1}" -f $env:COMPUTERNAME.ToLower(), $env:USERDNSDOMAIN.ToLower()),   [string] $username = ("{0}\{1}" -f $env:USERDOMAIN, $env:USERNAME) ) Invoke-Command -ComputerName $computerName -Script {   param([string] $username)   $tempPath = [System.IO.Path]::GetTempPath()   $import = Join-Path -Path $tempPath -ChildPath "import.inf"   if(Test-Path $import) { Remove-Item -Path $import -Force }   $export = Join-Path -Path $tempPath -ChildPath "export.inf"   if(Test-Path $export) { Remove-Item -Path $export -Force }   $secedt = Join-Path -Path $tempPath -ChildPath "secedt.sdb"   if(Test-Path $secedt) { Remove-Item -Path $secedt -Force }   try {     Write-Host ("Granting SeServiceLogonRight to user account: {0} on host: {1}." -f $username, $computerName)     $sid = ((New-Object System.Security.Principal.NTAccount($username)).Translate([System.Security.Principal.SecurityIdentifier])).Value     secedit /export /cfg $export     $sids = (Select-String $export -Pattern "SeServiceLogonRight").Line     foreach ($line in @("[Unicode]", "Unicode=yes", "[System Access]", "[Event Audit]", "[Registry Values]", "[Version]", "signature=`"`$CHICAGO$`"", "Revision=1", "[Profile Description]", "Description=GrantLogOnAsAService security template", "[Privilege Rights]", "$sids,*$sid")){       Add-Content $import $line     }     secedit /import /db $secedt /cfg $import     secedit /configure /db $secedt     gpupdate /force     Remove-Item -Path $import -Force     Remove-Item -Path $export -Force     Remove-Item -Path $secedt -Force   } catch {     Write-Host ("Failed to grant SeServiceLogonRight to user account: {0} on host: {1}." -f $username, $computerName)     $error[0]   } } -ArgumentList $username 
like image 98
grenade Avatar answered Oct 03 '22 05:10

grenade


This is how I solved it:

Based on: this article

You can download Carbon from here

First import Carbon module as follows:

Import-Module -Name $Path_To_Carbon -Global -Prefix CA  [array]$UserPrivileges = Get-CAPrivileges -Identity $UserName; [bool]$LogOnAsAServiceprivilegeFound = $false;  if ($UserPrivileges.Length > 0) {     if ($UserPrivileges -contains "SeServiceLogonRight")     {         $LogOnAsAServiceprivilegeFound = $true;     } }  if ($LogOnAsAServiceprivilegeFound -eq $false) {     Grant-CAPrivilege -Identity $UserName "SeServiceLogonRight" } 
like image 27
Jupaol Avatar answered Oct 03 '22 06:10

Jupaol