2008-11-24 30 views
9

Tôi đang cố gắng sử dụng PowerShell để định cấu hình thông tin đăng nhập tài khoản, nhưng tôi cần cấp cho tài khoản "Đăng nhập dưới dạng dịch vụ" ngay để nó hoạt động. Làm thế nào tôi có thể làm điều này trong PowerShell?Sử dụng powershell, làm thế nào để tôi cấp "Đăng nhập dưới dạng dịch vụ" cho một tài khoản?

+0

Tôi đang cố gắng để làm như vậy, trên Windows Server 2008 R2 x64 * Core *, mà không xuất xưởng với ntrights.exe (công cụ được đề cập trong bài viết được liên kết của câu trả lời). Mọi trợ giúp thêm? –

+0

một tùy chọn khác tại đây: https://code.msdn.microsoft.com/windowsapps/Add-log-on-as-a-service-a64dd63c – Rory

Trả lời

3

Đây là liên kết mà bạn cũng có thể thực hiện trong PSH. http://www.derkeiler.com/Newsgroups/microsoft.public.windowsxp.security_admin/2003-12/2865.html.

Vấn đề là không có bất kỳ API công khai nào để quản lý các cài đặt này, do đó bạn có chút khó khăn khi sử dụng các công cụ dòng lệnh được cung cấp trong ResKits.

+2

Cảm ơn bạn. Tôi đã kết thúc bằng cách sử dụng ntrights để làm điều này. ntrights.exe + r SeServiceLogonRight -u http://support.microsoft.com/?kbid=279664 –

1

PowerShell không có bất kỳ phương tiện gốc nào để làm điều này, có nghĩa là bạn có thể đang xem xét WMI hoặc ADSI - bạn có nhiều khả năng tìm thấy các ví dụ trong VBScript, vốn đã lâu hơn, mặc dù cá nhân tôi đừng nghĩ rằng tôi đã từng tìm ra cách để chỉ định quyền người dùng theo chương trình. Tuy nhiên, điều đó không có nghĩa là nó không thể được thực hiện, nhưng có thể bạn sẽ nhìn ra ngoài phạm vi của PowerShell một cách cụ thể.

10

Các kịch bản Powershell dưới đây sẽ cấp SeServiceLogonRight trên máy chủ theo quy định của ComputerName cho người sử dụng theo quy định của tên (kịch bản là một đoạn trích từ đây: 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]", "SeServiceLogonRight = *$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 
4

Đây là cách tôi đã giải quyết:

Dựa trên: this article

Bạn có thể tải Carbon from here

Đầu tiên mô-đun Carbon nhập khẩu như sau:

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" 
} 
+0

Không cần phải kiểm tra các đặc quyền hiện có. Nó hoạt động ngay cả khi bạn gọi nó nhiều lần: Grant-CAPrivilege -Identity $ UserName "SeServiceLogonRight" –

Các vấn đề liên quan