Reading Time: 3 minutes

Hi everyone! #WAC extensions list is growing day by day and more functionality is being added to our favorite management tool. But, there is a small key feature missing at the moment. The possibility to perform automatic updates for the extensions you are using.

For this reason, I took a little bit of time to compile the following PowerShell script which it takes care of the extension update process for us. So let’s break down a bit the script.

The following script was tested on a #WAC Gateway Server.

Step 1: Import the WAC module to the current session

Firstly, we need to add the #WAC module to the current session in order to leverage the extension update function that comes with it. The script must be executed from the target machine where the #WAC is hosted.

# Add the module to the current session

Import-Module "$env:ProgramFiles\windows admin center\PowerShell\Modules\ExtensionTools" -Verbose

Step 2: Specify the WAC gateway

During this step, we need to specify the #WAC url address which the updater will run again to.

# Specify the WAC gateway

$WAC = "https://lab-wac-core"

Step 3: Getting a list of all the outdated extensions

The following on-liner will query against the #WAC extensions and list only the ones that are outdated.

# List the WAC extensions

$extensions = Get-Extension $WAC | Where-Object {$_.isLatestVersion -like 'False'}

Step 4: Update #WAC outdated extensions and produce a log file

Having a list with all the extensions that are missing the missing updates from the previous step, we are now able to initiate the update process from the default extension feed “https://aka.ms/sme-extension-feed”. The feed is not included below because its the default one.

# Update WAC extensions and produce log file

ForEach($extension in $extensions)
{    
    Update-Extension $WAC -ExtensionId $extension.Id -Verbose | out-file -append C:\Users\Public\WACUpdateLog$(get-date -f MM-dd-yyyy_HH_mm).txt
}

Step 5: Cleanup log files older than 5 days

During the update process, logs are produced under the folder C:\Users\Public\. So in this step, we do casual housekeeping.

# Delete log files older than 5 days

Get-ChildItem –Path  "C:\Users\Public\WACUpdateLog*" –Recurse -include *.log | Where-Object { $_.CreationTime –lt (Get-Date).AddDays(-5) } | Remove-Item

Step 6: Write contents of the log file to Windows event log

Finally, the contents of the log files will be written in Windows Event Log under Application events.

# Write log file to Windows event log

$Log = Get-Content -path C:\Users\Public\WACUpdateLog$(get-date -f MM-dd-yyyy_HH_mm).txt | Out-String

Write-EventLog -LogName Application -Source “WAC Extension Updater” -EntryType Information -EventId 0 -Message $Log

Wrap up

So this it! Wrap it up to a PowerShell script, save it on the server in which the #WAC is hosted and create a Scheduled Task to execute the script on a daily basis! Make sure the user that will be used during the execution of the script, has the role of Gateway administrator.

# Add the module to the current session

Import-Module "$env:ProgramFiles\windows admin center\PowerShell\Modules\ExtensionTools" -Verbose

# Specify the WAC gateway

$WAC = "https://lab-wac-core"

# List the WAC extensions

$extensions = Get-Extension $WAC | Where-Object {$_.isLatestVersion -like 'False'}

# Update WAC extensions and produce log file

ForEach($extension in $extensions)
{    
    Update-Extension $WAC -ExtensionId $extension.Id -Verbose | out-file -append C:\Users\Public\WACUpdateLog$(get-date -f MM-dd-yyyy_HH_mm).txt
}

# Delete log files older than 5 days

Get-ChildItem –Path  "C:\Users\Public\WACUpdateLog*" –Recurse -include *.log | Where-Object { $_.CreationTime –lt (Get-Date).AddDays(-5) } | Remove-Item


# Write to Windows event log

$Log = Get-Content -path C:\Users\Public\WACUpdateLog$(get-date -f MM-dd-yyyy_HH_mm).txt | Out-String

Write-EventLog -LogName Application -Source “WAC Extension Updater” -EntryType Information -EventId 0 -Message $Log -Verbose

Thanks for reading!