powershell 中的 netsh 命令输出日志记录

发布于 2024-12-11 17:07:22 字数 6379 浏览 0 评论 0原文

下面的脚本工作正常并显示我需要的所有信息 - 但是,显示 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

The below script works fine and displays all the iformation I need - however, there is a problem in displaying NETSH commands. So, normally in powershell if you enter the below:

netsh int tcp show global

It displays in the TABLe format as below:

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

However, when I do this in a powershell script that logs to a LOG file the output is all scewed - ie the below:

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  

Any idea how I can keep the TABLE format that powershell command prompt displays?

Script:

 $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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冰火雁神 2024-12-18 17:07:22

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

>"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 是从输出中跳过一些开头行的简单方法)

That's because you LogWrite function simply converts its input to a string and "dumps" it to the log file. The simplest solution for you would be to replace it with appending | Out-File -Append <logfile> to each post you want to put in the log:

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

will produce:

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

(The Select-Object is a simple way to skip some beginning lines from output)

临风闻羌笛 2024-12-18 17:07:22

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

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

You can skip the "header/footer" lines with a simple regular expression that gets the lines that matches the colon character:

netsh int tcp show global | where {$_ -match ':'} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文