我在讓這個腳本運行兩個陳述句時遇到了問題。我必須運行兩次該腳本才行。當我運行一次時,它完成了第一條陳述句并安裝了驅動程式。但它沒有做第二條。但當我再次運行它時,它就會執行第二條。如果有任何幫助的話,我將不勝感激。另外,請原諒我的腳本寫得不夠好。我知道我也可以用更少的代碼來做這個。我正在學習,而且是在我的褲子上飛行。下面是我的腳本:
$devicesTwo = Get-WmiObject Win32_PNPEntity | Wher-Object{$_. ConfigManagerErrorCode -ne 0}。| Select-Object -Property CompatibleID
$devices = Get-WmiObject Win32_PNPEntity | Where-Object{$_.ConfigManagerErrorCode -ne 0}。| Select-Object -Property HardwareId
$unknown_Dev = $devices -屬性 HardwareId
$unknown_Dev_Two=$devicesTwo
ForEach($ProblemDevice in $unknown_Dev) {
$ProblemDevice.HardwareID[0]
$test = $ProblemDevice。
}
ForEach($ProblemDeviceTwo in $unknown_Dev_Two) {
$ProblemDeviceTwo.CompatibleID[0]
$testTwo = $ProblemDeviceTwo。
}
#Get List of Drivers from Folder[/span]}。
$folderWithDrivers = "C:UsersTechDesktopDrivers"
#收集驅動資訊
$drivers = Get-ChildItem $folderWithDrivers -Include *.inf -Recurse
#$drivers | 選擇名稱
forEach ($device in $drivers){
$found = Get-Content $device
if ($found | Select-String $test.HardwareID -SimpleMatch) {
pnputil /add-driver $device /install
}
elseif ($found | Select-String $testTwo.CompatibleID -SimpleMatch) {
pnputil /add-driver $device /install
}
else {
Write-Host "未找到"。
}
uj5u.com熱心網友回復:
從我的評論中繼續下去。...這里有一些快速的腳本,可能對你有用。
# Assign folder檔案夾路徑,包含驅動程式到變數。
$Divers_Path = "C:UsersTechDesktopDrivers"/span>
# 獲取我的Drivers檔案夾中所有.inf檔案的串列。
$Driver_Files = Get-ChildItem -Path $Divers_Path -Filter '*.inf' -File -Recurse -ErrorAction SilentlyContinue
# Get all unknown devices -File -Recurse -ErrorAction SilentlyContinue
$UNK_Devices = Get-CimInstance -Query "Select Name,CompatibleID,HardwareID From Win32_PNPEntity Where ConfigManagerErrorCode != 0"
foreach ($Device in $UNK_Devices)
{
$MaxCount = ($Device.HardwareID.Count, $Device.CompatibleID.Count | Measure-Object -Maximum) 。
if ($MaxCount -ge 1) {
# 這里的東西。最好是一條粗略的資訊:)
for ($i=0; $i -lt $MaxCount; $i )
{
$HWID_Found_Match = try { Select-String -Path $Driver_Files -Pattern $Device. HardwareID[$i] -SimpleMatch } catch { }
if ($HWID_Found_Match) {
foreach ($File in $HWID_Found_Match.Path)
{
PNPUtil.exe /Add-Driver $File /Install
}
}
else {
# 沒有找到匹配的$HWID。
}
$CID_Found_Match = try { Select-String -路徑 $Driver_Files -模式 $Device. CompatibleID[$i] -SimpleMatch } catch { }
if ($CID_Found_Match) {
foreach ($File in $CID_Found_Match.Path)
{
PNPUtil.exe /Add-Driver $File /Install
}
}
else {
# 沒有找到匹配的$CID。
}
}
}
else {
"沒有找到匹配的:$($Device.Name)" }
}
}
老實說,使用switch陳述句會讓你受益更多,但是,這可以做到。至于你的問題,就像我的評論中提到的,只有第一個條件被滿足才會運行:
$One = 'one'/span>
if ($One -eq 'One') {
# 條件滿足。
# 只運行這個代碼塊。
'One Found'。
}
elseif ($One -eq 'One') {
# 條件得到滿足,但是,第一個 "if "也得到滿足,所以這個。
# 將不會運行。
'One Found Again'。
}
else {
# This runs if none of the top conditions are met. 如果上面的條件都不滿足,就會運行。
'anything'。
}
# result:
# One Found
而switch陳述句運行所有滿足條件的代碼塊,除非指定停止(break)。
$One = 'one'
switch($One) {
'one' { "I ran Once" }
'one' { "我跑了兩次" }
'二' { "二" }
'三' {'三' }
}
# result:
# I ran Once
# I ran twice
從技術上講,你可以將for回圈內的所有內容換成這樣:
$Found_Matches = try { Select-... String -Path $Driver_Files -Pattern $Device. HardwareID[$i],$Device.CompatibleID[$i] -SimpleMatch }
catch { try { Select-String -Path $Driver_Files -Pattern $Device。 HardwareID[$i] -SimpleMatch } catch { Select-String -Path $Driver_Files -Pattern $Device. CompatibleID[$i] -SimpleMatch } }
switch ($Found_Matches)
{
{ $_ -match $Device.HardwareID[$i] } { $_.Path}
{ $_ -match $Device.CompatibleID[$i]} . { $_.Path }
請原諒丑陋的try和catch塊,因為這是讓我得到的錯誤消失的唯一方法。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/309320.html
標籤:
