2017-10-16 95 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 
的參數
相關問題