Solved

question about count variable

Posted on 2015-01-22
8
74 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 40

Expert Comment

by:Subsun
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Hi all.   The other day I had to change the passwords for a bunch of users on the fly. Because they were so many, I decided to do it in an automated way and I would like to share it with you all.   If you are not doing it directly in a Domain Co…
This article will help you understand what HashTables are and how to use them in PowerShell.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

763 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

11 Experts available now in Live!

Get 1:1 Help Now