2012-06-16 52 views
2

我在Windows 2003和Windows 2008中运行Powershell,两者都运行2.0,但在Windows 2003通配符似乎不被接受(或至少不以同样的方式作出反应)。例如:Powershell Windows 2003通配符不工作

((get-counter -counter '\process(w3*)\id process').CounterSamples) 

正常工作在Windows 2008,但在Windows 2003中失败

((get-counter -counter '\process(w3wp)\id process').CounterSamples) 

正常工作在Windows 2003

我怎样才能筛选基于通配符在Windows 2003?

回答

2

嗨,我有这样的问题,它似乎是由于Windows Server 2003和2008如何工作而不是PowerShell自身之间的差异造成的,所以我做的是编写一个代码块来检测Windows Server的版本我正在运行,然后更改我要执行的代码。您可以使用下面的代码或将其放在switch语句中。

$WindowsVesrion = Get-WmiObject win32_operatingSystem 

IF ($WindowsVesrion.Version -gt 6.0) 
    { 
    ((get-counter -counter '\process(w3*)\id process').CounterSamples) 
    } 
ELSE { 
    ((get-counter -counter '\process(w3wp)\id process').CounterSamples) 
    }