在电子邮件中排序和格式化
Sort and format in email
我是 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
另外,有没有办法重新排列它们出现的顺序,例如,我希望计算机名在数组中排在第一位,但它却出现在中间?
如果您有 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
我是 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
另外,有没有办法重新排列它们出现的顺序,例如,我希望计算机名在数组中排在第一位,但它却出现在中间?
如果您有 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