以下是在控制台上显示物理磁盘信息的示例代码(非常感谢[电子邮件保护]):
Get-WmiObject Win32_DiskDrive | % {
$disk = $_
$partitions = "ASSOCIATORS OF " +
"{Win32_DiskDrive.DeviceID='$($disk.DeviceID)'} " +
"WHERE AssocClass = Win32_DiskDriveToDiskPartition"
Get-WmiObject -Query $partitions | % {
$partition = $_
$drives = "ASSOCIATORS OF " +
"{Win32_DiskPartition.DeviceID='$($partition.DeviceID)'} " +
"WHERE AssocClass = Win32_LogicalDiskToPartition"
Get-WmiObject -Query $drives | % {
New-Object -Type PSCustomObject -Property @{
Disk = $disk.DeviceID
DiskModel = $disk.Model
Partition = $partition.Name
DriveLetter = $_.DeviceID
VolumeName = $_.VolumeName
Size = "{0:N}" -f ($_.Size/1GB) -as [float]
FreeSpace = "{0:N}" -f ($_.FreeSpace/1GB) -as [float]
}
}
}
}
但我希望控制台输出到 TXT 文件。当我引入“Out-File”时,控制台上没有任何内容,但输出不同。它不会划分(GB),并且显示与我选择的字段不同的字段。以下是我所做的更改:
Get-WmiObject Win32_DiskDrive | % {
$disk = $_
$partitions = "ASSOCIATORS OF " +
"{Win32_DiskDrive.DeviceID='$($disk.DeviceID)'} " +
"WHERE AssocClass = Win32_DiskDriveToDiskPartition"
Get-WmiObject -Query $partitions | % {
$partition = $_
$drives = "ASSOCIATORS OF " +
"{Win32_DiskPartition.DeviceID='$($partition.DeviceID)'} " +
"WHERE AssocClass = Win32_LogicalDiskToPartition"
Get-WmiObject -Query $drives | **Out-File -filepath "d:\DiskInfo.txt" -append** | % {
New-Object -Type PSCustomObject -Property @{
Disk = $disk.DeviceID
DiskModel = $disk.Model
Partition = $partition.Name
DriveLetter = $_.DeviceID
VolumeName = $_.VolumeName
Size = "{0:N}" -f ($_.Size/1GB) -as [float]
FreeSpace = "{0:N}" -f ($_.FreeSpace/1GB) -as [float]
}
}
}
}
因此我改变了实现 OUT-FILE 的方式(将其放在每个输出行上):
Get-WmiObject Win32_DiskDrive | % {
$disk = $_
$partitions = "ASSOCIATORS OF " +
"{Win32_DiskDrive.DeviceID='$($disk.DeviceID)'} " +
"WHERE AssocClass = Win32_DiskDriveToDiskPartition"
Get-WmiObject -Query $partitions | % {
$partition = $_
$drives = "ASSOCIATORS OF " +
"{Win32_DiskPartition.DeviceID='$($partition.DeviceID)'} " +
"WHERE AssocClass = Win32_LogicalDiskToPartition"
Get-WmiObject -Query $drives | % {
New-Object -Type PSCustomObject -Property @{
Disk = $disk.DeviceID | Out-File -filepath "d:\DiskInfo.txt" -append
DiskModel = $disk.Model | Out-File -filepath "d:\DiskInfo.txt" -append
Partition = $partition.Name | Out-File -filepath "d:\DiskInfo.txt" -append
DriveLetter = $_.DeviceID | Out-File -filepath "d:\DiskInfo.txt" -append
VolumeName = $_.VolumeName | Out-File -filepath "d:\DiskInfo.txt" -append
Size = "{0:N}" -f ($_.Size/1GB) -as [float] | Out-File -filepath "d:\DiskInfo.txt" -append
FreeSpace = "{0:N}" -f ($_.FreeSpace/1GB) -as [float] | Out-File -filepath "d:\DiskInfo.txt" -append
}
}
}
}
现在它不仅在控制台(标题)和 TXT 文件(值)之间进行分割,而且还显示与以前不同的变量,并且不进行划分,只显示字节数,而不是 GB。
有人可以直接告诉我使用 OUT-FILE 吗?或者有更好的选择?
谢谢!
答案1
有人可以直接指导我使用out-file
吗?还有其他选择吗?
你可以使用 PowerShell重定向>>
脚本末尾的操作符。
} >> DiskInfo.txt
如果您想使用,out-file
那么也请将其放在脚本的末尾。
} | out-file Diskinfo.txt
笔记:
DiskInfo.txt
酌情更改。- 使用的优点
out-file
是可以添加参数,out-file
但不能>>
获取磁盘.ps1:
Get-WmiObject Win32_DiskDrive | % {
$disk = $_
$partitions = "ASSOCIATORS OF " +
"{Win32_DiskDrive.DeviceID='$($disk.DeviceID)'} " +
"WHERE AssocClass = Win32_DiskDriveToDiskPartition"
Get-WmiObject -Query $partitions | % {
$partition = $_
$drives = "ASSOCIATORS OF " +
"{Win32_DiskPartition.DeviceID='$($partition.DeviceID)'} " +
"WHERE AssocClass = Win32_LogicalDiskToPartition"
Get-WmiObject -Query $drives | % {
New-Object -Type PSCustomObject -Property @{
Disk = $disk.DeviceID
DiskModel = $disk.Model
Partition = $partition.Name
DriveLetter = $_.DeviceID
VolumeName = $_.VolumeName
Size = "{0:N}" -f ($_.Size/1GB) -as [float]
FreeSpace = "{0:N}" -f ($_.FreeSpace/1GB) -as [float]
}
}
}
} >> DiskInfo.txt
示例输出:
PS F:\test> .\Get-Disk
PS F:\test> type .\DiskInfo.txt
Size : 449.46
Partition : Disk #0, Partition #2
FreeSpace : 65.36
Disk : \\.\PHYSICALDRIVE0
DiskModel : WDC WD5000LPVX-08V0TT5
VolumeName :
DriveLetter : C:
Size : 59.61
Partition : Disk #2, Partition #0
FreeSpace : 37.13
Disk : \\.\PHYSICALDRIVE2
DiskModel : SanDisk Cruzer USB Device
VolumeName : SANDISK
DriveLetter : E:
Size : 2794.51
Partition : Disk #1, Partition #0
FreeSpace : 1648.17
Disk : \\.\PHYSICALDRIVE1
DiskModel : Seagate Expansion Desk USB Device
VolumeName : Expansion
DriveLetter : F:
PS F:\test>
about_Redirection
The Windows PowerShell redirection operators are as follows.
Operator Description Example
-------- ---------------------- ------------------------------
> Sends output to the Get-Process > Process.txt
specified file.
>> Appends the output to dir *.ps1 >> Scripts.txt
the contents of the
specified file.
2> Sends errors to the Get-Process none 2> Errors.txt
specified file.
2>> Appends errors to Get-Process none 2>> Save-Errors.txt
the contents of the
specified file.
2>&1 Sends errors (2) and Get-Process none, Powershell 2>&1
success output (1)
to the success
output stream.
3> Sends warnings to the Write-Warning "Test!" 3> Warnings.txt
specified file.
3>> Appends warnings to Write-Warning "Test!" 3>> Save-Warnings.txt
the contents of the
specified file.
3>&1 Sends warnings (3) and function Test-Warning
success output (1) { Get-Process PowerShell;
to the success Write-Warning "Test!" }
output stream. Test-Warning 3>&1
4> Sends verbose output to Import-Module * -Verbose 4> Verbose.txt
the specified file.
4>> Appends verbose output Import-Module * -Verbose 4>> Save-Verbose.txt
to the contents of the
specified file.
4>&1 Sends verbose output (4) Import-Module * -Verbose 4>&1
and success output (1)
to the success output
stream.
5> Sends debug messages to Write-Debug "Starting" 5> Debug.txt
the specified file.
5>> Appends debug messages Write-Debug "Saving" 5>> Save-Debug.txt
to the contents of the
specified file.
5>&1 Sends debug messages (5) function Test-Debug
and success output (1) { Get-Process PowerShell
to the success output Write-Debug "PS" }
stream. Test-Debug 5>&1
*> Sends all output types function Test-Output
to the specified file. { Get-Process PowerShell, none
Write-Warning "Test!"
*>> Appends all output types Write-Verbose "Test Verbose"
to the contents of the Write-Debug "Test Debug" }
specified file.
Test-Output *> Test-Output.txt
*>&1 Sends all output types Test-Output *>> Test-Output.txt
(*) to the success output Test-Output *>&1
stream.