2017-10-16 100 views
0

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

  • 计算机名/型号/序列号。
  • 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 

回答

2

$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 
的参数
相关问题