删除powershell中命令输出之间的空格 [英] Remove white spaces between commands output in powershell

查看:52
本文介绍了删除powershell中命令输出之间的空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本,我想删除 powershell 默认在输出结果中放置的空格.有什么办法吗?

I've got an script and I want to remove the white spaces that powershell puts by default in the output result. Is there any way of doing it?

=======Computer1=======
Microsoft Windows 8.1 Pro


Name         : Computer1
Model        : Vostro 200
Manufacturer : Dell Inc.





SerialNumber : 012345

这就是我想要的:

=======Computer1=======
Microsoft Windows 8.1 Pro
Name         : Computer1
Model        : Vostro 200
Manufacturer : Dell Inc.
SerialNumber : 012345

这是我的脚本:

$Computers=Import-Csv C:\Powershell\test.csv
$ResultsPath="C:\Powershell\test.txt"


foreach ($i in $Computers.Name) {
         "="*7 + $i + "="*7 
         if (Test-Connection $i -quiet) {
         (Get-WmiObject -class Win32_OperatingSystem -ComputerName $i).Caption
         Get-WmiObject -class Win32_Computersystem -ComputerName $i | Select-Object Name, Model, Manufacturer | Format-List
         Get-WmiObject win32_SystemEnclosure -ComputerName $i | Select-Object SerialNumber | Format-List }
         else { "nothing" }
         }

推荐答案

虽然 Trim 可以满足您的需求,但这不是 PowerShell 的方式.这是修改后的脚本,它在内部处理对象并按照您想要的方式写入输出.

While Trim will do what you need, this is not a PowerShell way. Here is revised script, that works with objects internally and writes output the way you want.

$Computers = Import-Csv 'C:\Powershell\test.csv'
$ResultsPath = 'C:\Powershell\test.txt'

foreach ($i in $Computers.Name) {
    $Header = '='*7 + $i + '='*7
    Write-Output $Header

    if (Test-Connection $i -quiet)
    {
        $Os = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $i | Select-Object -ExpandProperty Caption
        $Info = Get-WmiObject -Class Win32_Computersystem -ComputerName $i  | Select-Object Name, Model, Manufacturer
        $Sn = Get-WmiObject -Class Win32_SystemEnclosure -ComputerName $i | Select-Object -ExpandProperty SerialNumber

        $PC = New-Object -TypeName psobject -Property @{
            OperatingSystem = $Os
            Name = $Info.Name
            Model = $Info.Model
            Manufacturer = $Info.Manufacturer
            SerialNumber = $Sn
        }   | Select-Object OperatingSystem, Name, Model, Manufacturer, SerialNumber

        Write-Output ($PC | Format-List | Out-String).Trim()
    }
    else
    {
        Write-Output 'nothing'
    }
}

这篇关于删除powershell中命令输出之间的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