Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Count of WMIObject returns null instead of 1 1

Status
Not open for further replies.

ls62

Programmer
Oct 15, 2001
177
US
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
 
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:

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

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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top