Coping AD member groups for Computers - Poweshell

Viewed 30

Trying to create a script that Copies groups from 1 Computer to another. The script also has a list of Groups that won't copy over. I have been able to debug the script until line 42 with the below error message.

Here is my current script:

*If($Hostname -eq 'ISD-TS-01' -or 'ISD-TS-03' -or 'ISD-TS-04')
{
function Show-Menu
{
   param (
        [String]$Title = 'Copy AD Computer Groups Groups'
    )
    cls
    Write-Host ================ $Title ================
    $ComputerToCopy = Read-Host -Prompt 'Input the Computer to copy'
    $Computer = $ComputerToCopy
    
    foreach ($group in $groups) {
    $members = Get-ADGroupMember -Identity $group -Recursive  Select -ExpandProperty SamAccountName
    If ($members -contains $Computer) {
        Write-Host "$Computer  is in $group" -ForegroundColor Red
        Pause
        Exit 
    } Else {
        Write-Host Starting Script -ForegroundColor Green
    }
}
$NewComputer = Read-Host -Prompt 'Input the name of the new computer'
Get-ADComputer -Identity $ComputerToCopy -Properties memberof -Verbose | Select-Object -ExpandProperty memberof -Verbose |
Add-ADGroupMember -Members $NewComputer -PassThru -Verbose
    $Computer = $NewComputer
    $groups = 'G-SCCM-SD-EGRESS_WIN10'
    foreach ($group in $groups) {
    $members = Get-ADGroupMember -Identity $group -Recursive | Select -ExpandProperty SamAccountName
        If ($members -contains $Computer) {
        #remove-adgroupmember -Identity "$Group" -Member "$NewComputer" -Confirm:$false
        Write-Host "$Computer has been removed from $group" -ForegroundColor Cyan 
    } Else {
        Write-Host "$Computer is not a member of $group" -ForegroundColor Green
    }
}
    Write-Host "1: Press '1' to Retry."
    Write-Host "Q: Press 'Q' to quit."
}
do
 {
    show-menu
    $input = Read-Host "Select an option"
    Switch ($input)
    {
        '1' {
            cls
            'You chose option #1'
        }'2'{
            cls
            'You chose option #2'
        }'q'{
            return    
        }
     }
     pause
 }
 until ($input -eq 'q')
      }
      Else {
      Write-Host "Script Cannot be Run on this Host. Please use TS-01"
Read-Host
      }*

Powershell Error

1 Answers

You have to use the SamAccountName of the station! the SamAccountName is the computer name with $ at the end

you can use this in your script to find it

Get-ADComputer -Identity $NewComputer | Select-Object -ExpandProperty SamAccountName
Related