PowerShell 脚本问题运算符 [英] PowerShell script issue operators
问题描述
在部署更新的驱动程序之前,我尝试使用 PowerShell 检测英特尔 NIC 驱动程序.我稍微更改了脚本以进行故障排除,以确保我捕获了正确的驱动程序版本,并最终验证了操作符的正确使用(我可能不是).
I am attempting to use PowerShell to detect Intel NIC drivers, prior to deploying the updated drivers. I changed my script a bit to troubleshoot, to make sure I am capturing the correct driver versions, and eventually verifying proper use of operators (which I may not be).
#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
显然,这是不对的,所以一些测试......
Clearly, this is not right, so some testing…
将 -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
...看起来不错,但是...
..which looks good, but…
将 -lt
改为 -gt
输出
PS> .\testIntelNIC.ps1
12.6.47.1
12.15.31.0
driver is current
我是否错误地使用了运算符?还是 if/else?
Am I using the operators incorrectly? Or the if/else?
推荐答案
字符串比较对版本字符串的内部结构一无所知.将字符串转换为 Version
对象,您将能够进行适当的比较.请注意,您需要展开 DriverVersion
属性才能进行转换.
String comparisons don't know anything about the inner structure of your version strings. Cast the strings to Version
objects and you'll be able to do proper comparisons. Note that you need to expand the DriverVersion
property for the conversion to work.
[Version]$DeployVersion = "12.15.31.0"
...
[Version]$CurrentlyInstalledDriverVersion = Get-WmiObject Win32_PnPSignedDriver |
Where-Object {
$_.deviceclass -match "NET" -and
$_.devicename -like "*$INTELNICMODEL*" -and
$_.driverversion
} | Select -Expand driverversion
这篇关于PowerShell 脚本问题运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!