Powershell 在远程Windows服务器上创建本地用户并添加到管理员组

Powershell 在远程Windows服务器上创建本地用户并添加到管理员组,powershell,wmi,powershell-remoting,get-wmiobject,Powershell,Wmi,Powershell Remoting,Get Wmiobject,我已创建PowerShell脚本以在远程Windows服务器上创建用户并添加到管理员组: $Computer = Read-Host "Computer name:" $UserName = Read-Host "User name:" $Password = Read-Host "Password" -AsSecureString $AdminGroup = [ADSI]"WinNT://$Computer/Administrator,group" $User = [ADSI]"WinNT:/

我已创建PowerShell脚本以在远程Windows服务器上创建用户并添加到管理员组:

$Computer = Read-Host "Computer name:"
$UserName = Read-Host "User name:"
$Password = Read-Host "Password" -AsSecureString
$AdminGroup = [ADSI]"WinNT://$Computer/Administrator,group"
$User = [ADSI]"WinNT://$Computer/$UserName,user"
$Cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $UserName, (ConvertTo-SecureString $Password -AsPlainText –Force)
$User.SetPassword($Cred.GetNetworkCredential().Password)
$AdminGroup.Add($User.Path)
它给了我以下的错误:

The following exception occurred while retrieving member "SetPassword": " The user name could not be found. At C:\test1.ps1:7 char:18 + $User.SetPassword <<<< ($Cred.GetNetworkCredential().Password) + CategoryInfo : NotSpecified: (:) [], ExtendedTypeSystemException + FullyQualifiedErrorId : CatchFromBaseGetMember The following exception occurred while retrieving member "Add": "The specified local group does not exist. At C:\test1.ps1:8 char:16 + $AdminGroup.Add <<<< ($User.Path) + CategoryInfo : NotSpecified: (:) [], ExtendedTypeSystemException + FullyQualifiedErrorId : CatchFromBaseGetMember 检索成员“SetPassword”时发生以下异常: 找不到用户名。 在C:\test1.ps1:7 char:18 +$User.SetPassword我认为您在下面的“administrators”中缺少一个“s”

我有一个将用户添加到本地administrators组的(工作)脚本,该行如下所示:

$AdminGroup = [ADSI]"WinNT://$ComputerName/Administrators,group"
我想你在下面的“管理员”中漏掉了一个“s”

我有一个将用户添加到本地administrators组的(工作)脚本,该行如下所示:

$AdminGroup = [ADSI]"WinNT://$ComputerName/Administrators,group"

您实际上从未创建过用户。您还想更正管理员组名。我已修复了您的代码:

$Computer = Read-Host "Computer name:"
$UserName = Read-Host "User name:"
$Password = Read-Host "Password" -AsSecureString
$AdminGroup = [ADSI]"WinNT://$Computer/Administrators,group"
$CompObject = [ADSI]"WinNT://$Computer"
$User = $CompObject.Create('User',$UserName)
$Cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $UserName, (ConvertTo-SecureString $Password -AsPlainText –Force)
$User.SetPassword($Cred.GetNetworkCredential().Password)
$User.SetInfo()
$AdminGroup.Add($User.Path)

您实际上从未创建过用户。您还想更正管理员组名。我已修复了您的代码:

$Computer = Read-Host "Computer name:"
$UserName = Read-Host "User name:"
$Password = Read-Host "Password" -AsSecureString
$AdminGroup = [ADSI]"WinNT://$Computer/Administrators,group"
$CompObject = [ADSI]"WinNT://$Computer"
$User = $CompObject.Create('User',$UserName)
$Cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $UserName, (ConvertTo-SecureString $Password -AsPlainText –Force)
$User.SetPassword($Cred.GetNetworkCredential().Password)
$User.SetInfo()
$AdminGroup.Add($User.Path)

如果要创建用户,则需要实际创建用户。仅当用户帐户已存在时,您使用的语句才会返回用户帐户:

创建本地帐户的最简单方法可能是以下命令:

使用是可能的,但更复杂:

$acct = [adsi]"WinNT://$Computer"
$user = $acct.Create('User', $UserName)
$user.SetPassword($Cred.GetNetworkCredential().Password)
$user.SetInfo()
另外,正如其他人已经指出的,您拼错了administrators组的名称(这是导致第二个错误的原因)。由于该组的名称可以本地化,具体取决于您运行的语言版本,因此您可能仍希望解决该问题:

$AdminGroupName = Get-WmiObject Win32_Group -Filter "LocalAccount=True AND SID='S-1-5-32-544'" |
                  Select-Object -Expand Name
$AdminGroup = [adsi]"WinNT://$Computer/$AdminGroupName,group"

如果要创建用户,则需要实际创建用户。仅当用户帐户已存在时,您使用的语句才会返回用户帐户:

创建本地帐户的最简单方法可能是以下命令:

使用是可能的,但更复杂:

$acct = [adsi]"WinNT://$Computer"
$user = $acct.Create('User', $UserName)
$user.SetPassword($Cred.GetNetworkCredential().Password)
$user.SetInfo()
另外,正如其他人已经指出的,您拼错了administrators组的名称(这是导致第二个错误的原因)。由于该组的名称可以本地化,具体取决于您运行的语言版本,因此您可能仍希望解决该问题:

$AdminGroupName = Get-WmiObject Win32_Group -Filter "LocalAccount=True AND SID='S-1-5-32-544'" |
                  Select-Object -Expand Name
$AdminGroup = [adsi]"WinNT://$Computer/$AdminGroupName,group"

@Ansgar Wiechers-非常感谢用户加载项远程工作,但是尽管有上述设置,它仍无法添加到管理员组。@Ansgar Wiechers-非常感谢用户加载项远程工作,但是尽管有上述设置,它无法添加到管理员组。