gpt4 book ai didi

powershell - 如何格式化输出,以便它们都在.log文件中对齐?

转载 作者:行者123 更新时间:2023-12-02 23:28:56 24 4
gpt4 key购买 nike

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



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

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

#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
========================================================================
========================================================================
"

最佳答案

更改声明

$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个空格(将数字调整为所需的压痕深度)。

关于powershell - 如何格式化输出,以便它们都在.log文件中对齐?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21945857/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com