2013-04-09 52 views
1

我有下面,我一直在努力去工作,但目前不能。我希望它通過並獲得netsh命令的輸出,但我需要的只是檢查三個輸出。TCP煙囪命令通過Powershell

下面是一個命令輸出,如果我只是運行它:

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


Receive-Side Scaling State   : disabled 
Chimney Offload State    : disabled 
Direct Cache Acess (DCA)   : disabled 
Receive Window Auto-Tuning Level : disabled 
ECN Capability      : disabled 
RFC 1323 Timestamps     : disabled 

Nowthen,如果我運行下面例如,爲了獲得「煙囪卸載狀態」,並檢查它被設置爲禁用,那麼它失敗,並經過到ELSE statment稱它設置爲Enabled時,它不是...任何想法如何解決這個問題,請:

代碼:

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

if ($netsh -eq 'Chimney Offload State    : disabled') 
{ 
    Write-Host "TCP Chimney disabled" 
} 
else 
{ 
    Write-Host "TCP Chimney is ENABLED - WRONG!!!" 
} 

回答

5

另一種方法是將對象化的結果:

$pso = New-Object -TypeName PSObject 

netsh int tcp show global | where {$_ -match ':'} | foreach{ 
    $item = $_.Split(':') -replace '\s|-|\(|\)' 
    $pso | Add-Member -MemberType NoteProperty -Name $item[0] -Value $item[1] 
} 

if($pso.ChimneyOffloadState -eq 'disabled') 
{ 
    ... 
} 
+0

這是很好的!我會試試這個 - 看起來好多了。感謝Shay – lara400 2013-04-09 13:53:28

0

得到了它......它是添加一個 - 匹配的聲明.. 。

if ($netsh -MATCH 'Chimney Offload State    : disabled')