0

我需要创建一个 powershell 脚本,您可以双击(64 位计算机),它将输出到与 powershell 脚本位于同一位置的 .txt 文件,以生成以下信息:

  • 计算机名称/型号/序列号
  • C 驱动器大小/C 驱动器上的可用磁盘空间
  • 计算机运行的是哪个版本的操作系统
  • 谁当前登录到计算机

到目前为止(但它不是很有效)我有:

$computerSystem = get-wmiobject Win32_ComputerSystem
$computerOS = get-wmiobject Win32_OperatingSystem
$computerHDD = Get-WmiObject Win32_LogicalDisk -Filter drivetype=3

$txtObject = New-Object PSObject -property @{
    'PCName' = $computerSystem.Name
    'Model' = $computerSystem.Model
    'SerialNumber' = $computerBIOS.SerialNumber
    'HDDSize' = "{0:N2}" -f ($computerHDD.Size/1GB)
    'HDDFree' = "{0:P2}" -f ($computerHDD.FreeSpace/$computerHDD.Size)
    'OS' = $computerOS.caption
    'SP' = $computerOS.ServicePackMajorVersion
    'User' = $computerSystem.UserName
    } 

$txtObject | Select PCName, Model, SerialNumber, HDDSize, HDDFree, OS, SP, User | Get-Process | Out-File 'system-info.txt' -NoTypeInformation -Append
4

1 回答 1

1

$PSScriptRoot= 脚本启动的当前位置,因此如果您在保存路径中指定它,例如Out-File "$PSScriptRoot\system-info.txt",它将保存在与脚本相同的位置

Get-Process不能在这个位置使用

NoTypeInformation不作为参数存在Out-File

$computerSystem = get-wmiobject Win32_ComputerSystem
$computerOS = get-wmiobject Win32_OperatingSystem
$computerHDD = Get-WmiObject Win32_LogicalDisk -Filter drivetype=3

$txtObject = New-Object PSObject -property @{
    'PCName' = $computerSystem.Name
    'Model' = $computerSystem.Model
    'SerialNumber' = $computerBIOS.SerialNumber
    'HDDSize' = "{0:N2}" -f ($computerHDD.Size/1GB)
    'HDDFree' = "{0:P2}" -f ($computerHDD.FreeSpace/$computerHDD.Size)
    'OS' = $computerOS.caption
    'SP' = $computerOS.ServicePackMajorVersion
    'User' = $computerSystem.UserName
    } 

$txtObject | Select-Object PCName, Model, SerialNumber, HDDSize, HDDFree, OS, SP, User | Out-File "$PSScriptRoot\system-info.txt" -Append
于 2017-10-16T12:10:11.380 回答