Solved

PowerShell Set-StrictMode -Version Latest

Posted on 2014-01-08
3
529 Views
Last Modified: 2014-01-12
I've just discovered "Set-StrictMode -Version Latest" in Powershell and am wondering how to handle this issue, or is it an issue at all?

$MainQuery= Invoke-Sqlcmd -Query $Query

If the SQL returns more than 1 record then $MainQuery.Count is valid, but if there is only 1 result returned the "Count" property is not valid.

I want to show the number of records:
$TotalRecords = $MainQuery.Count
write-host $TotalRecords

*Also note, that in PS3 Microsoft changed something (forget what it is called) where 0 and 1 are different (no longer numbers unless specified with "@()" ) than numbers 2 and higher. As an example "@(1)" has to be used sometimes or it is seen as not a one, but an object or something. Thought I would mention this because it seems like it might be the same issue here?

Anyway, I guess $MainQuery might be  called a dynamic object because the properties change? Hmmm, maybe it is just the change Microsoft made, but I would still like to know if the code is "OK" or if I should handle it differently?

Does PS 4 have a way to test if a property exist for an object and then if it does I could code it so that if .Count was not available then to set the $TotalRecords = 1 else $TotalRecords = $MainQuery.Count

What are your thoughts?
thanks!
0
Comment
Question by:johnj_01201
3 Comments
 
LVL 39

Accepted Solution

by:
footech earned 500 total points
ID: 39766242
If a query or cmdlet returns multiple objects, saving this to a variable is generally (I hestitate to say "always") an array, and each object is an element of the array.  When only one object is returned, it's generally not an array.  That explains why the .count property is not always available.  You can get around this by forcing the results to be stored in an array, even if there's only one object/element.  Typically you can do this by enclosing the command(s) which return the results within array notation.  So try the below.
$MainQuery= @(Invoke-Sqlcmd -Query $Query)

Open in new window

0
 
LVL 68

Expert Comment

by:Qlemo
ID: 39766737
I'm not aware of a change of handling of 0 and 1 differently in PS3. Probably you mean the handling of (no element), (one element) and (many elements). No element is always something tricky, because it can mean many things, and sometimes needs to be ignored, in other cases not, and such.

As footech elaborated, enclosing results in @(...) will make sure you can treat them as "array" all the time.
0
 
LVL 1

Author Closing Comment

by:johnj_01201
ID: 39775516
Thanks. I'm new to PS and get confused. Your answer is the same (I think) to what I've found a while ago when trying to get IF statements to work when 1 result was returned from invoke-sql.  Based on your explanation, your fix is exactly what I need to do whenever I use invoke-sqlcmd and expect the .count property.

http://connect.microsoft.com/SQLServer/feedback/details/796367/count-property-of-invoke-sqlcmd-result-is-null-when-record-set-contains-exactly-one-record
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

863 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

23 Experts available now in Live!

Get 1:1 Help Now