-1

I'm trying to get C:\ total size and the free space, but it is not appending to CSV file.

Tried using DeviceID and Devicetype, not sure which one is true

$path="C:\server" #Split-Path $MyInvocation.MyCommand.path 
#$cred= get-credential 
$Computers = get-content "$path\computers.txt"   
foreach ($Computer in $Computers)  
{  
    $Disks = Get-wmiobject  Win32_LogicalDisk -computername $Computer
}
Ambrose Leung
  • 3,704
  • 2
  • 25
  • 36
Zee
  • 1
  • 2

1 Answers1

0

this will do what i think you want. i am still unsure what your actual problem is since you did not include all the code that you referred to.

i THINK the problem you were referring to was how to ID the system drive. i got the drive letter from Get-CimInstance -ClassName CIM_OperatingSystem and then used that to filter the results from a call to Get-CimInstance -ClassName CIM_LogicalDisk.

i've only one system, so the responses are all from that one. [grin]

#requires -RunAsAdministrator

# fake reading in a list of computers from a text file
#    in real life, use Get-Content
$ComputerList = @'
LocalHost
BetterNotBeThere
127.0.0.1
10.0.0.1
'@ -split [environment]::NewLine

$IC_Scriptblock = {
    $CIM_ComputerSystem = Get-CimInstance -ClassName CIM_ComputerSystem
    $CIM_OperatingSystem = Get-CimInstance -ClassName CIM_OperatingSystem
    $CIM_LogicalDisk = Get-CimInstance -ClassName CIM_LogicalDisk |
        Where-Object {$_.Name -eq $CIM_OperatingSystem.SystemDrive}

    [PSCustomObject]@{
        ComputerName = $CIM_ComputerSystem.Name
        SysDrive = $CIM_OperatingSystem.SystemDrive
        SysDrive_FreeSpace_GB = '{0:N2}' -f ($CIM_LogicalDisk.FreeSpace / 1GB)
        SysDrive_FreeSpace_Pct = '{0:N0}' -f (($CIM_LogicalDisk.FreeSpace / $CIM_LogicalDisk.Size) * 100)
        SysDrive_Size_GB = '{0:N2}' -f ($CIM_LogicalDisk.Size / 1GB)
        }
    }

$RespondingSystems = foreach ($CL_Item in $ComputerList)
    {
    $IC_Params = @{
        ComputerName = $CL_Item
        ScriptBlock = $IC_Scriptblock
        ErrorAction = 'SilentlyContinue'
        }

    Invoke-Command @IC_Params
    }

$NON_RespondingSystems = $ComputerList.Where({$_ -notin $RespondingSystems.PSComputerName})

$RespondingSystems
'=' * 30
$NON_RespondingSystems

# send to CSV
#    this also removes the PSComputerName, PSShowComputerName, & RunspaceId properties
$RespondingSystems |
    Select-Object -Property * -ExcludeProperty PSComputerName, PSShowComputerName, RunspaceId |
    Export-Csv -LiteralPath "$env:TEMP\Zee_SysDriveInfo.csv" -NoTypeInformation

on screen output ...

ComputerName           : [MySystemName]
SysDrive               : C:
SysDrive_FreeSpace_GB  : 747.75
SysDrive_FreeSpace_Pct : 80
SysDrive_Size_GB       : 931.41
PSComputerName         : LocalHost
RunspaceId             : e09d14e9-448b-4e0e-99c1-95e9c636963b

ComputerName           : [MySystemName]
SysDrive               : C:
SysDrive_FreeSpace_GB  : 747.75
SysDrive_FreeSpace_Pct : 80
SysDrive_Size_GB       : 931.41
PSComputerName         : 127.0.0.1
RunspaceId             : ece90fac-5f1d-4f27-8685-5e4955e306b8

==============================
BetterNotBeThere
10.0.0.1

CSV file content ...

"ComputerName","SysDrive","SysDrive_FreeSpace_GB","SysDrive_FreeSpace_Pct","SysDrive_Size_GB"
"[MySystemName]","C:","747.75","80","931.41"
"[MySystemName]","C:","747.75","80","931.41"
Lee_Dailey
  • 7,292
  • 2
  • 22
  • 26