/build/static/layout/Breadcrumb_cap_w.png

Adding user to local group script error

Hi all , 

In a bid to improve my powershell knowledge I tried writing a script to add users to local groups , using different function for each group. This is the script :


import-module ActiveDirectory

function Add-RemoteUser{


[CmdletBinding()]

Param(
[Parameter(Mandatory=$True)]
[string]$Computer,
[Parameter(Mandatory=$True)]
[string]$user
)


$domainuser = "domain\"+$user
$Userexist = Get-ADUser -Identity $user


if ($Userexist -eq $null) {Write-host "Wtf?? This user doesn't exist" -ForegroundColor Yellow}

Else

{([ADSI]"WinNT://$Computer/Remote Desktop Users,group").psbase.Invoke("Add",([ADSI]"WinNT://$domainuser").path)}

}


 I tried running this function but get error :

Exception calling "Invoke" with "2" argument(s): "A member could not be added to or removed from the local group because the member does not exist.
"
At G:\Add_local Ad user.ps1:28 char:2
+ {([ADSI]"WinNT://$Computer/Remote Desktop Users,group").psbase.Invoke("Add",([ADSI]" ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodTargetInvocation

Looked around and seen a few mentions , the userid I tried does exist in AD , computer name is correct and online . Also am not using Fully qualified domain name for the computer name so no idea why am getting this error. Any ideas?

0 Comments   [ + ] Show comments

Answers (2)

Answer Summary:
Posted by: M P 8 years ago
Purple Belt
2

Top Answer

Try passing the user as "domain/username" instead of "domain\user" (from your variable) to the Add function.
Posted by: TonyFishers 8 years ago
Yellow Belt
1
Duuh..of course!  how could I miss that? That was it pal! Many thanks ! 

Comments:
  • No problem. Glad to help. - M P 8 years ago
 
This website uses cookies. By continuing to use this site and/or clicking the "Accept" button you are providing consent Quest Software and its affiliates do NOT sell the Personal Data you provide to us either when you register on our websites or when you do business with us. For more information about our Privacy Policy and our data protection efforts, please visit GDPR-HQ