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 如何通过远程处理自动化Wusa并克服等待时间不足的问题_Powershell - Fatal编程技术网

Powershell 如何通过远程处理自动化Wusa并克服等待时间不足的问题

Powershell 如何通过远程处理自动化Wusa并克服等待时间不足的问题,powershell,Powershell,场景:获取kb文件列表并通过网络上目标机器的WUSA安装远程执行它们 流程如下: 输入带目标计算机的powershell会话 对于$List中的每个循环$KB wusa.exe$kb 等待,直到安装 返回$list中下一个设备的wusa.exe 代码: # SET UP ERROR-HANDLING FOR THE PS SESSION $ErrorActionPreference = "continue" # DECLARE VARIABLE THAT CONTAINS LIST OF PA

场景:获取kb文件列表并通过网络上目标机器的WUSA安装远程执行它们

流程如下:

  • 输入带目标计算机的powershell会话
  • 对于$List中的每个循环$KB
  • wusa.exe$kb
  • 等待,直到安装
  • 返回$list中下一个设备的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
    

    1) 请修复代码格式(代码=缩进4个空格)。2) 你的问题是什么?我不能用wusa远程安装kbs,所以我需要使用批处理fle,但是我必须执行批处理文件,但是从cmd我不能使用wait命令。克服wait cmd的建议?使用start/wait-start应用程序并等待其终止。