header
header Register : : Login header
header
divider
menuleft
menuright
submenu
left

[August 25th, 2008] Check the home page regarding PowerShell related news from a brand new sponsor: Idera

Subject: CmdLets in if statements
Prev Next
You are not authorized to post a reply.

Author Messages
frejenUser is Offline
New Member
New Member
Posts:8

07/25/2008 2:15 AM  

Hi,

I often use cmdlets in if statements, most of the time the case is similiar to verify that an (AD)object does not exist before proceeding with creation. So lets take this for an example:

if (!(get-qadobject -Identity $Id)) {
Write-Host "The object does not exist, create"
}else{
Write-Host "The object does exist, do not create"
}


The problem is that i sometimes get a lot of error output printed. Like:

"Get-QADObject : Cannot resolve DN for the given identity: 'activedirectory.ad/path/to/OU'At C:\Scripts\NoSched\createGroup.ps1:18 char:20+ If (!(get-qadobject  <<<< -Identity $GroupOU -erroraction SilentlyContinue)) {"

Is that the effect of -ea silentlycontinue? Thought it would suppress the error message. I have tried using -errorvariable also but it does not suppress the errors anyway.


I would love to have common parameter that is something like -returnbool and then have the cmdlet return $TRUE on success and $FALSE on failure/error.
Or maybe there is an other way in powershell to make this kind of checks. I have read about_common but it does not help me determine if i use -ea and -ev in the wrong way. How do you verify the existence/non-existence of an object before you remove/create. Maybe there is a way to let the cmdlet responsible for the creation/removal to handle that verification?

Frejen

marco.shawUser is Offline
Co-Community Director
Power User
Power User
Posts:151

07/25/2008 4:05 AM  

Using -ea only helps with non-terminating errors.  In this case it is a terminating error, thus -ea doesn't work as you'd like.  You can however set a global variable to handle terminating errors (you could also use TRAP):

PS>get-qaduser foobar
Get-QADUser : Cannot resolve DN for the given identity: 'foobar'
At line:1 char:12
+ get-qaduser <<<<  foobar
PS>$erroractionpreference
Continue
PS>$erroractionpreference_orig=$erroractionpreference
PS>$erroractionpreference="silentlycontinue"
PS>get-qaduser foobar
PS>$erroractionpreference=$erroractionpreference_orig
PS>get-qaduser foobar
Get-QADUser : Cannot resolve DN for the given identity: 'foobar'
At line:1 char:12
+ get-qaduser <<<<  foobar
PS>

Always remember to set your $erroractionpreference variable back to its original state, if required.


Marco

*Microsoft MVP - Windows PowerShell: http://www.microsoft.com/mvp
*PowerGadgets MVP: http://www.powergadgets.com/mvp
*Blog: http://marcoshaw.blogspot.com
frejenUser is Offline
New Member
New Member
Posts:8

07/25/2008 4:37 AM  
Thank you Marco, that may do the trick. Will play around a little bit soon. I mentioned -errorvariable in my original post, sometimes i have had the problem that my ev is empty even though an error has occured. Can that also be related to non-terminating errors or similiar?

Frejen
marco.shawUser is Offline
Co-Community Director
Power User
Power User
Posts:151

07/25/2008 4:57 AM  

Posted By frejen on 07/25/2008 4:37 AM
Thank you Marco, that may do the trick. Will play around a little bit soon. I mentioned -errorvariable in my original post, sometimes i have had the problem that my ev is empty even though an error has occured. Can that also be related to non-terminating errors or similiar?

Frejen

Seems terminating will go here:

PS>$errorΎ]

 


Marco

*Microsoft MVP - Windows PowerShell: http://www.microsoft.com/mvp
*PowerGadgets MVP: http://www.powergadgets.com/mvp
*Blog: http://marcoshaw.blogspot.com
ShayUser is Offline
Shell Enthusiast
Shell Enthusiast
Posts:68


07/26/2008 7:23 AM  

I'm using  v1.1.1.758 (lastest version) and it works as you expect:

PS > get-qadobject foo
# returns nothing

PS > (get-qadobject foo) -eq $null
True


PS > if (!(get-qadobject -Identity foo)) {
>>     Write-Host "The object does not exist, create"
>> } else{
>>     Write-Host "The object does exist, do not create"
>> }
>>
The object does not exist, create

 


Shay Levy
Windows PowerShell MVP
http://blogs.microsoft.co.il/blogs/ScriptFanatic
You are not authorized to post a reply.
Forums > Using PowerShell > General PowerShell > CmdLets in if statements



ActiveForums 3.7
right
   
footer Sponsored by Quest Software • SAPIEN Technologies • ShellTools, LLC • Microsoft Windows Server 2008 footer
footer