×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Count of WMIObject returns null instead of 1

Count of WMIObject returns null instead of 1

Count of WMIObject returns null instead of 1

(OP)
Hi,

Can someone tell me what is wrong with my .count statement that it returns no value (null) instead of one. See my code snip below and result. This does work if I have >1 job returned

Code:
$thresholdTime = (Get-Date).Addhours(-4)
$printJobs = Get-WmiObject Win32_PrintJob -ComputerName gdceps02w12v | Where-Object {[System.Management.ManagementDateTimeConverter]::ToDateTime($_.TimeSubmitted) -lt $thresholdTime -and $_.Name -like '*mbprt*'}
$printjobs
$pcnt = $printJobs.count

Write-Host "Server $server : Found ["$pcnt"] print jobs."
====================
Here's the output:


Document : 6628207BE12744FBA141193013DA97AC-tmpacea
JobId : 254
JobStatus : Error | Printing
Owner : srve
Priority : 1
Size : 1314
Name : bkr_tst9_mbprt, 254

Server gdceps02w12v : Found [ ] print jobs.


Thanks
Lee

RE: Count of WMIObject returns null instead of 1

PowerShell has a habit of unwrapping an array if there is one or no entries in the array. You can force the datatype of a variable. I would suggest you make the following change:

[array] $printJobs = Get-WmiObject Win32_PrintJob -ComputerName gdceps02w12v |
    Where-Object {[System.Management.ManagementDateTimeConverter]::ToDateTime($_.TimeSubmitted) -lt $thresholdTime -and $_.Name -like '*mbprt*'} 

Once you do that it will force the return value to an array. If Get-WmiObject returns $null then $printJobs.Count would equal 0.

Also Get-WmiObject is deprecated. I would suggest you use Get-CimInstance instead.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close