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

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
MichaelOBrienAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

LearnctxEngineerCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Chris DentPowerShell DeveloperCommented:

> 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
MichaelOBrienAuthor Commented:
Thanks guys.
0
MichaelOBrienAuthor Commented:
Thanks guys that helps.  

Michael
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.