Solved

question about count variable

Posted on 2015-01-22
8
77 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 39

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
 
LVL 39

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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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 39

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 39

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

Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
The article will show you how you can maintain a simple logfile of all Startup and Shutdown events on Windows servers and desktops with PowerShell. The script can be easily adapted into doing more like gracefully silencing/updating your monitoring s…
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

914 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now