Компьютерный форум OSzone.net  

Компьютерный форум OSzone.net (http://forum.oszone.net/index.php)
-   Скриптовые языки администрирования Windows (http://forum.oszone.net/forumdisplay.php?f=102)
-   -   Restart-Computer (http://forum.oszone.net/showthread.php?t=291292)

milligan 20-11-2014 07:02 2432368

Restart-Computer
 
Нашел скрипт для проверки статуса "Требуется перезагрузка для завершения установки обновлений". Хочу чтобы серверы ночью перегружались по наличию этого статуса.
Код:

Function Get-WURebootStatus
{
    <#
        .SYNOPSIS
            Show Windows Update Reboot status.

        .DESCRIPTION
            Use Get-WURebootStatus to check if reboot is needed.
               
        .PARAMETER Silent
            Get only status True/False without any more comments on screen.
       
        .EXAMPLE
        Check whether restart is necessary. If yes, ask to do this or don't.
               
                PS C:\> Get-WURebootStatus
                Reboot is required. Do it now ? [Y/N]: Y
               
        .EXAMPLE
                Silent check whether restart is necessary. It return only status True or False without restart machine.
       
        PS C:\> Get-WURebootStatus -Silent
                True
               
        .NOTES
                Author: Michal Gajda
                Blog  : http://commandlinegeeks.com/
               
        .LINK
                http://gallery.technet.microsoft.com/scriptcenter/2d191bcd-3308-4edd-9de2-88dff796b0bc

        .LINK
        Get-WUInstallerStatus
        #>   

        [CmdletBinding(
            SupportsShouldProcess=$True,
        ConfirmImpact="Low"
    )]
    Param
        (
                [Alias("StatusOnly")]
                [Switch]$Silent,
                [String[]]$ComputerName = "localhost",
                [Switch]$AutoReboot
        )
       
        Begin
        {
                $User = [Security.Principal.WindowsIdentity]::GetCurrent()
                $Role = (New-Object Security.Principal.WindowsPrincipal $user).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)

                if(!$Role)
                {
                        Write-Warning "To perform some operations you must run an elevated Windows PowerShell console."       
                } #End If !$Role
        }
       
        Process
        {
        ForEach($Computer in $ComputerName)
                {
                        If ($pscmdlet.ShouldProcess($Computer,"Check that Windows update needs to restart system to install next updates"))
                        {                               
                                if($Env:COMPUTERNAME,"localhost","." -contains $Computer)
                                {
                                    Write-Verbose "$($Computer): Using WUAPI"
                                        $objSystemInfo= New-Object -ComObject "Microsoft.Update.SystemInfo"
                                        $RebootRequired = $objSystemInfo.RebootRequired
                                } #End if $Computer -eq $Env:COMPUTERNAME
                                else
                                {
                                        Write-Verbose "$($Computer): Using Registry"
                                        $RegistryKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]"LocalMachine",$Computer)
                                        $RegistrySubKey = $RegistryKey.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\")
                                        $RegistrySubKeyNames = $RegistrySubKey.GetSubKeyNames()
                                        $RebootRequired = $RegistrySubKeyNames -contains "RebootRequired"

                                } #End else $Computer -eq $Env:COMPUTERNAME
                               
                                Switch($RebootRequired)
                                {
                                        $true        {
                                                If($Silent)
                                                {
                                                        Return $true
                                                } #End If $Silent
                                                Else
                                                {
                                                        if($AutoReboot -ne $true)
                                                        {
                                                                $Reboot = Read-Host "$($Computer): Reboot is required. Do it now ? [Y/N]"
                                                        } #End If $AutoReboot -ne $true
                                                        Else
                                                        {
                                                                $Reboot = "Y"
                                                        } #End else $AutoReboot -ne $true
                                                       
                                                        If($Reboot -eq "Y")
                                                        {
                                                                Write-Verbose "Rebooting $($Computer)"
                                                                Restart-Computer -ComputerName $Computer -Force
                                                        } #End If $Reboot -eq "Y"
                                                } #End Else $Silent
                                        } #End Switch $true
                                               
                                        $false        {
                                                If($Silent)
                                                {
                                                        Return $false
                                                } #End If $Silent
                                                Else
                                                {
                                                        Write-Output "$($Computer): Reboot is not Required."
                                                } #End Else $Silent
                                        } #End Switch $false
                                } #End Switch $objSystemInfo.RebootRequired
                               
                        } #End If $pscmdlet.ShouldProcess($Env:COMPUTERNAME,"Check that Windows update needs to restart system to install next updates")
                } #End ForEach $Computer in $ComputerName
        } #End Process
       
        End{}                               
} #In The End :)

Все красиво работает, НО после перезагрузки всплывает окошко с требованием указать причину перезагрузки.
В cmd в команде shutdown были параметры указать причину. В powershell Restart-computer (который выполняется в этом скрипте) такого параметра не нашел (мб плохо искал).
Кто, чего может подсказать?

Kazun 20-11-2014 08:50 2432396

У Restart-Computer нет такого параметра. Поэтому самое простое использовать утилиту shutdown или метод Win32ShutdownTracker класса Win32_OperatingSystem - http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx.

Код:

invoke-wmimethod -path Win32_OperatingSystem -name Win32ShutdownTracker -argumentlist 0,$null,0,6


Время: 23:11.

Время: 23:11.
© OSzone.net 2001-