2017-10-16 69 views
0

所以我有下面的代碼工作得很好,但由於某種原因,它只計算我的D:驅動器,而不是我的C:驅動器?PowerShell驅動器計算器

$computerName = Get-Wmiobject Win32_ComputerSystem 
$computerOS = Get-Wmiobject Win32_OperatingSystem 
$computerHDD = Get-Wmiobject Win32_LogicalDisk -Filter drivetype=3 

ForEach($HDD in $computerHDD){ 
$txtObject = New-Object PSObject -property @{ 
    'ComputerName' = $computerName.Name 
    'ComputerModel' = $computerName.Model 
    'SerialNumber' = $computerName.SerialNumber 
    'HDDSize' = "{0:N2}" -f ($HDD.Size/1GB) 
    'HDDFree' = "{0:P2}" -f ($HDD.FreeSpace/$HDD.Size) 
    'OS' = $computerOS.caption 
    'OS_type' = $computerOS.OSArchitecture 
    'User' = $computerName.UserName 
    } 
} 
$txtObject | Select-Object ComputerName, ComputerModel, SerialNumber, HDDSize, HDDFree, OS, Os_type, User | Out-File "$PSSCriptRoot\computer_info.txt" -Append 
+0

是你的'D:'驅動類型'3'(本地磁盤)的驅動器?不是可移動磁盤('2')或光盤('5')? – BACON

+0

我的D:驅動器是機械硬盤驅動器,我的C:驅動器是SSD。 – luanswan2002

+0

對不起,我後面讀到;所以只能計算'D:'驅動器。無論如何,這一切都取決於存儲在「$ computerHDD」中的內容。如果'Get-Wmiobject Win32_LogicalDisk'沒有返回所有期望的驅動器,那麼它們將永遠不會在'ForEach'循環中處理。 – BACON

回答

2

好像你需要做一個數組。試試這個...

$computerName = Get-Wmiobject Win32_ComputerSystem 
$computerOS = Get-Wmiobject Win32_OperatingSystem 
$computerHDD = Get-Wmiobject Win32_LogicalDisk -Filter drivetype=3 

$output = @() 
ForEach($HDD in $computerHDD){ 
$txtObject = New-Object PSObject -property @{ 
    'ComputerName' = $computerName.Name 
    'ComputerModel' = $computerName.Model 
    'SerialNumber' = $computerName.SerialNumber 
    'HDDSize' = "{0:N2}" -f ($HDD.Size/1GB) 
    'HDDFree' = "{0:P2}" -f ($HDD.FreeSpace/$HDD.Size) 
    'OS' = $computerOS.caption 
    'OS_type' = $computerOS.OSArchitecture 
    'User' = $computerName.UserName 
    } 
    $output += $txtObject 
} 
$output | Select-Object ComputerName, ComputerModel, SerialNumber, HDDSize, HDDFree, OS, Os_type, User | Out-File "$PSSCriptRoot\computer_info.txt" -Append 
+0

謝謝,這工作得很好。只有小巧的是它產生了兩個類似的列表,不同之處在於SSD/HDD。 – luanswan2002

+0

我不確定你的意思。你指的是標籤嗎? – ShanayL

0

你在循環的每次迭代覆蓋$txtObject,所以你只輸出包含從最後一次迭代的驅動器。相反,你應該初始化$txtObject爲數組,然後追加每個驅動器的信息是:

$computerHDD = Get-Wmiobject Win32_LogicalDisk -Filter drivetype=3 

$txtObject = @() 
ForEach($HDD in $computerHDD){ 
    $txtObject += New-Object PSObject -property @{ 
     # ... 
    } 
} 
$txtObject | Select-Object ... | Out-File "$PSSCriptRoot\computer_info.txt" -Append 

更重要的是,你可以消除環路和變量,只是使用管道:

$computerName = Get-WmiObject Win32_ComputerSystem 
$computerOS = Get-WmiObject Win32_OperatingSystem 

Get-WmiObject Win32_LogicalDisk -Filter drivetype=3 ` 
    | ForEach-Object -Process { 
     New-Object PSObject -Property @{ 
      'ComputerName' = $computerName.Name 
      'ComputerModel' = $computerName.Model 
      'SerialNumber' = $computerName.SerialNumber 
      'HDDSize'  = "{0:N2}" -f ($_.Size/1GB) 
      'HDDFree'  = "{0:P2}" -f ($_.FreeSpace/$_.Size) 
      'OS'   = $computerOS.caption 
      'OS_type'  = $computerOS.OSArchitecture 
      'User'   = $computerName.UserName 
     }; 
    } | Out-File "$PSSCriptRoot\computer_info.txt" -Append 

請注意,New-Object與您的原始代碼幾乎完全相同,必須使用$_而不是$HDD

相關問題