尝试创建一个批处理文件,该文件将检查iTunes版本,然后在版本低于脚本中列出的版本时进行更新。
我遇到的问题是,从注册表项获得IF值的最佳方式是什么。
我在谷歌上搜索了一下,找不到符合我想做的事情。
::Finds the value of the Version key
REG QUERY "HKLMSOFTWAREApple Computer, Inc.iTunes" /v Version
这就是我陷入困境的地方。如何使用版本中的值?我需要使用FOR循环吗?我试过玩它,但没有玩
::If the version matches the number below iTunes is up to date
IF Version==12.5.4.42 @echo Up to date! && goto end
::If the version is not equal to the number below
IF NOT Version==12.5.4.42 && goto install
::Installs the current version from the repository
:install
msiexec.exe ALLUSERS=true reboot=suppress /qn /i "%~dp0appleapplicationsupport.msi"
msiexec.exe /qn /norestart /i "%~dp0applemobiledevicesupport.msi"
msiexec.exe /qn /norestart /i "%~dp0itunes.msi"
echo Cleaning Up Installation
del C:UsersPublicDesktopiTunes.lnk
:end
exit
我觉得自己是一个工具,但我无法弄清楚这一点。以前从未处理过FOR声明。提前为我的愚蠢道歉。
脚本的一个特定问题是,这一行中有一个额外的&&
:
IF NOT Version==12.5.4.42 && goto install
暂时将rem
标记为@echo off
可以帮助您找到这些简单的语法错误。正如Magoo所指出的,Version是一个永远不会等于12.5.4.42的字符串。当您要对批处理中的变量求值时,它们会被%
包围(有时是!
)。
更普遍地说,在比较版本号时,最好使用一种能够客观化版本号并能够理解major.minor.build.revision的语言。如果安装的版本是,例如,12.10.0.0,则不希望触发安装。与批次中的12.5.4.42进行比较将触发安装。尽管12.10.x.x在数字上大于12.5.x.x,但按字母顺序它较小,并且在if
比较中被视为较低的值。
如图所示,在cmd控制台中,输入以下内容并查看会发生什么:
if 12.10.0.0 leq 12.5.4.42 @echo triggered!
我会使用PowerShell来完成繁重的任务。下面是一个使用Batch+PowerShell混合脚本的示例。由于我没有安装iTunes,所以我还没有测试过它,所以你可能需要加盐调味。
<# : batch portion (begin multiline PowerShell comment block)
@echo off & setlocal
set "installer_version=12.5.4.42"
powershell -noprofile "iex (${%~f0} | out-string)" && (
echo Up to date!
goto :EOF
)
:install
msiexec.exe ALLUSERS=true reboot=suppress /qn /i "%~dp0appleapplicationsupport.msi"
msiexec.exe /qn /norestart /i "%~dp0applemobiledevicesupport.msi"
msiexec.exe /qn /norestart /i "%~dp0itunes.msi"
echo Cleaning Up Installation
del C:UsersPublicDesktopiTunes.lnk
goto :EOF
: end batch / begin PowerShell hybrid code #>
$regkey = "HKLM:SOFTWAREApple Computer, Inc.iTunes"
$installed = (gp $regkey Version -EA SilentlyContinue).Version
if (-not $installed) {
"iTunes not installed."
exit 1
}
# exits 0 if true, 1 if false (-le means <=)
exit !([version]$env:installer_version -le [version]$installed)
不过,要回答您提出的问题,即如何捕获reg
或任何其他命令的输出,请使用for /F
循环。有关详细信息,请参阅cmd控制台中的for /?
。
IF Version==12.5.4.42 @echo Up to date! && goto end
字符串CCD_ 10永远不会等于字符串12.5.4.42
。您需要Version
的内容,因此代码应该是
IF %Version%==12.5.4.42 @echo Up to date!&goto end
(单个&
连接命令)
以下if
是多余的。要达到该语句,版本必须不是-12.5.4.42,否则执行将转移到:end
BTW,goto :eof
,其中:eof
中的冒号为必填项表示"转到文件的物理末尾"。