powershell 中的 netsh 命令输出日志记录
下面的脚本工作正常并显示我需要的所有信息 - 但是,显示 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是因为 LogWrite 函数只是将其输入转换为字符串并将其“转储”到日志文件中。对您来说最简单的解决方案是将其替换为附加
| Out-File -Append
到您想要放入日志中的每个帖子:将产生:(
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:will produce:
(The
Select-Object
is a simple way to skip some beginning lines from output)您可以使用简单的正则表达式跳过“页眉/页脚”行,该正则表达式获取与冒号字符匹配的行:
You can skip the "header/footer" lines with a simple regular expression that gets the lines that matches the colon character: