[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Powershell - Is it ok use a command in place of a variable in evaluating an if statement

Posted on 2010-04-09
4
Medium Priority
?
414 Views
Last Modified: 2012-05-09
Can anyone point me to some documentation explaining or explain here the rationale behind putting output into an array like this:

$a = Get-MailboxDatabase
foreach ($i in $a)
{Write-Host $i}

versus simply omitting a var/array by:

foreach ($i in (Get-MailboxDatabase))
{Write-Host $i}

I understand there may be best practice, practical concerns, or coding standard expectations but I don't know what they are, and would like to adhere to one way or the other if it makes a difference.

I do like the idea of streamlining code by using fewer variables where possible.

Here is another example of what I am asking. Would it be more proper to do:

$database = Get-MailboxDatabase $targetdatabase
if ($database -eq $null)
{
write-host “The target database doesn’t exist:” $targetdatabase
return
}
or is it ok save variable and a line of code with:

if ((Get-MailboxDatabase $targetdatabase) -eq $null)
{
write-host “The target database doesn’t exist:” $targetdatabase
return
}

So where could this cause some gotchas in coding Powershell?

Best Regards,
Michael
0
Comment
Question by:MichaelOBrien
  • 2
4 Comments
 
LVL 18

Accepted Solution

by:
Learnctx earned 500 total points
ID: 30251565
I have always done this and never had any issues. I'm not aware of any best practice that says not to do this other than to make your scripts and commands more readable. I personally would use:

if ((gwmi win32_pingstatus -filter "address='blehblehbleh'").statuscode -eq 0 ){do stuff}

over

$result = gwmi win32_pingstatus -filter "address='blehblehbleh'
if ($result -eq 0){do stuff}

The only time I'd do it the other way is for readability so that other people looking at a script can see what is happening a bit easier.
0
 
LVL 71

Assisted Solution

by:Chris Dent
Chris Dent earned 500 total points
ID: 30301840

> or coding standard expectations

As long as your audience (potentially just you) is able to understand the code you write (both now and a few years down the line) then you're good to go.

We can probably think up a few guidelines. Those will be limited to things like use of white space, use of indentation, variable naming, comments, the usual things associated with programming style. None of them are rules though :)

Chris
0
 

Author Closing Comment

by:MichaelOBrien
ID: 31760850
Thanks guys.
0
 

Author Comment

by:MichaelOBrien
ID: 30636410
Thanks guys that helps.  

Michael
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
In this post, I will showcase the steps for how to create groups in Office 365. Office 365 groups allow for ease of flexibility and collaboration between staff members.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…

612 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question