用于检查C:驱动器容量并写入CSV文件的Powershell脚本

时间:2018-12-19 18:03:31

标签: powershell ps

我正在尝试获取C:\的总大小和可用空间,但是它没有追加到CSV文件中。

使用DeviceID和Devicetype尝试过,不确定哪一个是正确的

$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
}

1 个答案:

答案 0 :(得分:0)

这将满足我的要求。我仍然不确定您的实际问题是什么,因为您没有包括您所引用的所有代码。

我想您所指的问题是如何标识系统驱动器。我从Get-CimInstance -ClassName CIM_OperatingSystem得到了驱动器号,然后用它来过滤对Get-CimInstance -ClassName CIM_LogicalDisk的调用的结果。

我只有一个系统,因此响应全部来自该系统。 [咧嘴]

#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

屏幕输出...

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文件内容...

"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"