[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

question about count variable

Posted on 2015-01-22
8
Medium Priority
?
89 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 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
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 
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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

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).
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
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…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

650 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