powershell计算机信息脚本

时间:2017-10-16 11:59:48

标签: powershell get-wmiobject

我需要创建一个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

1 个答案:

答案 0 :(得分: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