- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-20-2022 11:55 PM
Hello Team,
I'm attempting to add a user to a group, but I'm getting an error that says "Powershell execution error." Can somebody explain what went wrong to me? Despite my modifications, the problem remains.
Powershell Script:
#Validate if groupname is not empty
if (test-path env:\SNC_groupname) {
$groupname = $env:SNC_groupname;
$username = $env:SNC_username;
$domainname = $env:SNC_domainname;
if($env:SNC_domainuser){
$domainuser = $env:SNC_domainuser;
}else{
$domainuser = $domainname;
}
};
#Normalize fields
$groupname = $groupname -replace "%27","'";
$username = $username -replace "%27","'";
$domainname= $domainname-replace "%27","'";
$domainuser= $domainuser-replace "%27","'";
#Get the Domain of the User
$sam = $username
if($domainname -eq $domainuser)
{
$forest = (Get-ADForest).Name + ":3268"
}else
{
$forest = $domainuser
}
$type = Get-ADObject -Filter 'SamAccountName -eq $username' -Server $forest | Select -Expand ObjectClass
if($type -eq 'computer')
{
$userserver = Get-ADComputer -Filter 'SamAccountName -eq $sam' -Server $forest -Properties CanonicalName | Select @{N='Domain';E={($_.CanonicalName -split '/')[0]}}
#Grab User and Group objects to allow Cross-Domain adds
$User = Get-ADComputer $username -Server $userserver.Domain
$Group = Get-ADGroup $groupname -Server $domainname
Add-ADGroupMember -Identity $Group -Members $User -Server $domainname -credential $cred
}
if($type -eq 'user')
{
$userserver = Get-ADUser -Filter 'SamAccountName -eq $sam' -Server $forest -Properties CanonicalName | Select @{N='Domain';E={($_.CanonicalName -split '/')[0]}}
#Grab User and Group objects to allow Cross-Domain adds
$User = Get-ADUser $username -Server $userserver.Domain
$Group = Get-ADGroup $groupname -Server $domainname
Add-ADGroupMember -Identity $Group -Members $User -Server $domainname -credential $cred
#Get-ADGroupMember -Identity $Group
}
Thanks,
JRY
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-01-2022 11:43 PM
Hi,
I have tried to debuging your script there's something issue with the script it's fetching two groups for the User - $userserver.Domain. It might be impacting that the first group doesn't have the users.
Please try below script it might work.
#Validate if groupname is not empty
if (test-path env:\SNC_groupname) {
$groupname = $env:SNC_groupname;
$username = $env:SNC_username;
$domainname = $env:SNC_domainname;
if($env:SNC_domainuser){
$domainuser = $env:SNC_domainuser;
}else{
$domainuser = $domainname;
}
};
#Normalize fields
$groupname = $groupname -replace "%27","'";
$username = $username -replace "%27","'";
$domainname= $domainname-replace "%27","'";
$domainuser= $domainuser-replace "%27","'";
#Get the Domain of the User
$sam = $username
if($domainname -eq $domainuser)
{
$forest = (Get-ADForest).Name + ":3268"
}else
{
$forest = $domainuser
}
$type = Get-ADObject -Filter 'SamAccountName -eq $username' -Server $forest | Select -Expand ObjectClass
if($type -eq 'computer')
{
$userserver = Get-ADComputer -Filter 'SamAccountName -eq $sam' -Server $forest -Properties CanonicalName | Select @{N='Domain';E={($_.CanonicalName -split '/')[0]}}
#Grab User and Group objects to allow Cross-Domain adds
$User = Get-ADComputer $username -Server $userserver.Domain
$Group = Get-ADGroup $groupname -Server $domainname
Add-ADGroupMember -Identity $Group -Members $User -Server $domainname -credential $cred
}
if($type -eq 'user')
{
#$userserver = Get-ADUser -Filter 'SamAccountName -eq $sam' -Server $forest -Properties CanonicalName | Select @{N='Domain';E={($_.CanonicalName -split '/')[0]}}
#Grab User and Group objects to allow Cross-Domain adds
$User = Get-ADUser $username -Server $domainuser
$Group = Get-ADGroup $groupname -Server $domainname
Add-ADGroupMember -Identity $Group -Members $User -Server $domainname -credential $cred
#Get-ADGroupMember -Identity $Group
}
If my answer helps any way please mark it helpful.
Thanks,
Chaitanya
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2022 06:42 AM
Are you sure the group name is right to which you are trying to add?
Because initial screenshot says that "mhtestgroup.99" is not found.
Crosscheck once at the AD end and verify the group name from their team.
Mark my ANSWER as CORRECT and HELPFUL if it helps
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-25-2022 04:52 AM
Yes, I have verified the group name it's correct one and valid group as well.
Thanks,
JRY
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-26-2022 12:52 AM
Few questions if you can answer that would be fruitful
1) do you have domain admin role in AD for making these changes?
2) Is this group getting created and then after that you are trying to add few users into it?
3) Have you tested adding any other user to the group apart from the one you have provided in script?
Reference docs about Support article in this regards.
Mark my ANSWER as CORRECT and HELPFUL if this helps
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-26-2022 01:47 PM