Error: PowerShell Execution

JRY
Mega Guru

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.

 

find_real_file.png

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

1 ACCEPTED SOLUTION

Chaitanya Redd1
Tera Guru

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

View solution in original post

5 REPLIES 5

Ct111
Tera Sage

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

Yes, I have verified the group name it's correct one and valid group as well.

 

Thanks,

JRY

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.

LINK1

LINK2

LINK3

 

Mark my ANSWER as CORRECT and HELPFUL if this helps

Miguel Caldero2
Tera Contributor

Are you passing a new line with the group name?  Notice that after the group name, the next character (') is in another line in your screenshot?

 

find_real_file.png