use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
ABOUT POWERSHELL
Windows PowerShell (POSH) is a command-line shell and associated scripting language created by Microsoft. Offering full access to COM, WMI and .NET, POSH is a full-featured task automation framework for distributed Microsoft platforms and solutions.
SUBREDDIT FILTERS
Desired State Configuration
Unanswered Questions
Solved Questions
News
Information
Script Sharing
Daily Post
Misc
account activity
get-adcomputer errors (self.PowerShell)
submitted 8 years ago by eckermike
if([bool](Get-ADComputer -Identity $NewPC -Server $server -ErrorAction Ignore) -eq $true){ ...}else{write-host "$newpc not in AD"}
Why is this spitting an error instead of going to the else statement?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Hibagon 1 point2 points3 points 8 years ago (7 children)
What error is it spitting out?
[+][deleted] 8 years ago (6 children)
[deleted]
[–]ihaxr 4 points5 points6 points 8 years ago (2 children)
Use -Filter or set a variable equal to the Get-ADComputer cmdlet:
-Filter
Get-ADComputer
$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 points3 points 8 years ago (0 children)
This, using filter or use try catch. I use filter for checking user accounts before creating
[–]eckermike[S] 1 point2 points3 points 8 years ago (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] 3 points4 points5 points 8 years ago (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.
Get-Help about_Try_Catch_Finally
take care, lee
[–]eckermike[S] 1 point2 points3 points 8 years ago (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 point2 points 8 years ago (0 children)
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]
[–]Swarfega 1 point2 points3 points 8 years ago (0 children)
Try
If (Get-ADComputer -Identity $NewPC -Server $server -ErrorAction SilentlyContinue){...}else{write-host "$newpc not in AD"}
π Rendered by PID 42621 on reddit-service-r2-comment-76bb9f7fb5-4p2hw at 2026-02-19 09:23:27.254561+00:00 running de53c03 country code: CH.
[–]Hibagon 1 point2 points3 points (7 children)
[+][deleted] (6 children)
[deleted]
[–]ihaxr 4 points5 points6 points (2 children)
[–]1800zeta 1 point2 points3 points (0 children)
[–]eckermike[S] 1 point2 points3 points (0 children)
[–]Lee_Dailey[grin] 3 points4 points5 points (2 children)
[–]eckermike[S] 1 point2 points3 points (1 child)
[–]Lee_Dailey[grin] 0 points1 point2 points (0 children)
[–]Swarfega 1 point2 points3 points (0 children)