我想创建一个脚本来做到以下几点但我的编码技能仍然不是很好。
我想重新启动PC是空闲的,但是我希望有一个弹出用Yes或No命令出现继续或取消重新启动命令。
我打算做一个任务调度运行闲置部分,然后运行一个批处理文件,弹出的是/否选项。
@boxdog - 电脑不会锁定。
我设法创建一个脚本,按如下。
@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
)
不知道它有可能改变与倒计数的默认蓝色的窗户文字?或者,也许有人能调整我的脚本更好地工作?
这是一种方式来获得倒计时自定义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
还有,如果你想使用命令提示符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