I am trying to set up a method in which a user can tell me there username, I plug it in and it will return me their computerName.
//Get the Username
$username = Read-Host -prompt 'Username'
//Get the DistinguishedName and store it
$usernameDN = Get-ADUser $username -properties * | SELECT DistinguishedName
//Get the ComputerName
//This one fails everytime
Get-ADComputer -Filter {ManagedBy -eq $usernameDN} -properties * | SELECT CN,ManagedBy
//Error I receive...almost as if it has to be a string
Get-ADComputer : Invalid value: '' specified for extended attribute:
'ManagedBy'. At line:1 char:1
+ Get-ADComputer -Filter {ManagedBy -eq $usernamedn} -properties * | SE ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Get-ADComputer], ArgumentException
+ FullyQualifiedErrorId : ActiveDirectoryCmdlet:System.ArgumentException,Microsoft.ActiveDirectory.Management.Comm
ands.GetADComputer
//So you put it as a string
PS C:\WINDOWS\system32> Get-ADComputer -Filter {ManagedBy -eq '$usernamedn'} -properties * | SELECT CN,ManagedBy
//Error
Get-ADComputer : Identity info provided in the extended attribute:
'ManagedBy' could not be resolved. Reason: 'Cannot find an object with
identity: '$usernamedn' under: 'DC=****,DC=*****'.'. At line:1 char:1
+ Get-ADComputer -Filter {ManagedBy -eq '$usernamedn'} -properties * | ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Get-ADComputer], ADIdentityResolutionException
+ FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADIdentityResolutionException
,Microsoft.ActiveDirectory.Management.Commands.GetADComputer
//However if you replace the variable with the literal DistinguishedName...it will work
PS C:\WINDOWS\system32> Get-ADComputer -Filter {ManagedBy -eq 'CN=*******\, ***** *.,OU=********,OU=*****,OU=******,DC=*****,DC=******'} -properties * | SELECT CN,ManagedBy
//Result
CN ManagedBy
-- ---------
********* CN=**\, ** *.,OU=***,OU=***,OU=***,DC=***,DC=**
********* CN=**\, ** *.,OU=***,OU=***,OU=***,DC=***,DC=**
So I'm thinking my issue is that the Filter requires it to be string, but I can't figure out the right escape to make the variable read that way.
I tried to do the string formatting as well, but I don't completely understand that yet
Thanks,
|