**Unable to move or copy the users from one group into another group. Need to move only the specified user.
Tried with many ways (commented in code). Please help me to resolve this issue....
Output:
Unable to find a default server with Active Directory Web Services running.**
enter code here
param
(
# Parameter must be specified
[Parameter(mandatory=$false)] [String] $ADServer ,
[Parameter(mandatory=$false)] [String] $ADServer_UserName ,
[Parameter(mandatory=$false)] [String] $ADServer_Password ,
[Parameter(mandatory=$false)] [String] $FirstName ,
[Parameter(mandatory=$false)] [String] $LastName ,
[Parameter(mandatory=$false)] [String] $UserlogonName ,
[Parameter(mandatory=$false)] [String] $Password ,
[Parameter(mandatory=$false)] [String] $MobileNumber ,
[Parameter(mandatory=$false)] [String] $OrganizationalUnitName,
[Parameter(mandatory=$false)] [String] $GroupName
#[Parameter(mandatory=$false)]
#[String] $EmailID
)
try
{
$ADServer ='10.10.10.10'
$ADServer_UserName ='rpa'
$ADServer_Password ='Auto'
$FirstName ='ktb'
$LastName ='user1'
$UserlogonName ='ktbuser1'
$Password ='rpa@User3'
$MobileNumber ='91**********'
$OrganizationalUnitName ='Automation'
$GroupName ='KTBP'
$adModuleName = "ActiveDirectory"
# Check if "ActiveDirectory" module is available
if(Get-Module -ListAvailable -Name $adModuleName -ErrorAction Stop -WarningAction Ignore)
{
# Import "ActiveDirectory" module
Import-Module -Name $adModuleName -ErrorAction Stop -WarningAction Ignore
# Define a credential object
$ADServerCredential = New-Object -TypeName System.Management.Automation.PSCredential `
-ArgumentList $ADServer_UserName,(ConvertTo-SecureString $ADServer_Password -AsPlainText -Force) -ErrorAction Stop -WarningAction Ignore
# Check if specified user logon name exists in active directory
$isUserlogonNameExists = Get-ADUser -Server $ADServer -Credential $ADServerCredential -Filter "SamAccountName -eq '$($UserlogonName)'" -ErrorAction Stop -WarningAction Ignore
Write-Host "isUserlogonNameExists ->'$($isUserlogonNameExists)' "
if (!$isUserlogonNameExists)
{
Write-Host "The user logon name '$($UserlogonName)' is not available in AD. Choose another logon name, and then try again."
return
}
else
{
Write-Host "The user logon name '$($UserlogonName)' is available in AD."
# Set Name property of user
$FullName = "$($FirstName) $($LastName)"
# Check if specified username exists in active directory
$isUserFullNameExists = Get-ADUser -Server $ADServer -Credential $ADServerCredential -Filter "Name -eq '$($FullName)'" -ErrorAction Stop -WarningAction Ignore
if(!$isUserFullNameExists)
{
Write-Host "Windows cannot create the new user object because the name '$($FullName)' is already in use. Choose another name, and then try again."
}
else
{
# Check if organization unit exists
$isOrganizationalUnitExists = Get-ADOrganizationalUnit -Server $ADServer -Credential $ADServerCredential `
-Filter "Name -eq '$($OrganizationalUnitName)'" -ErrorAction Stop -WarningAction Ignore
Write-Host "isOrganizationalUnitExists -> '$isOrganizationalUnitExists '"
if($isOrganizationalUnitExists)
{
# Get path of Organizational Unit
$getOrganizationUnitPath = Get-ADOrganizationalUnit -Server $ADServer -Credential $ADServerCredential -Filter "Name -eq '$($OrganizationalUnitName)'" `
-ErrorAction Stop -WarningAction Ignore| Select -ExpandProperty DistinguishedName
Write-Host "getOrganizationUnitPath -> '$getOrganizationUnitPath'"
# Get path of forest
$forest = Get-ADDomain -Server $ADServer -Credential $ADServerCredential -ErrorAction Stop -WarningAction Ignore | Select -ExpandProperty Forest
Write-Host "forest -> '$forest'"
# Append user logon name with forest
$userPrincipalName = "$($UserlogonName)@$($forest)"
Write-Host "userPrincipalName -> '$userPrincipalName'"
# Create new active directory user
#New-ADUser -Server $ADServer -Credential $ADServerCredential -SamAccountName $UserlogonName -Name $FullName -DisplayName $FullName -Path "$($getOrganizationUnitPath)" `
#-GivenName $FirstName -Surname $LastName -UserPrincipalName $userPrincipalName -AccountPassword (ConvertTo-SecureString $Password -AsPlainText -force -ErrorAction Stop -WarningAction Ignore) `
#-Enabled $True -PasswordNeverExpires $True -MobilePhone $MobileNumber -ErrorAction Stop -WarningAction Ignore
$isUserCreatedInOrganizationUnit = Get-ADUser -Server $ADServer -Credential $ADServerCredential -Filter "SamAccountName -eq '$($UserlogonName)'" `
-ErrorAction Stop -WarningAction Ignore | Where-Object {$_.DistinguishedName -eq "CN=$($FullName),$($getOrganizationUnitPath)"}
Write-Host "isUserCreatedInOrganizationUnit -> '$isUserCreatedInOrganizationUnit'"
#Write-Host "$ -> '$'"
$OU = "OU=Automation,OU=KTBPersonal Banking,DC=gdc,DC=local"
#$Result=Get-ADGroupMember -Identity 'KTBCommercial Banking' | foreach { Move-ADObject -Identity $_.DistinguishedName -TargetPath $OU -Whatif}
#Get-ADGroupMember -Identity "KTBCommercial Banking" -Recursive | Move-ADObject -TargetPath "OU=Automation,OU=KTBPersonal Banking,DC=gdc,DC=local"
#Get-ADGroupMember "KTBPersonal Banking" | ForEach-Object { Add-ADGroupMember -Identity "KTBCommercial Banking" -Members $_ Remove-ADGroupMember -Identity "KTBPersonal Banking" -Members $_}
#Get-ADGroupMember "KTBPersonal Banking" | ForEach-Object { Add-ADGroupMember -Server $ADServer -Credential $ADServerCredential -Identity "KTBCommercial Banking" -Members $_ Remove-ADGroupMember -Identity "KTBPersonal Banking" -Members $_}
#Get-ADGroupMember "KTBPersonal Banking" | Get-ADUser | Foreach-Object { Add-ADGroupMember -Identity "KTBCommercial Banking" -Members $_ Remove-ADGroupMember -Identity "KTBPersonal Banking" -Members $_ }
#Get-ADGroupMember "KTBPersonal Banking" -Server $ADServer -Credential $ADServerCredential | Get-ADUser -Server $ADServer -Credential $ADServerCredential | Foreach-Object { Add-ADGroupMember -Server $ADServer -Credential $ADServerCredential -Identity "KTBCommercial Banking" -Members $_ Remove-ADGroupMember -Identity "KTBPersonal Banking" -Members $_ }
Add-ADGroupMember -Identity 'KTBCommercial Banking' -Members (Get-ADGroupMember -Identity 'KTBPersonal Banking' -Recursive) -Server $ADServer -Credential $ADServerCredential
# Check if user account is created
if($isUserCreatedInOrganizationUnit)
{
Write-Host "User '$($UserlogonName)' has been created."
Write-Host "Name : $($FullName) `nUserPrincipalName : $($userPrincipalName) `nPassword : $($Password) `nMobilePhone : $($MobileNumber)"
}
else
{
Write-Host "`nFailed to create user '$($UserlogonName)'."
}
}
else
{
Write-Host "`nOrganizational Unit '$($OrganizationalUnitName)' does not exists. Please specify valid OU name."
return
}
}
}
}
else
{
Write-Host "`n'$($adModuleName)' module not available."
}
}
catch
{
Write-Host "`n$_"
}
# End of script
Tried with many ways (commented in code). Please help me to resolve this issue....
Output:
Unable to find a default server with Active Directory Web Services running.**
enter code here
param
(
# Parameter must be specified
[Parameter(mandatory=$false)] [String] $ADServer ,
[Parameter(mandatory=$false)] [String] $ADServer_UserName ,
[Parameter(mandatory=$false)] [String] $ADServer_Password ,
[Parameter(mandatory=$false)] [String] $FirstName ,
[Parameter(mandatory=$false)] [String] $LastName ,
[Parameter(mandatory=$false)] [String] $UserlogonName ,
[Parameter(mandatory=$false)] [String] $Password ,
[Parameter(mandatory=$false)] [String] $MobileNumber ,
[Parameter(mandatory=$false)] [String] $OrganizationalUnitName,
[Parameter(mandatory=$false)] [String] $GroupName
#[Parameter(mandatory=$false)]
#[String] $EmailID
)
try
{
$ADServer ='10.10.10.10'
$ADServer_UserName ='rpa'
$ADServer_Password ='Auto'
$FirstName ='ktb'
$LastName ='user1'
$UserlogonName ='ktbuser1'
$Password ='rpa@User3'
$MobileNumber ='91**********'
$OrganizationalUnitName ='Automation'
$GroupName ='KTBP'
$adModuleName = "ActiveDirectory"
# Check if "ActiveDirectory" module is available
if(Get-Module -ListAvailable -Name $adModuleName -ErrorAction Stop -WarningAction Ignore)
{
# Import "ActiveDirectory" module
Import-Module -Name $adModuleName -ErrorAction Stop -WarningAction Ignore
# Define a credential object
$ADServerCredential = New-Object -TypeName System.Management.Automation.PSCredential `
-ArgumentList $ADServer_UserName,(ConvertTo-SecureString $ADServer_Password -AsPlainText -Force) -ErrorAction Stop -WarningAction Ignore
# Check if specified user logon name exists in active directory
$isUserlogonNameExists = Get-ADUser -Server $ADServer -Credential $ADServerCredential -Filter "SamAccountName -eq '$($UserlogonName)'" -ErrorAction Stop -WarningAction Ignore
Write-Host "isUserlogonNameExists ->'$($isUserlogonNameExists)' "
if (!$isUserlogonNameExists)
{
Write-Host "The user logon name '$($UserlogonName)' is not available in AD. Choose another logon name, and then try again."
return
}
else
{
Write-Host "The user logon name '$($UserlogonName)' is available in AD."
# Set Name property of user
$FullName = "$($FirstName) $($LastName)"
# Check if specified username exists in active directory
$isUserFullNameExists = Get-ADUser -Server $ADServer -Credential $ADServerCredential -Filter "Name -eq '$($FullName)'" -ErrorAction Stop -WarningAction Ignore
if(!$isUserFullNameExists)
{
Write-Host "Windows cannot create the new user object because the name '$($FullName)' is already in use. Choose another name, and then try again."
}
else
{
# Check if organization unit exists
$isOrganizationalUnitExists = Get-ADOrganizationalUnit -Server $ADServer -Credential $ADServerCredential `
-Filter "Name -eq '$($OrganizationalUnitName)'" -ErrorAction Stop -WarningAction Ignore
Write-Host "isOrganizationalUnitExists -> '$isOrganizationalUnitExists '"
if($isOrganizationalUnitExists)
{
# Get path of Organizational Unit
$getOrganizationUnitPath = Get-ADOrganizationalUnit -Server $ADServer -Credential $ADServerCredential -Filter "Name -eq '$($OrganizationalUnitName)'" `
-ErrorAction Stop -WarningAction Ignore| Select -ExpandProperty DistinguishedName
Write-Host "getOrganizationUnitPath -> '$getOrganizationUnitPath'"
# Get path of forest
$forest = Get-ADDomain -Server $ADServer -Credential $ADServerCredential -ErrorAction Stop -WarningAction Ignore | Select -ExpandProperty Forest
Write-Host "forest -> '$forest'"
# Append user logon name with forest
$userPrincipalName = "$($UserlogonName)@$($forest)"
Write-Host "userPrincipalName -> '$userPrincipalName'"
# Create new active directory user
#New-ADUser -Server $ADServer -Credential $ADServerCredential -SamAccountName $UserlogonName -Name $FullName -DisplayName $FullName -Path "$($getOrganizationUnitPath)" `
#-GivenName $FirstName -Surname $LastName -UserPrincipalName $userPrincipalName -AccountPassword (ConvertTo-SecureString $Password -AsPlainText -force -ErrorAction Stop -WarningAction Ignore) `
#-Enabled $True -PasswordNeverExpires $True -MobilePhone $MobileNumber -ErrorAction Stop -WarningAction Ignore
$isUserCreatedInOrganizationUnit = Get-ADUser -Server $ADServer -Credential $ADServerCredential -Filter "SamAccountName -eq '$($UserlogonName)'" `
-ErrorAction Stop -WarningAction Ignore | Where-Object {$_.DistinguishedName -eq "CN=$($FullName),$($getOrganizationUnitPath)"}
Write-Host "isUserCreatedInOrganizationUnit -> '$isUserCreatedInOrganizationUnit'"
#Write-Host "$ -> '$'"
$OU = "OU=Automation,OU=KTBPersonal Banking,DC=gdc,DC=local"
#$Result=Get-ADGroupMember -Identity 'KTBCommercial Banking' | foreach { Move-ADObject -Identity $_.DistinguishedName -TargetPath $OU -Whatif}
#Get-ADGroupMember -Identity "KTBCommercial Banking" -Recursive | Move-ADObject -TargetPath "OU=Automation,OU=KTBPersonal Banking,DC=gdc,DC=local"
#Get-ADGroupMember "KTBPersonal Banking" | ForEach-Object { Add-ADGroupMember -Identity "KTBCommercial Banking" -Members $_ Remove-ADGroupMember -Identity "KTBPersonal Banking" -Members $_}
#Get-ADGroupMember "KTBPersonal Banking" | ForEach-Object { Add-ADGroupMember -Server $ADServer -Credential $ADServerCredential -Identity "KTBCommercial Banking" -Members $_ Remove-ADGroupMember -Identity "KTBPersonal Banking" -Members $_}
#Get-ADGroupMember "KTBPersonal Banking" | Get-ADUser | Foreach-Object { Add-ADGroupMember -Identity "KTBCommercial Banking" -Members $_ Remove-ADGroupMember -Identity "KTBPersonal Banking" -Members $_ }
#Get-ADGroupMember "KTBPersonal Banking" -Server $ADServer -Credential $ADServerCredential | Get-ADUser -Server $ADServer -Credential $ADServerCredential | Foreach-Object { Add-ADGroupMember -Server $ADServer -Credential $ADServerCredential -Identity "KTBCommercial Banking" -Members $_ Remove-ADGroupMember -Identity "KTBPersonal Banking" -Members $_ }
Add-ADGroupMember -Identity 'KTBCommercial Banking' -Members (Get-ADGroupMember -Identity 'KTBPersonal Banking' -Recursive) -Server $ADServer -Credential $ADServerCredential
# Check if user account is created
if($isUserCreatedInOrganizationUnit)
{
Write-Host "User '$($UserlogonName)' has been created."
Write-Host "Name : $($FullName) `nUserPrincipalName : $($userPrincipalName) `nPassword : $($Password) `nMobilePhone : $($MobileNumber)"
}
else
{
Write-Host "`nFailed to create user '$($UserlogonName)'."
}
}
else
{
Write-Host "`nOrganizational Unit '$($OrganizationalUnitName)' does not exists. Please specify valid OU name."
return
}
}
}
}
else
{
Write-Host "`n'$($adModuleName)' module not available."
}
}
catch
{
Write-Host "`n$_"
}
# End of script