【问题标题】:Batch file to update iTunes depending on version根据版本更新 iTunes 的批处理文件
【发布时间】:2017-05-20 10:24:48
【问题描述】:

尝试创建一个批处理文件来检查 iTunes 版本,如果版本低于脚本中列出的版本,则会更新。

我遇到的问题是将值从注册表项获取到我的 IF 值的最佳方法是什么。

我在 Google 上环顾了一下,找不到与我想做的事情相匹配的东西。

::Finds the value of the Version key
REG QUERY "HKLM\SOFTWARE\Apple Computer, Inc.\iTunes" /v Version

这就是我卡住的地方。如何使用版本中的值?我需要为此使用 FOR 循环吗?我试过玩它,但不是 su

::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:\Users\Public\Desktop\iTunes.lnk

:end
exit

我觉得自己是一个无法解决这个问题的工具。之前没有处理过 FOR 语句。提前为我的愚蠢道歉。

【问题讨论】:

    标签: batch-file installation registry itunes


    【解决方案1】:

    您的脚本的一个具体问题是您在此行中有一个额外的&&

    IF NOT Version==12.5.4.42 && goto install
    

    暂时remarking out @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:\Users\Public\Desktop\iTunes.lnk
    goto :EOF
    
    : end batch / begin PowerShell hybrid code #>
    
    $regkey = "HKLM:\SOFTWARE\Apple 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 /?

    【讨论】:

    • 有趣的方法。让我测试一下,看看会发生什么!!!!
    • 如果我想让它安装 iTunes,如果它没有安装或者如果版本低于列出的版本,那么最好的方法是什么?
    • @Kellanist 我的解决方案已经做到了(我认为)。当 PowerShell 执行 exit 1 时,执行线程返回到批处理脚本。随着 PowerShell 以错误级别 1 退出,&amp;&amp; 将被跳过,并在 :install 处继续执行。顺便说一句,欢迎来到 Stack Overflow!如果我的回答有帮助,请考虑将其标记为已接受。 See this page 解释为什么这很重要。
    • 刚刚又试了一次。它给了我“iTunes not installed”然后退出。
    • @Kellanist 你给这个扩展名是 .bat 还是 .ps1?这主要是一个批处理脚本,带有 PowerShell 混合代码。给它一个 .bat 扩展名。抱歉,我应该在回答中解释这一点。
    【解决方案2】:
    IF Version==12.5.4.42 @echo Up to date!  && goto end
    

    字符串Version 永远不会等于字符串12.5.4.42。你需要Version内容,所以代码应该是

    IF %Version%==12.5.4.42 @echo Up to date!&goto end
    

    (单个&amp; 连接命令)

    下面的if 是多余的。要达到该语句,版本 必须 不是 12.5.4.42 否则执行将被转移到 :end

    顺便说一句,goto :eof :eof 中的冒号 required 表示“转到文件的物理结尾”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-27
      • 1970-01-01
      • 2019-04-20
      • 1970-01-01
      • 2012-01-16
      • 1970-01-01
      • 1970-01-01
      • 2013-07-16
      相关资源
      最近更新 更多