Automatic updates

Since there is no automatic update process, you can go about this in multiple ways. The easiest solutions for most are scheduled tasks and $profile. This process essentially involves executing the Update-Module cmdlet: 

# Automating updates
# Scheduled Tasks: Windows PowerShell style
# ScheduledJobs are also working, if you need the additional output
$parameters = @{
TaskName = 'PowerShellModuleUpdate'
TaskPath = 'KRAFTMUSCHEL'
Trigger = New-ScheduledTaskTrigger -AtLogOn -User $env:USERNAME
Action = New-ScheduledTaskAction -Execute 'powershell.exe' -Argument '-WindowStyle Hidden -Command "{Get-InstalledModule | Update-Module}"'
Settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -RunOnlyIfNetworkAvailable
}
Register-ScheduledTask @parameters

# Scheduled Tasks: Linux style
# REVIEW CODE before just downloading from URLs
[System.Net.WebClient]::new().DownloadFile('https://raw.githubusercontent.com/PowerShell/PowerShell/master/demos/crontab/CronTab/CronTab.psm1', 'CronTab.psm1')

# Perfect access to cron via PowerShell :)
Import-Module .CronTab.psm1

# Register new cron job to run every day at 05:00
New-CronJob -Minute 0 -Hour 5 -Command "pwsh -Command '&{Get-InstalledMOdule | Update-Module}'"
Get-CronTab

When using the PowerShell profile for a module update, you need to keep in mind that the entire profile script is executed when you start a new PowerShell session. Therefore, it is better to start the module update on a background job and have the job inform you with an event when it is done, as we saw in Chapter 4, Advanced Coding Techniques:

# The profile could also be used to update modules. Be careful however
# as this process might take a substantial amount of time!
Add-Content -Path $profile -Value 'Get-InstalledModule | Update-Module' -Force

# To be more flexible in your session you can register a job and have the job
# inform you when it is done.
$job = Start-Job { Get-InstalledModule | Update-Module -Force -ErrorAction SilentlyContinue }

$null = Register-ObjectEvent $job -EventName StateChanged -SourceIdentifier JobEnd -Action {
if($sender.State -eq 'Completed')
{
$duration = $job.PSEndTime - $job.PSBeginTime
Write-Host "Module update finished in $duration. Results available in `$global:jobInfo"
$global:jobInfo = Receive-Job $job
$job | Remove-Job
}
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset