PowerShell的打印GET-物理磁盘到一个文本文件

问题描述 投票:0回答:2

我需要得到各种硬盘中的所有公司的计算机使用的列表。我想最好的办法是运行输出信息到一个文本文件中的启动脚本。

我遇到的问题是打印到文本文件时。一开始物理磁盘工作在控制台,而是打印到一个文本文件时,它打印不同的东西。

$name = $env:COMPUTERNAME
$disk = get-physicaldisk | format-table -Auto deviceID,Size,BusType,MediaType,Model

New-Item -ItemType "file" -Path . -Name $name'.txt' -Value "$name `r`n$disk" -Force

我究竟做错了什么?

powershell
2个回答
1
投票

你可以出来的产品作为一个字符串。通过管道向| out-string

$name = $env:COMPUTERNAME
$disk = get-physicaldisk | format-table -Auto deviceID,Size,BusType,MediaType,Model | out-string
New-Item -ItemType "file" -Path . -Name $name'.txt' -Value "$name `r`n$($disk.ToString())" -Force

1
投票

你想输出从Get-PhysicalDisk到文件重定向:

$name = $env:COMPUTERNAME
$output_file_path = '~\temp\drives.txt'

Get-PhysicalDisk |
 Format-Table -Auto DeviceID, Size, BusType, MediaType, Model |
 Out-File -FilePath $output_file_path -Encoding 'ASCII' -Append

你可能会想,以取代与Format-TableExportTo-CSV否则,你将有一个有趣的时间后,处理所有的输出。

© www.soinside.com 2019 - 2024. All rights reserved.