所以我有下面的代码可以很好地工作,但由于某种原因,它只计算我的D:驱动器而不是我的C:驱动器?
;WITH cte AS (
SELECT [ID], [Time], [Area], [Name],
row_number() OVER(PARTITION BY [Area] ORDER BY [Time] desc) AS [rn]
FROM [AreaTable] WHERE ID=1
)
SELECT [ID], [Time], [Area], [Name] FROM cte WHERE [rn] = 1
答案 0 :(得分:1)
好像你需要制作一个数组。试试这个......
$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
答案 1 :(得分: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
。