【问题标题】:Shutdown command in batch file results in an error and crash of the cmd prompt批处理文件中的关闭命令导致 cmd 提示错误和崩溃
【发布时间】:2015-08-12 13:40:38
【问题描述】:

我正在尝试创建一个批处理文件以从我的 Surface Pro 3 上的任务栏关闭我的计算机,我已在我的台式计算机(均运行 Windows 8.1)上成功完成此操作,但在我的 Surface 上此批处理文件:

@echo off
SET /p var1 = "Shutdown? (Y/N) "
if /I %var1% == "Y" shutdown /s /t 0

导致错误“此时意外关闭”。以及包含它的命令窗口立即崩溃。这个确切的(编辑:措辞选择不当,实际上并不准确)批处理文件在我的桌面上按预期工作。我的 Surface 上的批处理文件仅包含代码:

shutdown /s /t 0

工作正常,这让我很困惑。我没有从我的搜索中找到任何解决方案,所以任何帮助将不胜感激!

【问题讨论】:

  • 该代码可以在您的桌面上运行吗?它不应该;您在if 语句中缺少%var% 周围的引号,并且您的set 命令不应在= 周围有空格。
  • 另外,choice 在这种情况下会比set /p 更好。
  • 引号和空格修复了它,我重新检查了我的桌面版本,一开始是正确的。我不应该把它称为完全相同的批处理文件,因为我没有从我的桌面复制/粘贴到我的表面。感谢SomethingDark的帮助

标签: batch-file windows-8.1


【解决方案1】:
@echo off
SET /p var1="Shutdown? (Y/N) "
if /I "%var1%" == "Y" shutdown /s /t 0

这个版本有效,所有需要修复的是= 周围的空格和%var1 周围的引号。感谢SomethingDark的回答。

【讨论】:

    【解决方案2】:

    为什么不使用 Powershell 的 Stop-Computer cmdlet?该 cmdlet 在 Powershell 4.0 中可用。

    -编辑以添加示例。感谢this question 提供的是/否确认码,确认在 Powershell v4 中工作。

    $message  = 'Shutdown Computer?'
    $question = 'Are you sure you want to Shutdown?'
    
    $choices = New-Object Collections.ObjectModel.Collection[Management.Automation.Host.ChoiceDescription]
    $choices.Add((New-Object Management.Automation.Host.ChoiceDescription -ArgumentList '&Yes'))
    $choices.Add((New-Object Management.Automation.Host.ChoiceDescription -ArgumentList '&No'))
    
    $decision = $Host.UI.PromptForChoice($message, $question, $choices, 1)
     if ($decision -eq 0) {
      Stop-Computer -force
    } else {
     "Shutdown aborted."
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-06-29
      • 2016-02-08
      • 1970-01-01
      • 1970-01-01
      • 2017-07-02
      • 2021-04-30
      • 1970-01-01
      相关资源
      最近更新 更多