简体   繁体   English

如何在Powershell中将用户添加到本地管理员组?

[英]How to add user to local admin group in Powershell?

I am trying to create user on remote machine using Powershell. 我正在尝试使用Powershell在远程计算机上创建用户。 Once account created I want to add that in local admin group. 创建帐户后,我想在本地管理员组中添加该帐户。

Account is getting created but it is not getting added in admin group. 帐户已创建,但未在管理组中添加。 Below is the code that I am using. 下面是我正在使用的代码。

  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)"
   }

What am I doing wrong? 我究竟做错了什么?

$env:computername is your local computer. $env:computername是您的本地计算机。 $env:localhost doesn't exist. $env:localhost不存在。 $computer1 is the variable you defined of the computer to you are adding the user to earlier. $computer1是您为之前添加用户的计算机定义的变量。

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

I use this 我用这个

$computername = "computername"  # place computername here for remote access
$username = 'user'
$password = 'P@ssw0rd1' #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")

That is a whole lot of code, just to do this. 这是一大堆代码,只是为了做到这一点。

Invoke-Command -ComputerName SomeRemoteComputerName -ScriptBlock { net user SomeNewUserName SomePassword net localgroup administrators SomeNewUserName /add } Invoke-Command -ComputerName SomeRemoteComputerName -ScriptBlock {net user SomeNewUserName SomePassword net localgroup administrators SomeNewUserName / add}

Yeppers, I know, it's not all pure PoSH. Yyppers,我知道,这不是纯粹的PoSH。 Sure, you can do this with via more code in PoSH (way more in ), but sometimes you just need to get stuff done. 当然,你可以通过PoSH中的更多代码(更多内容)来实现这一点,但有时你只需要完成工作。

But vs doing this from scratch (well, unless you are just trying to learn stuff). 但是从头开始这样做(好吧,除非你只是想学习东西)。 There is a whole set of pre-built scripts and module for you to leverage. 有一整套预先构建的脚本和模块供您利用。 See: 看到:

'gallery.technet.microsoft.com/scriptcenter/site/search?f%5B0%5D.Type=RootCategory&f%5B0%5D.Value=localaccount' 'gallery.technet.microsoft.com/scriptcenter/site/search?f%5B0%5D.Type=RootCategory&f%5B0%5D.Value=localaccount'

Of course if all machines were on PoSH v5+, then you just use the built-in cmdlets for local user / group management. 当然,如果所有计算机都在PoSH v5 +上,那么您只需使用内置cmdlet进行本地用户/组管理。

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

As for the other question: 'check service account exist or not' 至于另一个问题:'检查服务帐户是否存在'

Assuming you are asking if this is for a remote computer, then it's the same approach. 假设您在询问这是否适用于远程计算机,那么它就是相同的方法。

Invoke-Command -ComputerName SomeRemoteComputerName -ScriptBlock { Get-Service -Name SomeServiceName Get-Service -DisplayName SomeServiceDisplayName } Invoke-Command -ComputerName SomeRemoteComputerName -ScriptBlock {Get-Service -Name SomeServiceName Get-Service -DisplayName SomeServiceDisplayName}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM