как автоматизировать Wusa с удаленным доступом и преодолением отсутствия ожидания CMD - PullRequest
0 голосов
/ 23 мая 2018

Сценарий: Получение списка файлов в килобайтах и ​​их удаленное выполнение с помощью установки целевых компьютеров через сеть с помощью WUSA.

Процесс выглядит следующим образом:

  1. введите сеанс PowerShell W / целевой компьютер
  2. для каждого цикла $ КБ в $ List
  3. wusa.exe $ kb
  4. ждать до установки
  5. вернуться к wusa.exe для следующего устройства в списке $

код :

# SET UP ERROR-HANDLING FOR THE PS SESSION
$ErrorActionPreference = "continue"

# DECLARE VARIABLE THAT CONTAINS LIST OF PATCHES AVAILABLE FOR INSTALL
$PatchList = (Get-ChildItem -Path C:\WinPatch -recurse | Where-Object {$_.Extension -eq '.msu'})

# ESTABLISH LOOP TO ITERATE THRU PATCHES
foreach ($Patch in $PatchList)
    {
        Try 
            {
            Write-Host ("`n Preparing to install: " + $Patch) -ForegroundColor Yellow
            Write-Host ("`n Installing...") -ForegroundColor Magenta
            $SB = {
                $arglist = "$Patch", "/quiet", "/norestart"
                Start-Process -FilePath "C:\windows\system32\wusa.exe" -ArgumentList $arglist -Wait}
            Invoke-Command -ScriptBlock $SB
            Write-Host "`n Installation complete`n" -ForegroundColor Green
            }
        Catch 
            {
            [System.Exception]
            Write-Host "Installation failed with Error -- $Error()" -ForegroundColor Red
            $Error.Clear()
            }
    }

# RESTART OPTIONS
$Ans1 = Read-Host "`n Would you like to restart this computer now (Type Y for yes or N for no)"
if ($Ans1 -eq 'Y' -or $Ans1 -eq 'y')
    {
        Remove-Item -Path C:\WinPatch\*.msu -Force
    Write-Host "`n This computer will restart in 5 seconds..." -ForegroundColor Yellow
        Start-Sleep -Seconds 5
        Restart-Computer -Force
    }
else
    {
        # TEST COMPUTER FOR PS VERSION
    $Tester = test-wsman -computername localhost | Select-Object -Property ProductVersion
    if ($Tester.ProductVersion.EndsWith("1.0"))
        { 
            Write-Host "`n This computer has PS v1.0 installed and you will have to open Task Scheduler to schedule restart" -ForegroundColor Red
            Read-Host "`n Press ENTER to continue..."
        }
    elseif ($Tester.ProductVersion.EndsWith("2.0"))
        { 
            Write-Host "`n This computer has PS v2.0 installed and you will have to open Task Scheduler to schedule restart" -ForegroundColor Red
            Read-Host "`n Press ENTER to continue..."
        }
    else
        {
            # SCHEDULE RESTART
            Import-Module PSScheduledJob
            $RST = Read-Host -Prompt "`n Enter date/time to restart computer...format is --> mm/dd/yyyy hh:mmAM/PM"
            $Ans2 = Read-Host -Prompt "`n You entered: $RST... if this is correct, enter Y for yes"
            if ($Ans2 -eq 'Y' -or $Ans2 -eq 'y')
                {
                    $Nomen = Read-Host -Prompt "`n Enter name for scheduled restart "
                    $Trig = New-JobTrigger -Once -At $RST
                    Register-ScheduledJob -Name $Nomen -Trigger $Trig -ScriptBlock { Restart-Computer -force } -RunAs32
                }
            else
                {
                    Write-Host "`n Please restart the script and try again" -ForegroundColor Red
                }
        }
    }

Break
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...