?
Solved

question about count variable

Posted on 2015-01-22
8
Medium Priority
?
90 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
  • 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 41

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 2000 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
Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

 
LVL 41

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 41

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 41

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

A Cyber Security RX to Protect Your Organization

Join us on December 13th for a webinar to learn how medical providers can defend against malware with a cyber security "Rx" that supports a healthy technology adoption plan for every healthcare organization.

Question has a verified solution.

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

Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
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…
Loops Section Overview
Screencast - Getting to Know the Pipeline

850 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