powershell脚本的格式输出

时间:2018-03-27 07:45:05

标签: powershell

我已运行以下代码来收集网络上所有计算机的信息。

import-module activedirectory

$getget = Get-ADComputer -Filter "OperatingSystem -like 'Windows 10 *'" - Property * | select name -ExpandProperty Name 


echo $getget

foreach ($new1 in $getget) {


$bios = Get-WmiObject Win32_BIOS -ComputerName $new1
$Proc = Get-WmiObject Win32_processor -ComputerName $new1
$memory = Get-WmiObject Win32_physicalmemory -ComputerName $new1
$system = Get-WmiObject Win32_ComputerSystem -ComputerName $new1
$opera = Get-WmiObject win32_operatingsystem -ComputerName $new1
$ip = [System.Net.Dns]::GetHostAddresses("$new1")
$TotalSlots = (Get-WmiObject -Class "win32_PhysicalMemoryArray" - ComputerName $new1).MemoryDevices 


$Object = New-Object PSObject -Property @{

'OPerating system'     = $opera.Caption
IP                     = $ip.IPAddressToString 
UserName               = $system.UserName
ComputerName           = $proc.SystemName
Model                  = $system.Model
'Serial Number'        = $bios.SerialNumber
'Processor Name'       = $proc.name
'RAM (GB)'             = ($system.TotalPhysicalMemory / 1GB -as [int])
'Used RAM slot'        = $memory.count
'Total RAM slot'       = $totalslots}

Write-output $Object | Out-File "C:\web\.info.txt" -append
ECHO $object
}

结果是大文本文件,格式如下所示,网络上每100台机器的文本块。

ComputerName     : MYMACHINE01
Processor Name   : Intel(R) Core(TM) XXXXXXXXXXXX
RAM (GB)         : 4
UserName         : 
IP               : xxx.xx.xxx.xxx
Total RAM slot   : 4
Used RAM slot    : 
OPerating system : Microsoft Windows 
Serial Number    : XXXXXXXXXXXX
Model            : OptiPlex XXXXXXX


ComputerName     : MACHINE02
Processor Name   : Intel(R) Core(TM) XXXXXXXXXXX
RAM (GB)         : 8
UserName         : XXXXXXXXXXX\XXXXXXX
IP               : XXX.XXX.XXX.XXX
Total RAM slot   : 2
Used RAM slot    : 2
OPerating system : Microsoft Windows 
Serial Number    : XXXXXXXXX
Model            : OptiPlex XXXXXXX

如何更改代码,使左侧的标签成为列标题,每台机器成为一行? e.g:

ComputerName, Processor Name, RAM (GB), UserName,       IP,      Total RAM slot, Used RAM slot, OPerating system, Serial Number,   Model    

MYMACHINE01   INTEL CORE      16        XXXXXXX  XXX.XXX.XXX.XXX       X                X         windows           XXXXXXXXX      XXXXX
MACHINE02     XXXXXXXXX        8        XXXXXXX  XXX.XXX.XXX.XXX       X                X         windows           XXXXXXXXX      XXXXX

我尝试过format-table但是我没有成功,作为一个PowerShell初学者,我可能会错过一些简单的东西。对此的任何帮助将不胜感激。

谢谢。

1 个答案:

答案 0 :(得分:0)

在将对象写入文件之前,您需要将对象传递给Format-Table

Write-Output $Object | Format-Table | Out-File "C:\web\.info.txt" -append