i using following function add users local user group.i need add array of users local group on machine.
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") i getting following error:
the following exception occurred while retrieving member "add":
group name not found.
i new powershell. please :)
the issue here
-groupname:"comserver consumer", -userarr a comma powershell delimiter array.
the problem had was, seperatet parameters comma, powershell makes space.
take question more informations: how pass multiple parameters function in powershell?
/update
here code
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 please try out
Comments
Post a Comment