powershell - 使用 powershell,如何将 "Log on as service"授予帐户?

标签 powershell

我正在尝试使用 powershell 配置帐户凭据,但我需要授予帐户“作为服务登录”的权限才能使其正常工作。如何在 powershell 中执行此操作?

最佳答案

下面的 Powershell 脚本将授予 SeServiceLogonRight 在由 computerName 指定的主机上到由 username 指定的用户(脚本摘自此处: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

关于powershell - 使用 powershell,如何将 "Log on as service"授予帐户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/313831/

相关文章:

powershell - 在 PowerShell 中重用正在运行的 PuTTY 代理(选美)

powershell - 如何在管道中加入数组

c# - Powershell Cmdlet - 为什么一次处理一个 string[] 参数元素?

powershell - -ErrorAction Stop 在 Copy-Item 中不起作用

打包在批处理文件中的PowerShell脚本只执行了一半

当有大量文件时,Powershell 在 "get-childitem . -directory -recurse"中运行缓慢

powershell - 自动化Windows 7轻松访问设置 “Turn off unnecessary animations”

Powershell - 输出到屏幕和日志文件

powershell - 使用 PowerShell 的日期差异

powershell - 打开或关闭滚动锁定