Install WSUS Updates with Powershell

$Computers = Get-ADComputer -SearchBase "OU=Servrar,DC=contoso,DC=com" -Properties operatingSystem,dnshostname -Filter * | Select -ExpandProperty dnshostName 

#Ta bort de som redan har körts
$ExceptServers = (Import-Csv $LogFile -Delimiter "," -Header ComputerName,Status).ComputerName 
$Computers = $Computers | Where {$ExceptServers -notcontains $_}
$LogFile = "C:\Temp\WsusForce.txt"

Foreach ($Computer in $Computers)
{

Try 
{
$InstallScript = @'
####
$Criteria = "IsInstalled=0 and Type='Software'"

    #Search for relevant updates.
    $Searcher = New-Object -ComObject Microsoft.Update.Searcher
    $SearchResult = $Searcher.Search($Criteria).Updates
    #Download updates.
    $Session = New-Object -ComObject Microsoft.Update.Session
    $Downloader = $Session.CreateUpdateDownloader()
    $Downloader.Updates = $SearchResult
    $Downloader.Download()

    #Install updates.
    $Installer = New-Object -ComObject Microsoft.Update.Installer
    $Installer.Updates = $SearchResult
    $Result = $Installer.Install()

#####
$TS = New-Object -ComObject Schedule.Service
$TS.Connect($env:COMPUTERNAME)
$TaskFolder = $TS.GetFolder("\")
$Tasks = $TaskFolder.GetTasks(1)
$TaskToDelete = "WsusForce"
foreach($Task in $Tasks)
    {
    If($Task.Name -eq $TaskToDelete)
        {$TaskFolder.DeleteTask($Task.Name,0)}
    }
#Ta bort sig själv
Remove-Item -LiteralPath $MyInvocation.MyCommand.Path -Force
'@
			Set-Content -Path "\\$Computer\C$\Windows\Temp\WsusForce.ps1" -Value $InstallScript -ErrorAction SilentlyContinue
						
			
			Invoke-Command -ScriptBlock {
				# The name of the scheduled task
				$TaskName = "WsusForce"
				# The description of the task
				$TaskDescr = "WsusForce"
				# The Task Action command
				$TaskCommand = "powershell.exe"
				# The PowerShell script to be executed
				$TaskScript = "C:\Windows\Temp\WsusForce.ps1"
				# The Task Action command argument
				$TaskArg = "-WindowStyle Hidden -NonInteractive -Executionpolicy unrestricted -file $TaskScript"
				
				# The time when the task starts
				$TaskStartTime = [datetime]::Now.AddSeconds(10)
				# Attach the Task Scheduler com object
				$service = new-object -ComObject ("Schedule.Service")
				# connect to the local machine. 
				# http://msdn.microsoft.com/en-us/library/windows/desktop/aa381833(v=vs.85).aspx
				$Service.Connect()
				$RootFolder = $service.GetFolder("\")
				
				$TaskDefinition = $service.NewTask(0)
				$TaskDefinition.RegistrationInfo.Description = "$TaskDescr"
				$TaskDefinition.Settings.Enabled = $true
				$TaskDefinition.Settings.AllowDemandStart = $true
				
				$Triggers = $TaskDefinition.Triggers
				#http://msdn.microsoft.com/en-us/library/windows/desktop/aa383915(v=vs.85).aspx
				$Trigger = $Triggers.Create(1) # Creates a "One time" trigger
				$Trigger.StartBoundary = $TaskStartTime.ToString("yyyy-MM-dd'T'HH:mm:ss")
				$Trigger.Enabled = $true
				
				# http://msdn.microsoft.com/en-us/library/windows/desktop/aa381841(v=vs.85).aspx
				$Action = $TaskDefinition.Actions.Create(0)
				$Action.Path = "$TaskCommand"
				$Action.Arguments = "$TaskArg"
				
				#http://msdn.microsoft.com/en-us/library/windows/desktop/aa381365(v=vs.85).aspx
				$RootFolder.RegisterTaskDefinition("$TaskName", $TaskDefinition, 6, "System", $null, 5)
				
				
				
			} -ComputerName $Computer -ErrorAction Stop

    Add-Content $LogFile "$Computer,OK"
}
Catch
    {
    Add-Content $LogFile "$Computer,$($_.Exception.Message)"
    }
}
This entry was posted in Uncategorized. Bookmark the permalink.