如何格式化输出,使它们在.log文件中对齐?

时间:2014-02-21 21:48:05

标签: powershell

我正在为我的大学作业编写一个脚本,它基本上收集您的计算机信息并将其输出到.log文件。我已经编写了脚本但是当它将信息输出到.log文件时,安装的软件名称,已安装的软件GUID以及计算机中所有用户的名称都列出如下:

enter image description here

但我希望它看起来像这样:

enter image description here

无论如何,我可以编辑我的脚本,使其像这样?这是我的剧本:

#Checking For Log File

$LogLocation = "$Home\Desktop\"
$LogFile = "Baabbasi.log"
$TestPathResult = Test-Path $Home\Desktop\Baabbasi.log
If ($TestPathResult -eq "False") {New-Item -Path $LogLocation -Name $LogFile -ItemType File}

#The Process After
Clear-Host 

$TodaysDate =  Get-Date

$ComputerName = $env:ComputerName 

$BiosName = Get-WMIObject Win32_BIOS | Select-Object -ExpandProperty Name
$BiosVersion = Get-WMIObject Win32_BIOS | Select-Object -ExpandProperty Version

$HDSizes = Get-WMIObject Win32_LogicalDisk -filter "DriveType=3" | Select-Object @{Name="size(GB)";Expression={"{0:N2}" -f($_.size/1gb)}}
$TotalHDSize = ($HDSizes | Measure-Object "size(GB)" -Sum).Sum

$PhysicalMemory = (Get-WMIObject Win32_PhysicalMemory).Capacity
$PhysicalMemoryinGB = $PhysicalMemory/1gb

$OSVersion = (Get-WmiObject -Class Win32_OperatingSystem).Version
$OSName = $env:OS

$InstalledSoftwareNames = Get-WMIObject Win32_Product | Select-Object -ExpandProperty Name | Out-String

$InstalledSoftwareGUID = Get-WMIObject Win32_Product | Select-Object -ExpandProperty IdentifyingNumber| Out-String

$LatestHotfix = Get-Hotfix | select-object HotFixID,InstalledOn | Sort-Object InstalledON -descending | Select -first 1 | Select-Object -ExpandProperty HotfixID

$UserAccount = [Environment]::UserName

$AllUserAccounts = Get-WmiObject Win32_UserAccount | Select-Object -ExpandProperty Name | Out-String

Add-Content $Home\Desktop\Baabbasi.log "
Date: $TodaysDate
======================================================================
Computer Name: $ComputerName
================ ====================================================== 
BIOS Name: $BiosName
BIOS Version: $BiosVersion
HD Size: $TotalHDSize GB
RAM Size: $PhysicalMemoryinGB GB
Operating System: $OSName
Operating System Version: $OSVersion
Installed Software Name: 
$InstalledSoftwareNames 
Installed Software GUID: 
$InstalledSoftwareGUID 
Last Installed Hot Fix: $LatestHotfix
Name of Registered System User: $UserAccount
Names of All Registered System Users on the System: 
$AlluserAccounts
========================================================================
========================================================================
"

1 个答案:

答案 0 :(得分:3)

更改声明

$InstalledSoftwareGUID = Get-WMIObject Win32_Product |
    Select-Object -ExpandProperty IdentifyingNumber| Out-String

这样的事情:

$InstalledSoftwareGUID = Get-WMIObject Win32_Product |
    Select-Object -ExpandProperty IdentifyingNumber |
    % { (' ' * 20) + $_ } | Out-String

在将列表转换为单个字符串之前,这将在每个GUID前面加上20个空格(将数字调整到所需的缩进深度)。