我有以下腳本,它提取驅動程式詳細資訊并提取到文本檔案 driveroutput.txt,然后將 oem 編號從已發布名稱提取到文本檔案呼叫 oem.txt 我然后使用每一行洗掉 oem# 的驅動程式。 inf 也有關系。
$driveroutput = "C:\temp\driveroutput.txt"
$oemoutput = "C:\temp\oem.txt"
pnputil /enum-drivers > $driveroutput
Get-Content $driveroutput | Select-String -Pattern "Published Name" | %{$_.Line.Split(" ")} | Select-String -Pattern ".inf" | Where { $_ } | Set-Content $oemoutput
foreach($line in Get-Content $oemoutput) {
pnputil.exe /delete-driver $line
}
driveroutput.txt 檔案示例
Published Name: oem24.inf
Original Name: ntprint.inf
Provider Name: Microsoft
Class Name: Printers
Class GUID: {4d36e979-e325-11ce-bfc1-08002be10318}
Driver Version: 06/21/2006 10.0.17763.1217
Signer Name: Microsoft Windows
Published Name: oem11.inf
Original Name: ntprint.inf
Provider Name: Microsoft
Class Name: Printers
Class GUID: {4d36e979-e325-11ce-bfc1-08002be10318}
Driver Version: 06/21/2006 10.0.17763.771
Signer Name: Microsoft Windows
Published Name: oem13.inf
Original Name: nvhda.inf
Provider Name: NVIDIA Corporation
Class Name: Sound, video and game controllers
Class GUID: {4d36e96c-e325-11ce-bfc1-08002be10318}
Driver Version: 12/15/2017 1.3.36.6
Signer Name: Microsoft Windows Hardware Compatibility Publisher
Published Name: oem16.inf
Original Name: nv_dispi.inf
Provider Name: NVIDIA
Class Name: Display adaptors
Class GUID: {4d36e968-e325-11ce-bfc1-08002be10318}
Driver Version: 03/23/2018 23.21.13.9135
Signer Name: Microsoft Windows Hardware Compatibility Publisher
oem.txt 檔案示例
oem24.inf
oem11.inf
oem13.inf
oem16.inf
有誰知道我如何通過包含“NVIDIA”字樣的提供商名稱進一步過濾。我希望運行的腳本將僅洗掉與 nvidia 相關的驅動程式而不是所有驅動程式
uj5u.com熱心網友回復:
您可以在oem*.inf不使用臨時文本檔案的情況下獲取驅動程式 (the )。
為了做到這一點,我建議首先將所有這些行與一個 NewLine 連接起來,這樣你最終就會得到一個多行字串。
然后將此字串拆分為雙換行符上的不同部分,以便為它回傳的每個驅動程式提供一組文本塊,我們可以進一步決議。
請閱讀以下代碼的行內注釋:
# First join the string array from pnputil with newlines and split on the empty lines
# that separate each driver. The result is a set of textblocks for per driver from which
# we filter out the blocks where the wanted provider is mentioned.
$drivers = ((pnputil.exe /enum-drivers) -join [environment]::NewLine -split '(\r?\n){2,}' -ne '' |
Where-Object { $_ -match 'Provider Name:. \bNVIDIA\b' })
# test if we did find NVIDIA drivers
if ($drivers) {
# Next replace the (first) colons with equal signs. Convert each block into a Hashtable using
# ConvertFrom-StringData and output the value of the property where the actual inf file is stored
$drivers | ForEach-Object {
$driver = ($_ -replace '(?<!:.*):', '=' | ConvertFrom-StringData -ErrorAction SilentlyContinue).'Published Name'
Write-Host "deleting driver using '$driver'"
# Now you can delete the driver
# for safety reasons I have commented this out:
# pnputil.exe /delete-driver $driver
}
}
else {
Write-Host 'No NVIDIA drivers found'
}
將冒號替換:為等號的正則運算式詳細資訊=:
(?<! Assert that it is impossible to match the regex below with the match ending at this position (negative lookbehind)
: Match the character “:” literally
. Match any single character
* Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
)
: Match the character “:” literally
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/331895.html
