PowerShell Script to provide Group Policy (local security policy) for a Windows group / user

 A generic PowerShell script to provide group policy security setting to user / group - defaults to "SeInteractiveLogonRight" and tested against the same. 

Useful in scenario wherein you want to execute a windows scheduler task and a domain user must be attached to a windows group and then the windows group can be given a security setting like "SeInteractiveLogonRight" or logon locally permission for the domain account. Or, in other words, the domain account has now capability to access the computer resources and the domain resources.

Should be tested against others in this list:

Group Policy SettingConstant Name
Access Credential Manager as a trusted callerSeTrustedCredManAccessPrivilege
Access this computer from the networkSeNetworkLogonRight
Act as part of the operating systemSeTcbPrivilege
Add workstations to domainSeMachineAccountPrivilege
Adjust memory quotas for a processSeIncreaseQuotaPrivilege
Allow log on locallySeInteractiveLogonRight
Allow log on through Remote Desktop ServicesSeRemoteInteractiveLogonRight
Back up files and directoriesSeBackupPrivilege
Bypass traverse checkingSeChangeNotifyPrivilege
Change the system timeSeSystemtimePrivilege
Change the time zoneSeTimeZonePrivilege
Create a pagefileSeCreatePagefilePrivilege
Create a token objectSeCreateTokenPrivilege
Create global objectsSeCreateGlobalPrivilege
Create permanent shared objectsSeCreatePermanentPrivilege
Create symbolic linksSeCreateSymbolicLinkPrivilege
Debug programsSeDebugPrivilege
Deny access to this computer from the networkSeDenyNetworkLogonRight
Deny log on as a batch jobSeDenyBatchLogonRight
Deny log on as a serviceSeDenyServiceLogonRight
Deny log on locallySeDenyInteractiveLogonRight
Deny log on through Remote Desktop ServicesSeDenyRemoteInteractiveLogonRight
Enable computer and user accounts to be trusted for delegationSeEnableDelegationPrivilege
Force shutdown from a remote systemSeRemoteShutdownPrivilege
Generate security auditsSeAuditPrivilege
Impersonate a client after authenticationSeImpersonatePrivilege
Increase a process working setSeIncreaseWorkingSetPrivilege
Increase scheduling prioritySeIncreaseBasePriorityPrivilege
Load and unload device driversSeLoadDriverPrivilege
Lock pages in memorySeLockMemoryPrivilege
Log on as a batch jobSeBatchLogonRight
Log on as a serviceSeServiceLogonRight
Manage auditing and security logSeSecurityPrivilege
Modify an object labelSeRelabelPrivilege
Modify firmware environment valuesSeSystemEnvironmentPrivilege
Obtain an impersonation token for another user in the same sessionSeDelegateSessionUserImpersonatePrivilege
Perform volume maintenance tasksSeManageVolumePrivilege
Profile single processSeProfileSingleProcessPrivilege
Profile system performanceSeSystemProfilePrivilege
Remove computer from docking stationSeUndockPrivilege
Replace a process level tokenSeAssignPrimaryTokenPrivilege
Restore files and directoriesSeRestorePrivilege
Shut down the systemSeShutdownPrivilege
Synchronize directory service dataSeSyncAgentPrivilege
Take ownership of files or other objectsSeTakeOwnershipPrivilege

#######################################################

<#

.Synopsis

  Grant interactive logon to a local user/group.

.Parameter grpname

  Defines the username/grpname under which the service should run.

.Example

  Usage:

  .\GrantSecurityRight.ps1  -grpname "logonlocally"

#>

param(  

  [string] $grpname,

  [string] $logonright="SeInteractiveLogonRight"

)

 


  $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 $logonright to user group: $grpname."

    

    $sid = ((New-Object System.Security.Principal.NTAccount($grpname)).Translate([System.Security.Principal.SecurityIdentifier])).Value

    secedit /export /cfg $export

    $sids = (Select-String $export -Pattern "$logonright").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

Write-Host "Granted $logonright to user group: $grpname."

  } catch {

 Write-Host "Failed to grant $logonright to user group: $grpname. $_"  

      exit 1

  }

#######################################################

Calling Script:

param(

  [Parameter(Mandatory=$true)]

  [string] $GrpName,

  [Parameter(Mandatory=$true)]

  [string] $GrpDescription  

)


# create local group: LogonLocally

New-LocalGroup -Name $GrpName -Description $GrpDescription

Out-Log "[INFO] $GrpName created"

# grant security privilege (by default, local logon) to the group

.\GrantSecurityRight.ps1 -grpname $GrpName


#######################################################

Note that a domain user is associated with a SID, which is a unique id across the domain and this id can be assigned to the group as follows:

Add-LocalGroupMember -Group $GrpName -Member $BatchAccountSid -ErrorAction Stop

Get SID for a Windows domain user:

Get-AdUser -Identity $username | select Name, SID, USerPrincipalName

Comments

Popular Posts