Need to do a condition check - completely stuck....


guys,

...learning -  baby steps now...

here playing with:

import-csv c:\work\test2.csv | foreach-object { if (get-aduser $_.samaccountname) {write-host "removing $($_.samaccountname)" -fore green}else{write-host "skiping $($_.samaccountname)" -fore yellow}}

what happening when accounts listed in file exist in ad  - works ,  i getting line written "removing such , such account", when account not exist in ad,  listed in file getting error:

get-aduser : cannot find object identity: 'aa1' under: 'dc=...,dc=
....,dc=....'.
@ line:1 char:63
+ import-csv c:\work\test2.csv | foreach-object { if (get-aduser <<<<  $_.samac
countname) {write-host "removing $($_.samaccountname)" -fore green}else{write-h
ost "skiping $($_.samaccountname)" -fore yellow}}
    + categoryinfo          : objectnotfound: (aa1:aduser) [get-aduser], a
   didentitynotfoundexception
    + fullyqualifiederrorid : cannot find object identity: 'aa1' u
   nder: 'dc=...,dc=....,dc=edu'.,microsoft.activedirectory.management
  .commands.getaduser

even if this:

import-csv c:\work\test2.csv | foreach-object { if (get-aduser $_.samaccountname) {disable-adaccount $_.samaccountname}  #i getting same error...

i thought "else" should have taken care of - not... doing wrong? guess trying accomplish @ least see (write-host) account being disabled , not...  please advise.

it seems get-aduser following pattern of "if user exists, return it, if not, throw error", whereas assuming follows "if not, return nothing."

you can try using -erroraction common parameter suppress errors when calling get-aduser:

if((get-aduser -$_.samaccountname -erroraction silentlycontinue)){ ... }

thanks,

-lincoln



Windows Server  >  Windows PowerShell



Comments

Popular posts from this blog

Motherboard replacement

Cannot create Full Text Search catalog after upgrading to V12 - Database is not fully started up or it is not in an ONLINE state

Remote Desktop App - Error 0x207 or 0x607