powershell - How to get cleaner output with/without -replace -
the below script provide ntp status of server; output not clean. used "-replace" cleaner output. there better , cleaner option same output without replace?
function get-ntpstatus{ param([string]$computername=$env:computername) process{ if ($_) {$computername=$_} $computerobj = "" | select "server name","last time updated 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 source" = $b $computerobj."last successful sync time" = $a $computerobj."time zone" = $d $computerobj."ntp errors" = $c $computerobj } } **********output -replace********** server name : test2 last time updated 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 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 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 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.) ***************************************** thanks, mike ( powershell newbie)
using -expandproperty parameter on select caption should remove @{caption= in output:
$timz = get-wmiobject -class win32_timezone -computername $computername |select -property caption -expandproperty in other instances, can use -replace on select-string operations, can clean using foreach-object this:
$ntplsst = w32tm /query /status /computer:$computername | select-string "last successful sync time:" | foreach-object{$_ -replace "last successful sync time: ",""} in above case, desired output in $ntplsst instead of copying $a
Comments
Post a Comment