Dear experts,
I'm having an issue when trying to get the basename of the OUs for a list of users. I have a .csv file and trying following scripts but @OUname didn't give me the result.
How get I use the OU name without that CN=XXX part?
Thanks a lot!
foreach ($User in $ADUsers)
{
#Read user data from each field in each row and assign the data to a variable as below
$department = $User.Department
$Username = $User.Account
$email = $User.Email
$employeeid = $User.EmployeeID
$jobtitle = $User.JobTitle
$employeeType = $User.employeeType
$group1 = $User.Group1
$group2 = $User.Group2
$DN = Get-ADUser -Identity $Username -Properties * | select DistinguishedName
$OUname = Get-ADOrganizationalUnit -Identity $DN -Properties * | select name
I've defined $DN but the next $OUname went wrong with errors:
PS C:\Users\eaven.huang> $DN
DistinguishedName
-----------------
CN=Eaven.test,OU=IT Test,OU=Admins,OU=Staff,OU=Users,OU=XXX,DC=cn
PS C:\Users\eaven.huang> $OUname = Get-ADOrganizationalUnit -Identity $DN
Get-ADOrganizationalUnit : Cannot validate argument on parameter 'Identity'. The Identity property on the argument is null or empty.
At line:1 char:46
+ $OUname = Get-ADOrganizationalUnit -Identity $DN
+ ~~~
+ CategoryInfo : InvalidData: (:) [Get-ADOrganizationalUnit], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.GetADOrganizationalUnit