簡體   English   中英

Powershell 類似的操作員沒有給出結果

[英]Powershell -like operator not giving results

嘗試檢查我的系統中是否安裝了軟件或不使用這個非常簡單的腳本。

$ltc = @('Microsoft Office','Python','Google Chrome')
$InstalledSoftware = Get-ChildItem "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall"
foreach($obj in $InstalledSoftware) {
        $name = $obj.GetValue('DisplayName')
        #write-host $name
        foreach($lt in $ltc){
                if($name -like $lt) {
                    write-host $obj.GetValue('DisplayName') -NoNewline; write-host " - " -NoNewline; write-host $obj.GetValue('DisplayVersion')
        }
    }
}
timeout 60

出於某種原因,它只匹配Google Chrome而不是其他兩個。 我安裝了 Office 和 Python。 不知道我哪里錯了?

使用您的原始代碼,您真的想使用 -contains 運算符與 -like 運算符。

Clear-Host

$ltc = @('Microsoft 365 - en-us','SharpKeys','Microsoft Visual Studio Code')
$InstalledSoftware = Get-ChildItem "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall"
foreach($obj in $InstalledSoftware) {
        $name = $obj.GetValue('DisplayName')
       # write-host $name
         if($ltc -contains $name) {
           "$($obj.GetValue('DisplayName')) - $($obj.GetValue('DisplayVersion'))"
        }
}

使用我計算機上的程序對 Output 進行示例:

Microsoft 365 - en-us - 16.0.15427.20210
SharpKeys - 3.9.2000
Microsoft Visual Studio Code - 1.70.2

我還修改了您的 output 語句以使其更簡單。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM