I've searched for similar variants to what I'm trying to accomplish, and I can do it with no trouble on Linux, but Win10 is giving me a heck of a time. I'm sure it's something extremely simple that I'm simply overlooking, or a syntax I'm scrambling. What I'm trying to accomplish is to have a script run after boot to query the status of a specific driver and, if the status is OK, just continue on its merry way, or, if it has a problem, reinstall the driver from a specific source.
I have no problem doing the query the driver state part, or the reinstall part, but I can't manage to get it to recognize the fault condition to install the driver, or bypass it if everything is OK. Right now, I have the following to query the driver state:
wmic path win32_VideoController WHERE VideoProcessor="Video Processor Name" GET Status > C:\destination.txt
This gives me an output file consisting of four lines, top two empty, the third being "Status" and the fourth being whatever the status value is. (E.g. "OK", "Degraded", et cetera.)
For the end result, I'd like to then run a small script which reads whatever that status value is, and then determines whether to skip to the end, or install the driver from source using:
pnputil.exe -a c:\INF_Source_Dir\INF_Source_File.inf
For the middle bit, being that I don't know explicitly what that source variable is, I can't do a Findstr for it, and I've tried variants of the following, but none seem to change the variable I name:
set vidstatus=
for /F "tokens=" %%a in (c:\destination.txt) do set vidstatus=!vidstatus!
set var!vidstatus!=%%x
I haven't delved into PowerShell yet to see if I can do something similar, but I'm down for exploring that, too, starting here and then narrowing it down to just the problematic driver, but I'd still have to resolve taking that output and calling (or skipping) the driver install.
$DeviceState = Get-WmiObject -Class Win32_PnpEntity -ComputerName localhost -Namespace Root\CIMV2 | Where-Object {$_.ConfigManagerErrorCode -gt 0
}
$DevicesInError = foreach($Device in $DeviceState){
$Errortext = switch($device.ConfigManagerErrorCode){
0 {"This device is working properly."}
1 {"This device is not configured correctly."}
2 {"Windows cannot load the driver for this device."}
3 {"The driver for this device might be corrupted, or your system may be running low on memory or other resources."}
4 {"This device is not working properly. One of its drivers or your registry might be corrupted."}
5 {"The driver for this device needs a resource that Windows cannot manage."}
6 {"The boot configuration for this device conflicts with other devices."}
7 {"Cannot filter."}
8 {"The driver loader for the device is missing."}
9 {"This device is not working properly because the controlling firmware is reporting the resources for the device incorrectly."}
10 {"This device cannot start."}
11 {"This device failed."}
12 {"This device cannot find enough free resources that it can use."}
13 {"Windows cannot verify this device's resources."}
14 {"This device cannot work properly until you restart your computer."}
15 {"This device is not working properly because there is probably a re-enumeration problem."}
16 {"Windows cannot identify all the resources this device uses."}
17 {"This device is asking for an unknown resource type."}
18 {"Reinstall the drivers for this device."}
19 {"Failure using the VxD loader."}
20 {"Your registry might be corrupted."}
21 {"System failure: Try changing the driver for this device. If that does not work, see your hardware documentation. Windows is removing this device."}
22 {"This device is disabled."}
23 {"System failure: Try changing the driver for this device. If that doesn't work, see your hardware documentation."}
24 {"This device is not present, is not working properly, or does not have all its drivers installed."}
25 {"Windows is still setting up this device."}
26 {"Windows is still setting up this device."}
27 {"This device does not have valid log configuration."}
28 {"The drivers for this device are not installed."}
29 {"This device is disabled because the firmware of the device did not give it the required resources."}
30 {"This device is using an Interrupt Request (IRQ) resource that another device is using."}
31 {"This device is not working properly because Windows cannot load the drivers required for this device."}
}
[PSCustomObject]@{
ErrorCode = $device.ConfigManagerErrorCode
ErrorText = $Errortext
Device = $device.Caption
Present = $device.Present
Status = $device.Status
StatusInfo = $device.StatusInfo
}
}
if(!$DevicesInError){
write-host "Healthy"
} else {
$DevicesInError
}