【问题标题】:Powershell reboot console issuePowershell重启控制台问题
【发布时间】:2019-04-16 18:04:25
【问题描述】:

我有一个登录脚本,如果它的启动时间超过了允许的时间,它会重新启动 PC。

我有一个 PowerShell 脚本,它在登录期间运行并每周执行一次,以检查机器的正常运行时间,如果超过 48 小时则重新启动。该脚本允许用户请求扩展,因此他们不会立即被踢出机器。我有 1、3、8 小时的延期。只有1、3小时的延长工作。看来,如果您输入一个大于 3 的值,它就不会接受它。有没有办法让这个“扩展”工作超过 3 小时?

<#
.SYNOPSIS
    Script displays an interactive popup that display the reason for having the reboot the computer.
.DESCRIPTION
     The reboot script gives users the option to reboot now or delay the reboot for 1, 3, or 6 hours
.EXAMPLE
    Script executes from the login script
.INPUTS
    N/A
.OUTPUTS
    N/A
.NOTES
    General notes
#>

try {
    Add-Type -AssemblyName PresentationCore, PresentationFramework, WindowsBase, system.windows.forms
} 
catch {}

#Calculate the time since last reboot

$lastBootTime = Get-CimInstance -ClassName Win32_OperatingSystem | Select-Object LastBootUpTime | ForEach-Object { Get-Date -Date $_.LastBootUpTime }
$Time = (Get-Date) - $lastBootTime

#if last reboot is less than 2 days exit the script.
if ($Time.TotalHours -lt 48) {
    exit
}

#if computer has a server os, exit
$version = (Get-CimInstance -ClassName Win32_OperatingSystem).caption

if($version -match "server")
{
    exit
}

<#
 # Set Sync hash
 #>
$Global:hash = [hashtable]::Synchronized(@{})
$Global:hash.Stopwatch = New-Object System.Diagnostics.Stopwatch
$Global:hash.Timer = New-Object System.Windows.Forms.Timer
$Global:hash.Timer.Enabled = $true
$Global:hash.Timer.Interval = 1000


<#
 # Form design XML
 #>
[xml]$xaml = @'
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Name="window" Height="340" Width="400" Title="Mandatory Reboot"  WindowStartupLocation="CenterScreen" Background="Transparent" AllowsTransparency="True"
ResizeMode="NoResize" ShowInTaskbar="True"  SizeToContent="Height" Topmost="True" WindowStyle="None">
    <Window.Resources>
        <SolidColorBrush x:Key="Brush_ChromeBackground" Color="#FFC8D1E0"/>
        <SolidColorBrush x:Key="Brush_ChromeBorder" Color="#FFA0A0A0"/>
    </Window.Resources>
    <Border x:Name="Border_Chrome"  BorderBrush="{StaticResource Brush_ChromeBorder}" BorderThickness="5"  CornerRadius="10"  Width="Auto" Background="#FFCD3232">
        <Grid Margin="15" >
            <Grid.RowDefinitions>
                <RowDefinition Height="120"></RowDefinition>
                <RowDefinition Height="60"></RowDefinition>
                <RowDefinition Height="60"></RowDefinition>
                <RowDefinition Height="60"></RowDefinition>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="50"></ColumnDefinition>
                <ColumnDefinition Width="*"></ColumnDefinition>
                <ColumnDefinition Width="50"></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <TextBlock TextWrapping="Wrap" Grid.Column="0" Grid.ColumnSpan="3" FontSize="14" Foreground="White">
                <Bold FontSize="16">Mandatory Reboot</Bold><LineBreak/>
                This computer exceeds the maximum allowed uptime. Your computer will be automatically rebooted unless manually restarted or an extension is requested.<LineBreak/>
            </TextBlock>
            <Button Name="RestartNowBtn" Grid.Column="1" Grid.Row="1" Grid.ColumnSpan="3" Width="240" Height="40" HorizontalContentAlignment="Center" HorizontalAlignment="Left">
                <StackPanel Orientation="Horizontal" >
                    <Label Content="Restart Now" FontWeight="Bold"></Label>
                </StackPanel>
            </Button>
            <StackPanel Orientation="Horizontal" Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="3" Margin="5,8" >
                <GroupBox Width="100" Header="Postpone">
                    <ComboBox Name="ScheduleValue" VerticalAlignment="Center" >
                        <ComboBoxItem Name="one" Selector.IsSelected="True">1</ComboBoxItem>
                        <ComboBoxItem Name="three">3</ComboBoxItem>
                        <ComboBoxItem Name="eight">8</ComboBoxItem>
                    </ComboBox>
                </GroupBox>
                <TextBlock Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="10,0" FontWeight="Bold">Hour(s)</TextBlock>
                <Button Content="Request Extension" Name="scheduleBtn" Width="110" FontWeight="Bold"></Button>
            </StackPanel>
            <Grid Grid.Row="3" Grid.ColumnSpan="3" Margin="10">
                <ProgressBar Name="Time" Maximum="100" Value="100"></ProgressBar>
                <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center">Time to Automatic Reboot (All unsaved work will be lost)</TextBlock>
            </Grid>
        </Grid>
    </Border>
</Window>
'@

$Global:hash.window = [Windows.Markup.XamlReader]::Load((New-Object -TypeName System.Xml.XmlNodeReader -ArgumentList $xaml))

