PowerShell 驱动器大小

Posted

技术标签:

【中文标题】PowerShell 驱动器大小【英文标题】:PowerShell Drive Size 【发布时间】:2018-10-07 15:23:13 【问题描述】:

我发现了如何获取drive size的这个想法

我在将它合并到我的脚本时遇到问题,因为我不知道在哪里插入代码。 此外,即使存在多硬盘驱动器系统,每台计算机也只能输出一条线。

这是我正在使用的代码,其中包含“获取驱动器数据”代码

# Output file location to be changed as needed
$file="C:\scripts\reports\InentoryTest_$((Get-Date).ToString('MM-dd-yyyy')).csv"
$txt="c:\scripts\reports\InentoryTest-error_$((Get-Date).ToString('MM-dd-yyyy')).txt"

# Getting computers from Active Directory
$Computers = Get-ADComputer -Filter Name -like 'M416*' | select -expand name

  Foreach($Computer in $Computers)

if(!(Test-Connection -ComputerName $Computer -BufferSize 16 -Count 1 -ea 0 -quiet))
 
 write-host "Cannot reach $Computer is offline" -ForegroundColor red
 
 else
 

$Output = @()

    Try
    
       # Get Drive Data 
       $disk   = Get-WmiObject -ComputerName $Computer Win32_LogicalDisk | Where-Object  ( $_.DriveType ) -eq 3 -and ( ( $_.freespace / $_.size ) -lt .1  )  | ForEach-Object -Process 
                   [pscustomobject] @
        Drive        = $_.DeviceID
        Size         = '0:N1' -f ( $_.Size / 1GB )
        Free         = '0:N1' -f ( $_.freespace / 1GB )
        PercentFree  = '0:N1' -f ( $_.freespace / $_.size * 100 )
                        
                     
        $domain = Get-WmiObject Win32_ComputerSystem -ComputerName $Computer -ErrorAction Stop
        $os   = Get-WmiObject Win32_OperatingSystem -ComputerName $Computer -ErrorAction Stop
        $mac  = Get-WmiObject -class Win32_NetworkAdapterConfiguration -Filter "IPEnabled='True'" -ComputerName $Computer -ErrorAction Stop
        $bios = Get-WmiObject win32_bios -ComputerName $Computer -ErrorAction Stop
        $cpu  = Get-WmiObject –class Win32_processor -ComputerName $Computer -ErrorAction Stop
        $AD   = Get-ADComputer $Computer -properties Name,Lastlogondate,ipv4Address,enabled,description,DistinguishedName -ErrorAction Stop
        $ram  = "0 GB" -f ((Get-WmiObject Win32_PhysicalMemory -ComputerName $Computer -ErrorAction Stop | Measure-Object Capacity  -Sum).Sum / 1GB)
        $pc   = Get-WmiObject win32_computersystem -ComputerName $Computer -ErrorAction Stop | select @Name = "Type";Expression = if (($_.pcsystemtype -eq '2')  )
                'Laptop' Else 'Desktop Or Other'
        ,Manufacturer,@Name = "Model";Expression = if (($_.model -eq "$null")  ) 'Virtual' Else $_.model,username

        # Create Output
        $data = New-Object PSObject -Property @
            SerialNumber              = $bios.serialnumber -replace "-.*"
            Computername              = $AD.name
            IPaddress                 = $AD.ipv4Address
            MACaddress                = $mac.MACAddress
            Enabled                   = $AD.Enabled
            Description               = $AD.description
            OU                        = $AD.DistinguishedName.split(',')[1].split('=')[1] 
            DC                        = $domain.domain
            Type                      = $pc.type
            Manufacturer              = $pc.Manufacturer
            Model                     = $pc.Model
            RAM                       = $ram
            Disk                      = $disk #Get Drive Data
            ProcessorName             = ($cpu.name | Out-String).Trim()
            NumberOfCores             = ($cpu.NumberOfCores | Out-String).Trim()
            NumberOfLogicalProcessors = ($cpu.NumberOfLogicalProcessors | Out-String).Trim()
            Addresswidth              = ($cpu.Addresswidth | Out-String).Trim()
            OperatingSystem           = $os.caption
            InstallDate               = ([WMI] '').ConvertToDateTime($os.installDate)
            LastLogonDate             = $ld.lastlogondate
            LoggedinUser              = $pc.username
        

        # Only do this kind of update if it hasn't failed yet
        $Output += $data
        $desc="$($mac.MACAddress) ( $($bios.serialnumber -replace "-.*") ) $($pc.Model) | $((Get-Date).ToString('MM-dd-yyyy'))"
        #Set-ADComputer $Computer -Description $desc -verbose
        $Output | select Computername,Enabled,Description,IPaddress,MACaddress,OU,DC,Type,SerialNumber,Manufacturer,Model,RAM,Disk,ProcessorName,NumberOfCores,NumberOfLogicalProcessors,Addresswidth,OperatingSystem,InstallDate,LoggedinUser,LastLogonDate | export-csv -Append $file -NoTypeInformation 

    
    Catch [Exception]
    
    # Only do this kind of update if create output has failed
        $ErrorMessage = $_.Exception.Message
        Add-Content -value "$Computer, $ErrorMessage, skipping to next" $txt
        #Set-ADComputer $Computer -Description $ErrorMessage
            continue
      
     
    

【问题讨论】:

【参考方案1】:

您正在构建一个复杂的分层对象,因此最好收集所有对象,然后将结果转储为 JSON 或 XML 文件。但是,如果您确实想要一个平面字符串,那么您必须在将磁盘数据添加到要转储到 CSV 的对象之前将其显式格式化为字符串。比如:

$diskData = $disk | foreach 
  "[Drive: $($_.DeviceID), Size: $([int]($_.Size/1GB)), FreeSpace: $([int]($_.freespace/1GB)), PercentFree: $([int]($_.freespace/$_.size *100))]"

$diskdata = $diskdata -join " "

【讨论】:

以上是关于PowerShell 驱动器大小的主要内容,如果未能解决你的问题,请参考以下文章

powershell PowerShell:同步并挂载HSLU ILIAS驱动器#PowerShell

powershell 使用PowerShell映射驱动器

powershell 使用PowerShell映射Windows网络驱动器

powershell 使用PowerShell映射Windows网络驱动器

powershell PowerShell命令显示计算机上的所有驱动器

powershell 这使用PowerShell来更改计算机上的驱动器号。