使用是/否选项重启电脑

时间:2019-01-16 13:51:46

标签: powershell batch-file

我正在尝试创建一个脚本来执行以下操作,但是我的编码技能仍然不是很好。

我想重启一台闲置的PC,但是我想弹出一个对话框,显示是或否命令以继续或取消重启命令。

我要制定任务计划以运行空闲部分,然后运行批处理文件以弹出yes / no选项。

更新

@boxdog-PC无法锁定。

我设法按如下方法创建了一个脚本。

@ECHO OFF
SHUTDOWN /S /F /T 60
SET /P continue="You have been idle for more than 10 minutes.                                                                            Your computer is about to shutdown in 60 seconds do you want to abort (y/n): "
IF %continue% EQU y (
SHUTDOWN /A
)

不确定是否可以通过递减计数来更改默认的蓝色窗口文本?或者也许有人可以调整我的脚本以使其更好地工作?

2 个答案:

答案 0 :(得分:0)

如果要使用命令提示符,则有一个choice命令:

echo off
choice /M "You have been idle for more than 10 minutes. Shutdown"
if %ERRORLEVEL% equ 1 (shutdown /S /F /T 60)
if %ERRORLEVEL% equ 2 (echo no was selected)

ERRORLEVEL从1开始。因此,从执行choice /?的示例开始:

choice /C YNC /M "Press Y for Yes, N for No or C for Cancel."

“ Y”为1,“ N”为2,依此类推。

在Powershell中更简单,但是您将收到“默认”消息:

Stop-Computer -Confirm

答案 1 :(得分:0)

这里是一种获取倒计时的自定义UI的方法-编辑XAML以更改窗口设计。它接受以秒为单位的倒计时时间(默认为30)。另存为脚本并这样调用(倒计时60秒):

powershell.exe -WindowStyle Hidden -File .\script.ps1 60

注意:如果您按计划任务运行,则仅当登录用户与任务运行的用户相同时,才会显示对话框窗口。

Param(
    [Parameter()]
    [int]$TimeOut = 30 # Default to 30 second countdown
)

# Load WPF assembly
Add-Type -AssemblyName PresentationFramework

# Define WPF window
$window = [Windows.Markup.XamlReader]::Load(
    [Xml.XmlNodeReader]::new([xml]@'
    <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:local="clr-namespace:RebootTimer"
            Title="Idle Reboot Alert" Height="195" Width="400" MaxHeight="195" MaxWidth="400" MinHeight="195" MinWidth="400" ShowInTaskbar="False" Topmost="True" WindowStartupLocation="CenterScreen" WindowStyle="ToolWindow">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition></RowDefinition>
                <RowDefinition Height="30"></RowDefinition>
                <RowDefinition></RowDefinition>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <TextBlock x:Name="AlertText" Grid.Row="0" FontSize="14" TextWrapping="Wrap" VerticalAlignment="Center" HorizontalAlignment="Center" TextAlignment="Center">
                <Run>You have exceeded the 10 min idle timeout.</Run>
                <LineBreak/>
                <Run>The system will restart in ...</Run>
            </TextBlock>
            <TextBlock x:Name="CountdownText" Grid.Row="1" FontSize="22" FontWeight="Bold" TextWrapping="Wrap" HorizontalAlignment="Center" VerticalAlignment="Center" TextAlignment="Center" />
            <Button x:Name="CancelButton" Grid.Row="2" Height="35" Width="75">Cancel</Button>
        </Grid>
    </Window>
'@)
)

# Get controls to manipulate
$countdownText = $window.FindName('CountdownText')
$cancelButton = $window.FindName('CancelButton')

# Add event handler to cancel button
$cancelButton.Add_Click({$window.Close()})

$window.Add_SourceInitialized({           
    $script:seconds = $TimeOut

    $countdownText.Text = "$($script:seconds)s"

    $script:timer = New-Object System.Windows.Threading.DispatcherTimer
    $script:timer.Interval = ([TimeSpan]'0:0:1.0') # Fire every second

    $script:timer.Add_Tick.Invoke({   
        $countdownText.Text = "$($script:seconds)s"

        if($script:seconds-- -le 0) {
           Restart-Computer
        }
    })

    $script:timer.Start()
})

# Show the window
$window.ShowDialog() | Out-Null