Skip to content

Instantly share code, notes, and snippets.

@joegasper
Last active February 16, 2024 12:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joegasper/bfb0e83dd3c3bbe18e72f47b99ad4012 to your computer and use it in GitHub Desktop.
Save joegasper/bfb0e83dd3c3bbe18e72f47b99ad4012 to your computer and use it in GitHub Desktop.
PowerShell function to update Azure File Sync (AFS) Storage Agent on computer(s).
<#
.SYNOPSIS
Updates the Azure Files Storage Sync Agent on specified computers.
.DESCRIPTION
This cmdlet updates the Azure Files Storage Sync Agent on the specified computers by retrieving the installation directory from the registry, importing the update agent module DLL, and running the update process.
.PARAMETER ComputerName
Specifies the computer name on which the Azure Files Storage Sync Agent will be updated.
.EXAMPLE
Update-AfsStorageSyncAgent -ComputerName 'Computer1', 'Computer2', 'Computer3'
Updates the Azure Storage Sync Agent on Computer1, Computer2, and Computer3.
.EXAMPLE
'Computer1', 'Computer2', 'Computer3' | Foreach-Object -Parallel {. C:\Scripts\Update-AfsStorageSyncAgent.ps1; Update-AfsStorageSyncAgent -ComputerName $_} -ThrottleLimit 5
Updates the Azure File Share Storage Sync Agent on Computer1, Computer2, and Computer3 in parallel (requires PowerShell 7).
.NOTES
Requires PowerShell remoting to be enabled and configured on the target computers.
#>
function Update-AfsStorageSyncAgent {
[CmdletBinding()]
param(
[Parameter(Mandatory=$true, ValueFromPipeline=$true)]
[string[]]$ComputerName
)
begin {
# Initialization code here
Write-Host 'Starting the Azure File Sync Agent update process...'
# The script block to run remotely
$scriptBlock = {
# Get the InstallDir value from the registry
$installDir = (Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Azure\StorageSync\Agent' -Name 'InstallDir' -ErrorAction SilentlyContinue).InstallDir
# Proceed if the agent has been installed
if ($installDir) {
# Change the directory to the InstallDir path
Set-Location -Path $installDir
# Set the path to the AFS updater module
$AgentUpdaterModulePath = Join-Path -Path $installDir -ChildPath 'Kailani.Afs.Updater.Cmdlets.dll'
# Import the module and run the cmdlet to search and install latest update
Import-Module $AgentUpdaterModulePath
# Check if there is an update available and install
if ( (Get-StorageSyncAgentUpdate).UpdateNeeded ) {
# Run the cmdlet to search and install latest update
Write-Host "$env:COMPUTERNAME : Starting the agent updater action."
Install-StorageSyncAgentUpdate -Force
}
}
}
}
process {
# Run through a foreach to also support multiple items in the function parameter
foreach ($Name in $ComputerName) {
# Execute the script block on the current computer in the pipeline
Invoke-Command -ComputerName $Name -ScriptBlock $scriptBlock
}
}
end {
# Cleanup code here
Write-Host "Completed the Azure File Sync Storage Agent update process."
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment