netsh命令输出在powershell中记录

时间:2011-10-21 10:19:15

标签: logging powershell

以下脚本运行正常并显示我需要的所有iformation - 但是,显示NETSH命令时出现问题。因此,通常在powershell中输入以下内容:

netsh int tcp show global

它以TABLe格式显示如下:

TCP Global Parameters
----------------------------------------------
Receive-Side Scaling State          : disabled
Chimney Offload State               : disabled
NetDMA State                        : enabled
Direct Cache Acess (DCA)            : disabled
Receive Window Auto-Tuning Level    : disabled
Add-On Congestion Control Provider  : none
ECN Capability                      : disabled
RFC 1323 Timestamps                 : disabled

然而,当我在一个记录到LOG文件的powershell脚本中执行此操作时,输出都是上下文的 - 即下面的内容:

Querying active state...  TCP Global Parameters ---------------------------------------------- Receive-Side Scaling State          : disabled  Chimney Offload State               : disabled  NetDMA State                        : disabled  Direct Cache Acess (DCA)            : disabled  Receive Window Auto-Tuning Level    : disabled  Add-On Congestion Control Provider  : none  ECN Capability                      : disabled  RFC 1323 Timestamps                 : disabled  

知道如何保持powershell命令提示符显示的TABLE格式吗?

脚本:

 $computerdel = gc env:computername

 $t = "D:\temp\$(gc env:computername).log"
 #$e = Test-Path $t rm $t -ErrorAction SilentlyContinue

 #if ( $e -eq $true ) { rm $t }
 #else { Write-Host "Shortcut does not exist." }


 $Logfile = "D:\temp\$(gc env:computername).log"

 Function LogWrite {    Param ([string]$logstring)

    Add-content $Logfile -value $logstring }

 LogWrite
 -----------------------------------------------------------------------------------------------------------------------------------------------

 $Date = Get-Date

 LogWrite "Script has been run on $Date - This is Servers Local Time"
 LogWrite $computer = gc env:computername $onetcp = ((get-childitem
 c:\windows\system32\drivers\tcpip.sys).Versioninfo.ProductMajorPart).tostring()
 $twotcp = ((get-childitem
 c:\windows\system32\drivers\tcpip.sys).Versioninfo.ProductMinorPart).tostring()
 $threetcp = ((get-childitem
 c:\windows\system32\drivers\tcpip.sys).Versioninfo.ProductBuildPart).tostring()
 $fourtcp = ((get-childitem
 c:\windows\system32\drivers\tcpip.sys).Versioninfo.ProductPrivatePart).tostring()


 $onedfsr = ((get-childitem
 c:\windows\system32\dfsrs.exe).Versioninfo.ProductMajorPart).tostring()
 $twodfsr = ((get-childitem
 c:\windows\system32\dfsrs.exe).Versioninfo.ProductMinorPart).tostring()
 $threedfsr = ((get-childitem
 c:\windows\system32\dfsrs.exe).Versioninfo.ProductBuildPart).tostring()
 $fourdfsr = ((get-childitem
 c:\windows\system32\dfsrs.exe).Versioninfo.ProductPrivatePart).tostring()

 $hotfix1 = Get-HotFix -Id KB2450944 -ErrorAction SilentlyContinue
 $hotfix2 = Get-HotFix -Id KB2582284 -ErrorAction SilentlyContinue
 #$hotfix3 = Get-HotFix -Id KB979808 -ErrorAction SilentlyContinue

 LogWrite

 If ($hotfix2) {    LogWrite "Hotfix KB2582284 is installed - This is
 TCPIP.sys Upgrade Hotfix" -BackgroundColor Green -ForegroundColor
 Black  } else {    LogWrite "Hotfix KB2582284 is NOT installed - Please
 ensure you install this hotfix - This is DFSRS.exe Upgrade Hotfix"
 -ForegroundColor "red"     }

 LogWrite "TCPIP.sys Version on $computer is:
 ""$onetcp.$twotcp.$threetcp.$fourtcp"" " LogWrite If ($hotfix1) {
    LogWrite "Hotfix KB2450944 is installed - This is DFSRS.exe Upgrade
 Hotfix" -BackgroundColor Green -ForegroundColor Black  } else {
    LogWrite "Hotfix KB2450944 is NOT installed - Please ensure you
 install this hotfix - This is DFSRS.exe Upgrade Hotfix"
 -ForegroundColor "red"     }    LogWrite "DFSRS.exe Version on $computer is: ""$onedfsr.$twodfsr.$threedfsr.$fourdfsr"" "

 LogWrite

 If (get-wmiobject win32_share | where-object {$_.Name -eq "REMINST"})
 {   LogWrite "The REMINST share exists on $computer" } Else {  
 LogWrite "The REMINST share DOES NOT exist on $computer - Please
 create as per standards"  }  

 #If ($hotfix3) {
 #  LogWrite "Hotfix KB979808 is installed" -BackgroundColor Green -ForegroundColor Black
 #  }
 #else {
 #  LogWrite "Hotfix KB979808 is NOT installed - Please ensure you install this hotfix" -ForegroundColor "red"
 #}      LogWrite

 $u = "DBG\ADS-ALL-ROFS-Manager"; net localgroup "Distributed COM
 Users" | Where {$_ -match $u}

 If ($u) {  LogWrite "DBG\ADS-ALL-ROFS-Manager is part of Distributed
 COM Users localgroup on $computer"     } else {    LogWrite
 "DBG\ADS-ALL-ROFS-Manager is NOT part of Distributed COM Users
 localgroup on $computer"   } LogWrite $disabletaskoffload =
 Get-ItemProperty
 'HKLM:\SYSTEM\CurrentControlSet\Services\TCPIP\Parameters' -Name
 'DisableTaskOffload' | fl  DisableTaskOffload -ErrorAction
 SilentlyContinue $EnableTCPChimney = Get-ItemProperty
 'HKLM:\SYSTEM\CurrentControlSet\Services\TCPIP\Parameters' -Name
 'EnableTCPChimney' | fl  EnableTCPChimney $EnableTCPA =
 Get-ItemProperty
 'HKLM:\SYSTEM\CurrentControlSet\Services\TCPIP\Parameters' -Name
 'EnableTCPA' | fl  EnableTCPA $EnableRSS = Get-ItemProperty
 'HKLM:\SYSTEM\CurrentControlSet\Services\TCPIP\Parameters' -Name
 'EnableRSS' | fl  EnableRSS

 If ($disabletaskoffload) {     LogWrite "Registry Key DisableTaskOffload
 is set" -BackgroundColor Green -ForegroundColor Black  } else {
    LogWrite "Registry Key DisableTaskOffload is NOT set"
 -ForegroundColor "red"     }    If ($EnableTCPChimney) {   LogWrite "Registry Key EnableTCPChimney is set" -BackgroundColor Green
 -ForegroundColor Black     } else {    LogWrite "Registry Key EnableTCPChimney is NOT set" -ForegroundColor "red"  }    If
 ($EnableTCPA) {    LogWrite "Registry Key EnableTCPA is set"
 -BackgroundColor Green -ForegroundColor Black  } else {    LogWrite "Registry Key EnableTCPA is NOT set" -ForegroundColor "red"    }    If
 ($EnableRSS) {     LogWrite "Registry Key EnableRSS is set"
 -BackgroundColor Green -ForegroundColor Black  } else {    LogWrite "Registry Key EnableRSS is NOT set" -ForegroundColor "red"     } $netsh =
 netsh int tcp show global LogWrite LogWrite "***Running Netsh
 Check***" LogWrite "Ensure the following are set to disabled:"
 LogWrite "Receive-Side Scaling State: Disabled" LogWrite "Chimney
 Offload State: Disabled" LogWrite "Receive Window Auto-Tuning Level:
 Disabled" LogWrite LogWrite $netsh LogWrite

2 个答案:

答案 0 :(得分:2)

那是因为您的LogWrite函数只是将其输入转换为字符串并将其“转储”到日志文件中。对您来说最简单的解决方案是将| Out-File -Append <logfile>替换为要添加到日志中的每个帖子来替换它:

>"Some message" | Out-File -Append <logfile>
>netsh int tcp show global | Select-Object -skip 2 | Out-File -Append <logfile>

将产生:

Some message
TCP Global Parameters
---------------------------------------------- 
Receive-Side Scaling State          : enabled
Chimney Offload State               : automatic
NetDMA State                        : enabled
Direct Cache Acess (DCA)            : disabled
Receive Window Auto-Tuning Level    : normal
Add-On Congestion Control Provider  : none
ECN Capability                      : disabled
RFC 1323 Timestamps                 : disabled

Select-Object是从输出跳过一些起始行的简单方法)

答案 1 :(得分:1)

您可以使用简单的正则表达式跳过“页眉/页脚”行,该正则表达式获取与冒号字符匹配的行:

netsh int tcp show global | where {$_ -match ':'}