0

I am trying to learn to script with WMI object & PowerShell, so I'm not sure why the simple script below does not work for some server but work for the other:

$Server = 'PRODDB17-V'

Get-WmiObject -ComputerName $Server -Class Win32_LogicalDisk -Filter "DriveType = 3" | ft -AutoSize
Get-WmiObject -Query "SELECT * FROM Win32_Service WHERE DisplayName LIKE '%SQL%'" -ErrorAction Stop -ComputerName $Server | ft -AutoSize

I got the below error like below:

DeviceID DriveType ProviderName    FreeSpace          Size VolumeName
-------- --------- ------------    ---------          ---- ----------
C:               3               50624507904  104751689728 SYSTEM    
D:               3              135013552128 1915396026368 DATA      
L:               3               71224967168  167772155904 LOGS      
S:               3              131093495808 2198886936576 SQL       
P:               3              107122515968  644108775424 PageFile 
T:               3                8489771008   42946523136 TEMPDB    


Get-WmiObject : Out of memory 
At line:5 char:1
+ Get-WmiObject -Query "SELECT * FROM Win32_Service WHERE DisplayName LIKE '%SQL%' ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], ManagementException
    + FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.Commands.GetWmiObjectCommand
halfer
  • 19,824
  • 17
  • 99
  • 186
Senior Systems Engineer
  • 1,061
  • 2
  • 27
  • 63
  • 1
    If you're using PowerShell 3+, then it is recommended you use the [CIM](https://learn.microsoft.com/en-us/powershell/module/cimcmdlets/?view=powershell-6) cmdlets instead of the 'WMI' ones. For your examples, that means using `Get-CimInstance` instead of `Get-WmiObject` – boxdog Jan 22 '19 at 11:44
  • Can't test right now, but shouldn't that be `DriveType -eq 3` ? – Theo Jan 22 '19 at 12:12
  • 1
    @Theo Nope. `-Filter ` Specifies a `Where` clause to use as a filter. **Uses the syntax of the WMI Query Language (WQL).** – JosefZ Jan 23 '19 at 23:31

1 Answers1

0

By default, powershell will be allocated 150 MB of memory, this may cause some of the command to fail.

Use the following command to increase the limit (run from elevated PS instance) and restart the 'Windows Management Instrumentation' Service.

set-item wsman:localhost\Shell\MaxMemoryPerShellMB 512

Increase the limit based on your Physical memory.

Jegan.M
  • 127
  • 1
  • 2
  • 8