简体   繁体   English

从文本文件中更改值设置变量-Windows 批处理

[英]set variable from changing value in text file-Windows batch processing

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.我已经搜索了与我想要完成的任务类似的变体,并且我可以在 Linux 上毫无困难地做到这一点,但 Win10 给了我很多时间。 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.我在查询驱动程序 state 部分或重新安装部分没有问题,但我无法让它识别安装驱动程序的故障条件,或者如果一切正常则绕过它。 Right now, I have the following to query the driver state:现在,我有以下查询驱动程序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.这给了我一个 output 文件,该文件由四行组成,前两行是空的,第三行是“状态”,第四行是状态值。 (Eg "OK", "Degraded", et cetera.) (例如“OK”、“Degraded”等。)

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:对于中间位,由于我不明确知道该源变量是什么,因此我无法为其执行 Findstr,并且我尝试了以下变体,但似乎没有人更改我命名的变量:

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.我还没有深入研究 PowerShell 看看我是否可以做类似的事情,但我也想探索这个,从这里开始,然后将其缩小到有问题的驱动程序,但我仍然必须解决采取output 并调用(或跳过)驱动程序安装。

$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
}

The accepted answer in this question should work here as well.. I just tried it by stopping a service and then giving that service's name in the script.. it started it.这个问题中接受的答案也应该在这里起作用..我只是通过停止服务然后在脚本中给出该服务的名称来尝试它..它启动了它。 Every driver would have a corresponding service entry and sc commands would work on a driver service as well.每个驱动程序都有一个相应的服务条目,并且 sc 命令也适用于驱动程序服务。 Adapting this to inject fault or bypass conditions should be doable.. reproduced from the answer of the highlighted question:对此进行调整以注入故障或旁路条件应该是可行的..从突出显示的问题的答案中复制:

在此处输入图像描述

wmic output has a few quirks. wmic output 有一些怪癖。 The most important here is:这里最重要的是:
line endings are not the usual CRLF , but CRCRLF .行尾不是通常的CRLF ,而是CRCRLF A seemingly empty line isn't empty, it still has the superfluous CR .看似空的行并不是空的,它仍然有多余的CR Also this CR may become part of your variable.CR也可能成为您变量的一部分。
There are several possibilities to "correct" this behavior.有几种可能性可以“纠正”这种行为。 The safest and most general is to use another for loop:最安全和最通用的是使用另一个for循环:

for /f "delims=" %%a in ('wmic path win32_VideoController WHERE VideoProcessor^="Intel(R) UHD Graphics Family" GET Status /value^|find "="') do for /f "delims=" %%b in ("%%a") do set "%%b"
echo "%status%"

As an aside, @Mklement0 pointed me to Get-CimInstance, which provided me with the following alternative means of doing what I was trying to do, with PowerShell.顺便说一句,@Mklement0 将我指向 Get-CimInstance,它为我提供了以下替代方法来做我想做的事情,PowerShell。 This uninstalls the corrupted driver and reinstalls it from a known good source:这将卸载损坏的驱动程序并从已知的良好来源重新安装它:

$DriverInf = (Get-CimInstance Win32_VideoController -Filter "AdapterCompatibilit='Intel Corporation'").InfFilename
$SourceInf = [driver source directory\filename]
if (Get-CimInstance Win32_VideoController -filter "AdapterCompatibility='Intel Corporation'" | Where-Object {$_.ConfigManagerErrorCode -gt 0}) {pnputil.exe /delete-driver $DriverInf /uninstall /force ; pnputil.exe /add-driver $SourceInf /install |Outfile -FilePath [logfile location\filename]} else {"$Driverinf Video Driver OK" | Out-File -FilePath [Path to status file\VideoStatus.txt]}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM