2017-12-22 6 views
1

Powershell을 사용하여 원격 컴퓨터에서 사용자를 만들려고합니다. 일단 계정이 생성되면 로컬 관리 그룹에 추가하려고합니다.Powershell에서 로컬 관리자 그룹에 사용자를 추가하는 방법은 무엇입니까?

계정이 생성되고 있지만 관리 그룹에 계정이 추가되지 않습니다. 아래는 내가 사용하고있는 코드입니다.

cls 
    $username = "test_user" 
$password = "password" 
$computer1 = hostname 
    $users = $null 
    $computer = [ADSI]“WinNT://$computer1” 
    Try { 
     $users = $computer.psbase.children | select -expand name 
     if ($users -like $username) { 
     Write-Host "$username already exists" 
     } Else { 
     $user_obj = $computer.Create(“user”, “$username”) 
     $user_obj.SetPassword($password) 
     $user_obj.SetInfo() 

     $user_obj.Put(“description”, “$username”) 
     $user_obj.SetInfo() 
     $user_obj.psbase.invokeset(“AccountDisabled”, “False”) 
     $user_obj.SetInfo() 
     $users = $computer.psbase.children | select -expand name 
     if ($users -like $username) { 
      Write-Host "$username has been created on $($computer.name)" 

      $group = [ADSI]("WinNT://"+$env:COMPUTERNAME+"/administrators,group") 
$group.add("WinNT://$env:localhost/$username,user") 


     } Else { 



      Write-Host "$username has not been created on $($computer.name)" 
     } 
     } 
    } Catch { 
     Write-Host "Error creating $username on $($computer.path): $($Error[0].Exception.Message)" 
    } 

내가 뭘 잘못하고 있니?

+0

이 오류가. WinNT에서 fuj_wintel을 만드는 중 오류가 발생했습니다 : // mymachine : "1"인수가있는 "add"예외 : "잘못된 디렉토리 경로 이름이 전달되었습니다. – Ironic

+0

@BenH는 원격으로 작동합니다. 답변을 붙여 넣으십시오. – Ironic

+0

제안 할 수있는 것이 한 가지 더 있습니다. 어떻게 서비스 계정이 존재하는지 여부는 알 수 있습니까? – Ironic

답변

1

$env:computername은 로컬 컴퓨터입니다. $env:localhost이 존재하지 않습니다. $computer1은 사용자를 이전에 추가 할 컴퓨터의 정의 된 변수입니다. 단지이 일을

$group = [ADSI]("WinNT://$computer1/administrators,group") 
$group.add("WinNT://$computer1/$username,user") 
1

나는, 많은 코드 인이

$computername = "computername" # place computername here for remote access 
$username = 'user' 
$password = '[email protected]' #password 
$desc = 'Local admin account' 
$computer = [ADSI]"WinNT://$computername,computer" 
$user = $computer.Create("user", $username) 
$user.SetPassword($password) 
$user.Setinfo() 
$user.description = $desc 
$user.setinfo() 
$user.UserFlags = 65536 
$user.SetInfo() 
$group = [ADSI]("WinNT://$computername/administrators,group") 
$group.add("WinNT://$username,user") 
+0

당신의 대답은 정확합니다. 투표했습니다. BenH는 내가 그 문제를 더 일찍 해결할 수 있도록 도와줍니다. – Ironic

1

를 사용합니다.

호출-명령 -ComputerName SomeRemoteComputerName -scriptblock

Yeppers, 내가 알고, 모든 순수 포쉬 아니다 { 순 사용자 SomeNewUserName SomePassword는 순 LOCALGROUP 관리자는 를 추가/SomeNewUserName}. 물론, PoSH (더 많은 방법)의 더 많은 코드를 통해이 작업을 수행 할 수 있지만 때로는 작업을 완료해야 할 때가 있습니다.

하지만이 작업을 처음부터 수행하는 것 (물론, 물건을 배우려고하지 않는 한). 당신이 활용할 수 있도록 미리 만들어진 스크립트와 모듈이 있습니다. 참조 :

'gallery.technet.microsoft.com/scriptcenter/site/search?f%5B0%5D.Type=RootCategory이 & f를 % 5B0 % 5D.Value =에는 LocalAccount'물론

모든 기계의 경우 PoSH v5 +를 사용하는 경우 로컬 사용자/그룹 관리에 기본 제공 cmdlet 만 사용하면됩니다.

'docs.microsoft.com/en-us/powershell/module/microsoft.powershell.localaccounts/?view=powershell-5.1'

다른 질문에 대해서는

: '체크 서비스 계정이 존재 여부 '

원격 컴퓨터 용인지 묻는 가정하면 동일한 방법입니다.

호출-명령 -ComputerName SomeRemoteComputerName -scriptblock { 가져 오기 서비스 -Name SomeServiceName 받기 서비스 -DisplayName SomeServiceDisplayName }