如何使用/不使用-replace获得更清晰的输出

时间:2014-03-18 17:29:48

标签: powershell replace

以下脚本提供服务器的NTP状态;但输出不干净。我使用" -replace"为了清洁输出。是否有更好更清洁的选择来获得相同的输出而无需替换?

    Function get-NTPStatus{
Param([string]$computername=$env:computername)
Process{
if ($_) {$computername=$_}
    $Computerobj = "" | Select "Server Name","Last Time Updated From Source","Last Successful Sync Time","Time Zone","NTP Errors"
    $ntplsst = w32tm /query /status /computer:$computername | Select-String "Last Successful Sync Time:"
    $a =$ntplsst -replace "Last Successful Sync Time:",""
    $ntps = w32tm /query /status /computer:$computername | Select-String "Source:"
    $b =$ntps -replace "Source:",""
    $ntper = w32tm /query /status /verbose /computer:$computername | Select-String "Last Sync Error:"
    $c =$ntper -replace "Last Sync Error:",""
    $timz = Get-WmiObject -Class win32_timezone -ComputerName $computername |Select -Property Caption
    $d =$timz -replace "@{Caption=",""
$Computerobj."Server Name" = $computername
$Computerobj."Last Time Updated From Source" = $b
$Computerobj."Last Successful Sync Time" = $a
$Computerobj."Time Zone" = $d
$Computerobj."NTP Errors" = $c
$Computerobj    
}
}


**********Output with -replace**********

Server Name                   : TEST2
Last Time Updated From Source :  time.microsoft.com
Last Successful Sync Time     :  3/18/2014 12:57:20 PM
Time Zone                     : (UTC-05:00) Eastern Time (US & Canada)}
NTP Errors                    :  0 (The command completed successfully.)
*****************************************

Function get-NTPStatus{
Param([string]$computername=$env:computername)
Process{
if ($_) {$computername=$_}
    $Computerobj = "" | Select "Server Name","Last Time Updated From Source","Last Successful Sync Time","Time Zone","NTP Errors"
    $ntplsst = w32tm /query /status /computer:$computername | Select-String "Last Successful Sync Time:"
    $ntps = w32tm /query /status /computer:$computername | Select-String "Source:"
    $ntper = w32tm /query /status /verbose /computer:$computername | Select-String "Last Sync Error:"
    $timz = Get-WmiObject -Class win32_timezone -ComputerName $computername |Select -Property Caption
$Computerobj."Server Name" = $computername
$Computerobj."Last Time Updated From Source" = $ntps
$Computerobj."Last Successful Sync Time" = $ntplsst
$Computerobj."Time Zone" =$timz
$Computerobj."NTP Errors" = $ntper
$Computerobj    
}
}

**********Output without -replace**********

Server Name                   : TEST2
Last Time Updated From Source : Source: time.microsoft.com
Last Successful Sync Time     : Last Successful Sync Time: 3/18/2014 12:57:20 PM
Time Zone                     : @{Caption=(UTC-05:00) Eastern Time (US & Canada)}
NTP Errors                    : Last Sync Error: 0 (The command completed successfully.)
*****************************************

谢谢, 迈克(Powershell Newbie)

1 个答案:

答案 0 :(得分:2)

使用-ExpandProperty Select上的Caption参数应删除输出中的@{Caption=

$timz = Get-WmiObject -Class win32_timezone -ComputerName $computername |Select -Property Caption -ExpandProperty

在其他情况下,您可以在-replace操作中使用Select-String,但可以使用ForEach-Object进行清理:

$ntplsst = w32tm /query /status /computer:$computername | Select-String "Last Successful Sync Time:" | ForEach-Object{$_ -replace "Last Successful Sync Time: ",""}

在上述情况下,您可以在$ntplsst中获得所需的输出,而不是复制到$a