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:
Please log in to answer
Posted by:
M P
8 years ago
Posted by:
TonyFishers
8 years ago
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