gpt4 book ai didi

powershell - 将用户添加到本地用户组

转载 作者:行者123 更新时间:2023-12-03 01:20:21 27 4
gpt4 key购买 nike

我正在使用以下功能将用户添加到本地用户组中。我需要将一组用户添加到计算机上的本地组中。

function AddUsersToGroup([string]$MachineName,[string]$GroupName,[String[]]$Userarr)
{
write-host 'write: $Userarr'
Foreach($s in $Userarr)
{
write-host $s
}
$objOU = [ADSI]"WinNT://$MachineName/$GroupName,group"
Foreach($User in $Userarr)
{
$objOU.add("WinNT://$MachineName/$User,user")
}
}

$UserArray=@("Administrator", "NETWORK SERVICE", "LOCAL SERVICE","SYSTEM")
AddUsersToGroup -MachineName:"localhost" -GroupName:"Comserver Consumer",-Userarr:@("Administrator", "NETWORK SERVICE", "LOCAL SERVICE","SYSTEM")
AddUsersToGroup -MachineName:"localhost" -GroupName:"Comserver Consumer",-Userarr:@("Administrator", "NETWORK SERVICE", "LOCAL SERVICE","SYSTEM")

我收到以下错误:

The following exception occurred while retrieving member "add":
The group name could not be found.



我是Powershell的新手。请帮忙 :)

最佳答案

问题在这里

-GroupName:"Comserver Consumer", -Userarr

对于shell,逗号是数组的定界符。

您遇到的问题是,您将逗号分隔的参数分开了,但是PowerShell将其分隔为哪个空间。

请查看此问题以获取更多信息:
How do I pass multiple parameters into a function in PowerShell?

/更新

这是代码
function AddUsersToGroup([string]$MachineName,[string]$GroupName,[Array]$Users)
{


$Group = [ADSI]"WinNT://$MachineName/$GroupName,group"


Foreach($User in $Users)
{
Write-Host $User
$Group.Add("WinNT://$User,user")
}
}


[Array]$UserArray=@("Username", "NETWORK SERVICE", "LOCAL SERVICE","SYSTEM")
AddUsersToGroup -MachineName "localhost" -GroupName "Testgroup" -Users $UserArray

请尝试一下

关于powershell - 将用户添加到本地用户组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16397407/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com