【问题标题】:"Windows was unexpected at this time" error when trying to compare 2 strings in batch script尝试比较批处理脚本中的 2 个字符串时出现“此时 Windows 出现意外”错误
【发布时间】:2014-03-07 22:37:43
【问题描述】:

我正在编写一个批处理脚本来使用 USMT 将计算机从 XP 更新到 7。因为 USMT 有一个需要在操作系统升级之前运行的扫描状态组件和一个必须在操作系统升级之后运行的负载状态计算机尝试使用 if 语句检查操作系统是什么,然后运行正确的命令。我是批处理文件的新手,但从我一直在阅读的所有内容来看,我似乎在正确地编写它,但我显然在某个地方搞砸了。我收到“此时 Windows 出现意外错误”。我还知道,由于我包含了暂停命令,因此变量设置正确。我还尝试使用IF %WINVERSION% == %XP% goto XPTRUE/WIN7TRUE 并将所有内容括在:XPTRUE/WIN7TRUE 下的括号内,但这会产生相同的错误。

::Don't have commands print...only outputs are printed
@echo off
:: Set constants
SET XP=Microsoft Windows XP [Version 5.1.2600]
SET WIN7=Microsoft Windows [Version 6.1.7601]
SET XPUSMTLOCATION=C:\Program Files\USMT\Binaries\v4\x86
SET 7USMTLOCATION=C:\Program Files (x86)\USMT\Binaries\v4\amd64
SET BACKUPLOACTION=\\[SERVER IP]\z$\UserAccountBackUps\Backups
SET LOCALBACKUPLOCATION=C:\Backup\USMT
SET NASBACKUPLOCATION=S:\UserAccountBackUps\Backups
@PAUSE
::Get the current version of Windows batch file is running on and store it in WINVERSION
FOR /f "delims=" %%A IN ('ver') DO @SET WINVERSION=%%A
echo %WINVERSION%
PAUSE
::Get the MAC address of the computer and store it in MACA
FOR /F %%A IN ('getmac') DO @SET MACA=%%A
echo The MAC Address is: %MACA%
:: Tell user about script
echo This is a script designed to migrate computers with one network card from Windows XP to Windows 7 using USMT, this script should not be used with computers that have multiple network cards
echo Xp is %XP%
echo 7 is %WIN7%
::Check to see if the current version is XP
PAUSE

IF %WINVERSION% == %XP% (
echo This is windows XP
::Change directory to the location of USMT files
cd %XPUSMTLOCATION%
::Run scanstate to create backup
scanstate.exe C:\Backup /i:"\\[SERVER IP]\z$\UserAccountBackUps\USMT_XML_Files\MigApp.xml" /i:"\\[SERVER IP]\z$\UserAccountBackUps\USMT_XML_Files\MigDocs.xml" /i:"\\[SERVER IP]\z$\UserAccountBackUps\USMT_XML_Files\MigUser.xml" /o /v:2
::Change directory to the location of where the USMT backup is
cd %LOCALBACKUPLOCATION%
::Rename the backup to the MAC Address
rename USMT.MIG %MACA%.MIG
::Map the NAS to a drive because xcopy can not take IP addresses
echo Mapping NAS to drive
::NAS is mapped to drive S, if S is used for something else change s below to different letter
net use s: \\[SERVER IP]\z$
echo Prepairing to copy backup to NAS
::Use xcopy to transfer backup file the /v ensures the files are identical
::This must be done this way because if USMT tries to backup directly to the NAS it tries to overwrite all existing files
xcopy %LOCALBACKUPLOCATION%\%MACA%.MIG %NASBACKUPLOCATION% /v
echo The copy has completed, run this batch file again after OS Upgrade
)

IF %WINVERSION% == %WIN7% (
echo This is Windows 7
PAUSE
)

当我在我的 Windows 7 计算机上运行它时,我得到了这个:

我在我的 XP 计算机上得到了相同的输出,只是它告诉我当前版本是 xp。帮助将不胜感激。

【问题讨论】:

    标签: windows batch-file if-statement windows-7 windows-xp


    【解决方案1】:

    下面一行:

    FOR /f "delims=" %%A IN ('ver') DO @SET WINVERSION=%%A
    

    在 WINVERSION 变量中存储一个字符串,其中包含多个以空格分隔的单词,例如:

    SET WINVERSION=Microsoft Windows [Version 6.2.9200]
    

    这样,下面一行:

    IF %WINVERSION% == %XP% (
    

    扩展为:

    IF Microsoft Windows [Version 6.2.9200] == Microsoft Windows XP [Version 5.1.2600] (
    

    这当然会导致语法错误!输入:IF /? 了解更多详情。

    比较两个可能包含空格的字符串的方法是将它们括在引号中:

    IF "%WINVERSION%" == "%XP%" (
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-03
      • 1970-01-01
      • 2014-01-17
      • 1970-01-01
      • 1970-01-01
      • 2022-11-18
      • 1970-01-01
      相关资源
      最近更新 更多