在电子邮件中排序和格式化

时间:2015-02-05 15:59:14

标签: powershell powershell-v2.0 powershell-v3.0

我是Powershell的新手,我试图通过电子邮件发送以下代码的输出,如果我将它发送到控制台,它的格式很好,但当我将它输入到一封电子邮件,检索到的对象没有排成一行。

$bios = Get-WmiObject Win32_BIOS -ComputerName localhost
$os = Get-WmiObject `Win32_OperatingSystem -ComputerName localhost
$Proc = Get-WmiObject Win32_processor -ComputerName localhost | Select-Object -First 1
$memory = Get-WmiObject Win32_physicalmemory -ComputerName localhost
$system = Get-WmiObject Win32_ComputerSystem -ComputerName localhost

$Systeminfo = New-Object PSObject -Property @{
'ComputerName' = $proc.SystemName;
'Manufacturer' = $bios.Manufacturer;
'Model' = $system.Model;
'BIOS Version' = $bios.Version;
'Serial Number' = $bios.SerialNumber;
'Number of Processors ' = $system.NumberOfProcessors;
'Processor Name' = $proc.name;
'Logical Processor' = $system.NumberOfLogicalProcessors;
'Speed (MHZ)' = $proc.CurrentClockSpeed;
'RAM (GB)' = $system.TotalPhysicalMemory / 1GB -as [int];
'Used RAM slots' = $memory.count;
'OSVersion' = $os.Caption
}
$Systeminfo = $Systeminfo | Out-String

另外,有没有办法重新排列它们出现的顺序,例如,我希望computername在数组中是第一个,但它出现在中间?

1 个答案:

答案 0 :(得分:0)

如果您有PowerShell 3.0,则可以使用[ordered]

创建对象
$Systeminfo = [pscustomobject][ordered] @{
'ComputerName' = $proc.SystemName;
'Manufacturer' = $bios.Manufacturer;
'Model' = $system.Model;
'BIOS Version' = $bios.Version;
'Serial Number' = $bios.SerialNumber;
'Number of Processors ' = $system.NumberOfProcessors;
'Processor Name' = $proc.name;
'Logical Processor' = $system.NumberOfLogicalProcessors;
'Speed (MHZ)' = $proc.CurrentClockSpeed;
'RAM (GB)' = $system.TotalPhysicalMemory / 1GB -as [int];
'Used RAM slots' = $memory.count;
'OSVersion' = $os.Caption
}

那应该按照定义的顺序输出属性。否则,您需要使用select语句来订购所需的属性。

$Systeminfo | Select-Object ComputerName,Manufacturer,Model,"BIOS Version","Serial Number","Number of Processors","Processor Name","Logical Processor","Speed (MHZ)","RAM (GB)","Used RAM slots",OSVersion