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

后端 未结 8 864
感情败类
感情败类 2020-12-15 17:40

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

8条回答
  •  执念已碎
    2020-12-15 18:17

    This is not pure PowerShell but at least you do not need a third party tool.
    Everything is already on your computer and works from the command line.

    #Requires -RunAsAdministrator
    
    #The SID you want to add
    $AccountSid = 'S-1-5-21-1234567890-1234567890-123456789-500'
    
    $ExportFile = 'c:\temp\CurrentConfig.inf'
    $SecDb = 'c:\temp\secedt.sdb'
    $ImportFile = 'c:\temp\NewConfig.inf'
    
    #Export the current configuration
    secedit /export /cfg $ExportFile
    
    #Find the current list of SIDs having already this right
    $CurrentServiceLogonRight = Get-Content -Path $ExportFile |
        Where-Object -FilterScript {$PSItem -match 'SeServiceLogonRight'}
    
    #Create a new configuration file and add the new SID
    $FileContent = @'
    [Unicode]
    Unicode=yes
    [System Access]
    [Event Audit]
    [Registry Values]
    [Version]
    signature="$CHICAGO$"
    Revision=1
    [Profile Description]
    Description=GrantLogOnAsAService security template
    [Privilege Rights]
    {0}*{1}
    '@ -f $(
            if($CurrentServiceLogonRight){"$CurrentServiceLogonRight,"}
            else{'SeServiceLogonRight = '}
        ), $AccountSid
    
    Set-Content -Path $ImportFile -Value $FileContent
    
    #Import the new configuration 
    secedit /import /db $SecDb /cfg $ImportFile
    secedit /configure /db $SecDb
    

提交回复
热议问题