0

$computers is a array of server names.

If I do:

Get-WMIObject  -ComputerName $computers Win32_LogicalDisk | Where-Object {$_.DriveType -eq 3} | Select-Object SystemName, name, Freespace, size

A list of the drive name, size etc is displayed on screen a expected.

SystemName  name    Freespace         size
----------  ----    ---------         ----
APP01 C:    25942634496 235667451904
APP01 E:    97153241088 376746012672
APP02 C:    39813332992 117555851264
App02 D:    18904997888  87909462016
PC-03 C:    21936656384 112187142144
PC-03 D:   155361820672 183472484352
APP04 C:     5994582016 117555851264
APP04 D:    50258378752  53550772224 

If I do:

$DiskData = Get-WMIObject -ComputerName $computers Win32_LogicalDisk | Where-Object {$_.DriveType -eq 3} | Select-Object SystemName, name, Freespace, size

The variable $Diskdata does not contain the results array as I woud expect. If I output the array it appears to contain nothing at all.

PS C:\Windows\system32> $DiskData

PS C:\Windows\system32> 

However if i compare it to $NULL it is not null

PS C:\Windows\system32> $null -eq $DiskData
False

PS C:\Windows\system32> $DiskData.GetType()

IsPublic IsSerial Name                                     BaseType                                                                                                 
-------- -------- ----                                     --------                                                                                                 
True     True     Object[]                                 System.Array                                                                                             

Can anyone please tell me what is going on?

edit:

PS C:\Windows\system32> [System.Management.Automation.PSSerializer]::Serialize($DiskData)
<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04">
  <Obj RefId="0">
    <TN RefId="0">
      <T>System.Object[]</T>
      <T>System.Array</T>
      <T>System.Object</T>
    </TN>
    <LST />
  </Obj>
</Objs>
FrankU32
  • 311
  • 1
  • 3
  • 18
  • Your code works for me ;) What is your `computers` like? – T-Me Aug 20 '20 at 11:02
  • What version of PS you using? – FrankU32 Aug 20 '20 at 11:04
  • Powershell V 5.1, I did `$Computers = @("Pc1","Pc2")` – T-Me Aug 20 '20 at 11:07
  • As an aside: The CIM cmdlets (e.g., `Get-CimInstance`) superseded the WMI cmdlets (e.g., `Get-WmiObject`) in PowerShell v3 (released in September 2012). Therefore, the WMI cmdlets should be avoided, not least because PowerShell [Core] (version 6 and above), where all future effort will go, doesn't even _have_ them anymore. For more information, see [this answer](https://stackoverflow.com/a/54508009/45375). – mklement0 Aug 20 '20 at 11:56
  • @FrankU32 Can you explain how you determined `$DiskData` was empty? Does `$null -eq $DiskData` return `$true` after running the second example? – Mathias R. Jessen Aug 20 '20 at 12:42
  • $null -eq $DiskData actually comes back as false, however whatever it contains it doesnt contain an array of the results which is what i would expect. $computers is an array, i wonder if its overwriting each time rather than appending? – FrankU32 Aug 20 '20 at 13:30
  • Please make sure that you are using regular dashes (I recommend you to retype the command rather than copy/paste it), see also: [Powershell Parser can't tell the difference between En dash and dash](https://github.com/PowerShell/vscode-powershell/issues/1308). Also try to use a new variable name or even a new PowerShell session. If that doesn't resolve the issue, see whats comes up when you serialize the result: `[System.Management.Automation.PSSerializer]::Serialize($DiskData)`, and paste it to the question if it doesn't help you further. – iRon Aug 20 '20 at 15:00
  • Nothing here is cut and paste, these are all the proper dash. I have added the result of your command. – FrankU32 Aug 20 '20 at 16:50
  • It works for me. How about some other variable name like $diskdata2? – js2010 Aug 20 '20 at 17:30

2 Answers2

-1

I tried this on my network with the local and one remote PC with this result.

$Computers = "DellXPS8920","DellXPS137000"
$DiskData = Get-WMIObject -ComputerName $computers Win32_LogicalDisk | 
  Where-Object {$_.DriveType -eq 3} | 
  Select-Object SystemName, name, Freespace, size

PS> $DiskData

SystemName    name    Freespace         size
----------    ----    ---------         ----
DELLXPS8920   C:   137257218048 223188348928
DELLXPS8920   G:   141891551232 230452883456
DELLXPS8920   H:    62047666176 230452883456
DELLXPS137000 C:    52942987264  85427482624
DELLXPS137000 G:   142635814912 154618818560

PS> $DiskData | gm

   TypeName: Selected.System.Management.ManagementObject

Name        MemberType   Definition                    
----        ----------   ----------                    
Equals      Method       bool Equals(System.Object obj)
GetHashCode Method       int GetHashCode()             
GetType     Method       type GetType()                
ToString    Method       string ToString()             
Freespace   NoteProperty uint64 Freespace=137257218048 
name        NoteProperty string name=C:                
size        NoteProperty uint64 size=223188348928      
SystemName  NoteProperty string SystemName=DELLXPS8920 

So the issue is why are you getting a serialized result vs Selected.System.Management.ManagementObject as shown above. This was run on Win 10 1909, Powershell ISE 5.1, Peer-to-Peer network. The Dell137000 is the remote machine.

HTH

RetiredGeek
  • 2,980
  • 1
  • 7
  • 21
-2

As T-Me said, this works ok for me to. Maybe you could try

$DiskData = (Get-WMIObject -ComputerName $computers Win32_LogicalDisk | Where-Object {$_.DriveType -eq 3} | Select-Object SystemName, name, Freespace, size) | Out-String
Vivek Kumar Singh
  • 3,223
  • 1
  • 14
  • 27
hwnd
  • 137
  • 3
  • 2
    Not only can this not be expected to make a difference with respect to whether or not `$DiskData` ends up empty, it would fundamentally change what is assigned from a _collection of objects_ to a _single, multi-line string_ containing a _for-display representation_ of the objects. – mklement0 Aug 20 '20 at 12:01