Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell重新启动控制台问题_Powershell_Reboot_Login Script_Uptime - Fatal编程技术网

Powershell重新启动控制台问题

Powershell重新启动控制台问题,powershell,reboot,login-script,uptime,Powershell,Reboot,Login Script,Uptime,我有一个登录脚本,重新启动电脑,如果它已经超过允许的时间 我有一个PowerShell脚本,它在登录期间运行,每周执行一次,以检查机器的正常运行时间,并在机器运行时间>48小时时重新启动。该脚本允许用户请求扩展,因此不会立即将其从计算机上踢出。我有一个1、3、8小时的延期。只有1,3小时的延长工作。如果输入的值大于3,则它似乎不接受该值。有没有办法让这个“延期”工作时间超过3小时 <# .SYNOPSIS Script displays an interactive popup t

我有一个登录脚本,重新启动电脑,如果它已经超过允许的时间

我有一个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

试一试{
添加类型-AssemblyName PresentationCore、PresentationFramework、WindowsBase、system.windows.forms
} 
捕获{}
#计算自上次重新启动以来的时间
$lastBootTime=Get-CimInstance-ClassName Win32|OperatingSystem |选择对象LastBootUpTime | ForEach对象{Get-Date-Date$|.LastBootUpTime}
$Time=(获取日期)-$lastBootTime
#如果上次重新启动少于2天,请退出脚本。
如果($Time.TotalHours-lt 48){
出口
}
#如果计算机有服务器操作系统,请退出
$version=(获取CimInstance-类名Win32_OperatingSystem)。标题
如果($版本-匹配“服务器”)
{
出口
}
$Global:hash=[hashtable]::Synchronized(@{})
$Global:hash.Stopwatch=新对象System.Diagnostics.Stopwatch
$Global:hash.Timer=新对象System.Windows.Forms.Timer
$Global:hash.Timer.Enabled=$true
$Global:hash.Timer.Interval=1000
[xml]$xaml=@'
强制重新启动
此计算机超出了允许的最大正常运行时间。除非手动重新启动或请求扩展,否则计算机将自动重新启动。
1.
3.
8.
小时
自动重新启动的时间(所有未保存的工作都将丢失)
'@
$Global:hash.window=[Windows.Markup.XamlReader]::Load((新对象-TypeName System.Xml.XmlNodeReader-ArgumentList$xaml))
#连接到控件
$xaml.SelectNodes(“//*[@Name]”)ForEach对象-进程{
$Global:hash.$($.Name)=$Global:hash.window.FindName($.Name)
}
$Global:hash.RestartNowBtn.Add\u单击({
#停止计时器,以免自动重新启动
$Global:hash.Timer.Stop()
#现在强制重新启动!
cmd.exe/c shutdown.exe-r-d P:0:0-t 60-f
$Global:hash.window.Close()
})
$Global:hash.ScheduleBtn.Add_单击({
开关($Global:hash.ScheduleValue.SelectedItem.Name){
“一个”{
$rebootTime=(获取日期).AddHours(1).TimeOfDay.TotalSeconds
}
“三个”{
$rebootTime=(获取日期).AddHours(3).TimeOfDay.TotalSeconds
}
“八个”{
$rebootTime=(获取日期).AddHours(8).TimeOfDay.TotalSeconds
}
}
$startTime=[Math]::四舍五入($rebootTime-(获取日期).TimeOfDay.TotalSeconds),0)
#停止计时器,以免自动重新启动
$Global:hash.Timer.Stop()
cmd.exe/c shutdown.exe-r-d P:0:0-t$startTime-f
$Global:hash.Window.Close()
})
$Global:hash.window.Add\u加载({
$Global:hash.Stopwatch.Start()
$Global:hash.Timer.Add_Tick({
[timespan]$secs=$Global:hash.Stopwatch.appeased
如果($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()
出口
}
$Global:hash.Time.Value=(100-($secs.TotalSeconds/120)*100))
})
$Global:hash.Timer.Start()
})
$Global:hash.window.ShowDialog()| Out Null
我想允许8小时的延长,但不允许超过3小时。

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


我检查了所有3个“重启扩展”代码部分是否正常工作,方法是将8个代码移到其他位置。他们还是不允许超过3小时?不能安排这么长时间的重新启动吗?(我在排除故障时将上次重新启动计算器修改为0小时)谢谢您提供的信息。我对PowerShell不是很在行,是否可以添加以下内容:$sleeptime=(Get Date).AddHours(5).TimeOfDay.TotalSeconds$Global:hash.ScheduleBtn.add_单击({switch($Global:hash.ScheduleValue.SelectedItem.Name){“one”{$rebootTime=(Get Date).AddHours(1).TimeOfDay.TotalSeconds}“三个”{$rebootTime=(获取日期).AddHours(3).TimeOfDay.TotalSeconds}“八个”{$rebootTime=(获取日期).AddHours(3).TimeOfDay.TotalSeconds.AddHours$sleeptime}”我猜增加8小时会把事情推到第二天,结果是负数。另外,我对600秒的限制是错误的(看起来MS的文档已经过时了)。检查更新的答案。很抱歉我反应太慢。如果之前的评论被“推迟”,我想你是对的有一段时间会一直持续到第二天,它不起作用,但如果是在当天早些时候,它就起作用了,而不会持续到第二天。我将代码更改为您提供的代码,这就解决了问题。感谢您的帮助!
/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.
$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()
}