#Connect to Control
$xaml.SelectNodes("//*[@Name]") | ForEach-Object -Process { 
    $Global:hash.$($_.Name) = $Global:hash.window.FindName($_.Name)
}

<#
 # Restart Now Button Even Handler
 #>
$Global:hash.RestartNowBtn.Add_Click( {
        #Stop the timer so that the auto reboot dosen't happen
        $Global:hash.Timer.Stop()
        #Force Reboot Now!
        cmd.exe /c shutdown.exe -r -d P:0:0 -t 60 -f
        $Global:hash.window.Close()
    })

<#
 # Schedule Button Click event handler
 #>
$Global:hash.ScheduleBtn.Add_Click( {

        switch ($Global:hash.ScheduleValue.SelectedItem.Name) {
            "one" {
                $rebootTime = (Get-Date).AddHours(1).TimeOfDay.TotalSeconds
            }
            "three" {
                $rebootTime = (Get-Date).AddHours(3).TimeOfDay.TotalSeconds
            }
            "eight" {
                $rebootTime = (Get-Date).AddHours(8).TimeOfDay.TotalSeconds
            }
        }

        $startTime = [Math]::Round(($rebootTime - (Get-Date).TimeOfDay.TotalSeconds), 0)

        #Stop the timer so that the auto reboot dosen't happen
        $Global:hash.Timer.Stop()

        cmd.exe /c shutdown.exe -r -d P:0:0 -t $startTime -f 

        $Global:hash.Window.Close()
    })

<#
 # Form Loaded Event Handler
 #>
$Global:hash.window.Add_Loaded( {

        $Global:hash.Stopwatch.Start()
        $Global:hash.Timer.Add_Tick( {
                [timespan]$secs = $Global:hash.Stopwatch.Elapsed

                if ($secs.TotalSeconds -gt 120) {
                    cmd.exe /c shutdown.exe -r -d P:0:0 -t 60 -f
                    $Global:hash.Timer.Stop()
                    $Global:hash.window.Close()
                    exit
                }

                $Global:hash.Time.Value = (100 - (($secs.TotalSeconds / 120) * 100))
            })

        $Global:hash.Timer.Start()
    })

<#
 # Start the form
 #>
$Global:hash.window.ShowDialog() | Out-Null

我想允许延长 8 小时,但不允许 > 3。

【问题讨论】:

  • 我通过将 8 个移动到其他位置来检查所有 3 个“重启扩展”代码部分是否正常工作。他们仍然不允许超过 3 小时?难道不能安排这么长时间的重启吗? (我在故障排除时将上次重启计算器修改为0小时)

标签: powershell reboot login-script uptime


【解决方案1】:

这是shutdown.exe 可执行文件的限制。根据此处找到的文档:

https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/shutdown

/t <XXX>    Sets the time-out period or delay to XXX seconds before a restart or 
            shutdown. This causes a warning to display on the local console. You can 
            specify 0-600 seconds. If you do not use /t, the time-out period is 30 
            seconds by default.

如果您想超越这一点,则必须在执行 shutdown.exe 之前在 PowerShell 中创建一个暂停。这可以通过在 8 小时选项中添加 start-sleep 来完成,休眠 3 小时,然后指定最大 600 秒计时器。

编辑: 看起来该文档已过时,并且 shutdown 现在接受最长 10 年的任何内容(以秒为单位)。看,shutdown 命令只需要几秒钟。您应该能够只用秒 x 分钟 x 小时来计算,而不是您正在尝试的所有花哨的数学。一分钟 60 秒,一小时 60 分钟,所以唯一改变的是延迟的小时数。试试这个:

$Global:hash.ScheduleBtn.Add_Click( {

    $startTime = switch ($Global:hash.ScheduleValue.SelectedItem.Name) { 
        "one" { 1*60*60 } 
        "three" { 3*60*60 } 
        "eight" { 8*60*60}
    }
    $Global:hash.Timer.Stop()

    cmd.exe /c shutdown.exe -r -d P:0:0 -t $startTime -f 

    $Global:hash.Window.Close()
}

【讨论】:

  • 感谢您的信息。我对 PowerShell 不是很好,是否可以只添加以下内容: $sleeptime = (Get-Date).AddHours(5).TimeOfDay.TotalSeconds $Global:hash.ScheduleBtn.Add_Click( { switch ($Global:hash. ScheduleValue.SelectedItem.Name) { "one" { $rebootTime = (Get-Date).AddHours(1).TimeOfDay.TotalSeconds } "三" { $rebootTime = (Get-Date).AddHours(3).TimeOfDay.TotalSeconds } "八" { $rebootTime = (Get-Date).AddHours(3).TimeOfDay.TotalSeconds.AddHours$sleeptime}}
  • 我猜测增加 8 小时会将事情推到第二天,而您最终会得到负数。另外,我对 600 秒的限制似乎是错误的(看起来 MS 的文档已经过时了)。检查更新的答案。
  • 对不起,我的反应很慢。我认为您对之前的评论是正确的,如果将其“推迟”一段时间,然后进入第二天,它就不起作用,但如果它在当天早些时候起作用,并且没有进入第二天。我将代码更改为您提供的代码,这解决了问题。感谢您的帮助!
猜你喜欢
  • 2013-01-08
  • 1970-01-01
  • 2020-06-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多