problem in Get-Aduser


hi friends

i have lost patiense & cry.

it's 1 hour struggling simple issu.

in domain have 3 users called u1, u2, u3    both names & samaccount names same each user.

when run 

ps c:\> get-aduser -filter {name -eq "u1"}   ----> works

but followings doesn't work & show no output:

$i = 1

ps c:\> get-aduser -filter {name -eq "u$i"}   ----> no result (nothing shown in output)

a guy said: " powershell won't resolve variable name inside scriptblock.  you have define you're username outside , use variable"

but problem is, without -filter, if command checks existence of object in ad & in case object not present, produces error.

let me clarify:

writing function checks existence of these 3 users in ad database & if don't exist in ad, (without getting error describes "object not found"), creates them.

for ($i = 1; $i -le 3; $i++){       if (!(get-aduser -filter {name -eq "u$i"})) {new-aduser -name "u$i"}       }  #if don't use script block , terminating error & code won't continue:      ($i = 1; $i -le 3; $i++){        if (!(get-aduser "u$i")) {new-aduser -name "u$i"}       }

get-aduser : cannot find object identity: 'u1' under: 'dc=hp,dc=lab'.

@ c:\users\administrator\desktop\untitled2.ps1:2 char:13

+       if (!(get-aduser "u$i")) {new-aduser -name "u$i"}

+             ~~~~~~~~~~~~~~~~

    + categoryinfo          : objectnotfound: (u1:aduser) [get-aduser], adidentitynotfoundexception

    + fullyqualifiederrorid : activedirectorycmdlet:microsoft.activedirectory.management.adidentityno 

   tfoundexception,microsoft.activedirectory.management.commands.getaduser

 

'++' operator works on numbers. operand 'system.string'.

@ c:\users\administrator\desktop\untitled2.ps1:1 char:27

+    for ($i = 1; $i -le 3; $i++){

+                           ~~~~

    + categoryinfo          : invalidoperation: (:) [], runtimeexception

    + fullyqualifiederrorid : operatorrequiresnumber





not [problem.  quirk of filter

 get-aduser -filter "name -eq 'u$i'"

now work intended.


¯\_(ツ)_/¯



Windows Server  >  Windows PowerShell



Comments

Popular posts from this blog

Error: 0x80073701 when trying to add Print Services Role in Windows 2012 Standard

Disconnecting from a Windows Server 2012 R2 file sharing session on a Windows 7,8,10 machine

Event ID 64,77,1008 Certificates Events Windows Server 2008, 2008R2