/build/static/layout/Breadcrumb_cap_w.png

Scripting Question


Adding user to local group script error

11/21/2016 1875 views
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?
Answer Summary:
0 Comments   [ + ] Show comments

Comments

  • This content is currently hidden from public view.
    Reason: Spam For more information, visit our FAQ's.

Answer Chosen by the Author

2
Try passing the user as "domain/username" instead of "domain\user" (from your variable) to the Add function.
Answered 11/21/2016 by: M P
Purple Belt


Community Chosen Answer

1
Duuh..of course!  how could I miss that? That was it pal! Many thanks ! 
Answered 11/22/2016 by: TonyFishers
Yellow Belt

  • No problem. Glad to help.

All Answers

This content is currently hidden from public view.
Reason: Spam For more information, visit our FAQ's.
This content is currently hidden from public view.
Reason: Removed by member request For more information, visit our FAQ's.

Don't be a Stranger!

Sign up today to participate, stay informed, earn points and establish a reputation for yourself!

Sign up! or login

Share

 
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