PowerShell脚本问题运算符

时间:2016-05-20 22:10:54

标签: powershell

在部署更新的驱动程序之前,我尝试使用PowerShell检测Intel NIC驱动程序。我更改了我的脚本以进行故障排除,以确保我捕获正确的驱动程序版本,并最终验证操作符的正确使用(我可能不会)。

#Change $DeployVersion to the driver version being deployed
#Change $INTELNICMODEL to the model of Intel NIC
#=======================================================
$DeployVersion = “12.15.31.0″
$INTELNICMODEL = “82579LM”

#Get IntelNIC Driver Version from Win32_PnPSignedDriver
$CurrentlyInstalledDriverVersion = Get-WmiObject Win32_PnPSignedDriver |   Where-Object {$_.deviceclass -match “NET” -and $_.devicename -like “*$INTELNICMODEL*” -and $_.driverversion} | Select driverversion
Write $CurrentlyInstalledDriverVersion.driverversion
Write $DeployVersion
If ($CurrentlyInstalledVersion.driverversion -lt $DeployVersion)
{Write “need to update driver”}
else
{Write “driver is current”}

来自$CurrentlyInstalledDriverVersion = $DeployVersion

的计算机

输出

PS> .\testIntelNIC.ps1
12.15.31.0
12.15.31.0
need to update driver

显然,这是不对的,所以有些测试...

-lt更改为-gt

输出

PS> .\testIntelNIC.ps1
12.15.31.0
12.15.31.0
driver is current

...然后尝试-eq

输出

PS> .\testIntelNIC.ps1
12.15.31.0
12.15.31.0
driver is current

然后从$CurrentlyInstalledDriverVersion < $DeployVersion

的计算机上

输出

PS> .\testIntelNIC.ps1
12.6.47.1
12.15.31.0
need to update driver

..看起来不错,但是......

-lt更改为-gt

输出

PS> .\testIntelNIC.ps1
12.6.47.1
12.15.31.0
driver is current

我是否错误地使用了运算符?还是if / else?

1 个答案:

答案 0 :(得分:3)

字符串比较对版本字符串的内部结构一无所知。将字符串转换为Version个对象,您就可以进行适当的比较。请注意,您需要展开{​​{1}}属性才能使转换生效。

DriverVersion