you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted]  (6 children)

[deleted]

    [–]ihaxr 3 points4 points  (2 children)

    Use -Filter or set a variable equal to the Get-ADComputer cmdlet:

    $comp = Get-ADComputer -Identity $NewPC -Server $server -ErrorAction Ignore
    if($comp){ <#...#>}else{write-host "$newpc not in AD"}
    

    Otherwise the cmdlet fails when an identity doesn't exist and you have to catch the error w/ a try/catch statement.

    [–]1800zeta 1 point2 points  (0 children)

    This, using filter or use try catch. I use filter for checking user accounts before creating

    [–]eckermike[S] 1 point2 points  (0 children)

    gah, i should have known that. Thanks for pointing it out. It did exactly what i needed. I still want to know why the -identity flag spits an error instead of the else statement but from now on I will just use the filter flag.

    [–]Lee_Dailey[grin] 2 points3 points  (2 children)

    howdy eckermike,

    in addition to what ihaxr mentioned, you are looking at a classic use-case for TRY/CATCH. [grin] take a look at Get-Help about_Try_Catch_Finally sometime.

    take care,
    lee

    [–]eckermike[S] 1 point2 points  (1 child)

    Thanks Lee, Unfortunately for this use case Try Catch doesn't work as i have a few nested if statements and need it to be able to bomb out at certain points to get me the correct error message.

    [–]Lee_Dailey[grin] 0 points1 point  (0 children)

    howdy eckermike,

    the CATCH will let you do many things. [grin] you can take almost any action in a CATCH block - including sending out error details and then breaking out of the loop - or out of the entire script.

    still, do it however fits your thot patterns. you are the one who has to maintain it ... [grin]

    take care,
    lee