简体   繁体   English

尝试在批处理脚本中比较2个字符串时出现“此时Windows意外”错误

[英]“Windows was unexpected at this time” error when trying to compare 2 strings in batch script

I am writing a batch script to use USMT to update computers from XP to 7. Because USMT has a scanstate component that needs to be run before the OS upgrade and a loadstate computer that has to be run after the OS upgrade I am trying to use an if statement to check what the operating system is and then run the proper commands. 我正在编写一个批处理脚本,以使用USMT将计算机从XP更新到7。因为USMT具有在操作系统升级之前需要运行的scanstate组件和在操作系统升级之后必须运行的loadstate计算机。 if语句,检查操作系统是什么,然后运行正确的命令。 I am new to batch files but from everything I have been reading it seems like I am writing it properly but I am obviously messing up somewhere. 我是批处理文件的新手,但从我读过的所有内容来看,似乎我都在正确地编写它,但是显然我在某个地方弄乱了。 I am getting a "Windows is unexpected at this time error." 我收到“此时Windows意外错误”。 I also know that the variables are being set properly because of the pause commands that I included. 我也知道,由于包含了暂停命令,因此正确设置了变量。 I also tried using IF %WINVERSION% == %XP% goto XPTRUE/WIN7TRUE and enclosing everything within the brackets under a :XPTRUE/WIN7TRUE but that gives the same error. 我还尝试使用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
)

When I run this on my Windows 7 computer I get this: 当我在Windows 7计算机上运行此命令时,我得到以下信息:

图片

I get the same output on my XP computer except it tells me the current version is xp instead. 我在XP计算机上得到了相同的输出,除了它告诉我当前版本是xp。 Help would be greatly appreciated. 帮助将不胜感激。

The line below: 下面的行:

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

stores in WINVERSION variable a string that contain several words separated by spaces, for example: 在WINVERSION变量中存储一个字符串,该字符串包含多个用空格分隔的单词,例如:

SET WINVERSION=Microsoft Windows [Version 6.2.9200]

This way, the line below: 这样,下面的行:

IF %WINVERSION% == %XP% (

is expanded to: 扩展为:

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

that, of course, cause a syntax error! 当然会导致语法错误! Type: IF /? 类型: IF /? for further details. 有关更多详细信息。

The way to compare two strings that may contains spaces, is enclosing they in quotes: 比较两个可能包含空格的字符串的方法是将它们括在引号中:

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

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

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