Solved

question about count variable

Posted on 2015-01-22
8
83 Views
Last Modified: 2015-01-23
Hello,
If $JOBS contains one row, the variable $nb_jobs is null, why ?
$JOBS = (& sqlcmd -E -S SRVTEST -h -1 -b -W -Q "set nocount on; SELECT DISTINCT name FROM msdb..sysjobs")
$nb_jobs = $JOBS.count

Thanks

regards
0
Comment
Question by:bibi92
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 40

Expert Comment

by:Subsun
ID: 40565104
You probably using an earlier version of PowerShell. Before PS 3, If the var is not an array (Contains more than one objects), the count property result would be null.
You either have to upgrade to Powershell v3 or later..

Or try

($JOBS | Measure).count

Or

@($JOBS).count
0
 
LVL 40

Expert Comment

by:footech
ID: 40565156
My preferred method would be to modify the line as follows.
$JOBS = @(& sqlcmd -E -S SRVTEST -h -1 -b -W -Q "set nocount on; SELECT DISTINCT name FROM msdb..sysjobs")

Open in new window

0
 
LVL 40

Accepted Solution

by:
Subsun earned 500 total points
ID: 40565175
FYI, There is a small bug in @().count as it wont display the 0 count for a $null value. For a $null value it shows 1.. with PS 2 using Measure-Object would return the actual count..
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 40

Expert Comment

by:footech
ID: 40565458
I don't know if I would call it a bug.  PS treats $null as a valid object.  I think it's more an "as designed" feature.  But I've never considered it in depth... perhaps there is a bug report filed on the Connect site.
0
 
LVL 40

Expert Comment

by:Subsun
ID: 40565483
May be.. but $null.count or $null | Measure should ideally return count as 0 not 1. They have fixed it in PS 3 and above..
0
 
LVL 40

Expert Comment

by:footech
ID: 40565642
In PS 2.0 $null.count gives nothing.  In 3.0 it gives 0.
Having an array that includes a null element the count is the same in both.
$a = "asfsd",$null,"lkjasf"
$a.count


Am I missing something?
0
 
LVL 40

Expert Comment

by:Subsun
ID: 40567513
As per my understanding single object did not had count property in PS 2. This was fixed in PS 3 and above.

I see that the examples you have given is true even in PS 4 also, @($null).count still returns 1, and there are many other things which I don't really like. For example Foreach will iterate even through null objects (it does help in some scenarios).

Foreach ($A in ($null,$null)){"Test"} or $null | Foreach {"Test"}

But really i don't want to tear my hair out as there are workarounds. For count I normally use Measure-object which gives me more accurate result..
0
 
LVL 40

Expert Comment

by:footech
ID: 40567576
Up to this point I've worked with the understanding that nulls should be counted, and further processing should handle them appropriately.  But I think it's a valid approach to not count nulls if that's what you prefer.  You'd still have to handle them in further processing, so I don't see it making much difference either way.
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Synchronize a new Active Directory domain with an existing Office 365 tenant
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
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…

737 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