【问题标题】:powershell computer information scriptpowershell 计算机信息脚本
【发布时间】:2017-10-16 11:59:48
【问题描述】:

我需要创建一个可以双击的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

【问题讨论】:

    标签: powershell get-wmiobject


    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-29
      • 2020-06-05
      • 2011-08-02
      • 1970-01-01
      • 2019-06-05
      • 1970-01-01
      • 2023-01-13
      相关资源
      最近更新 更多