#Datto Device Agent updater #Ryan Shoemaker #Ambitions Consulting Group #505-234-7777 #20MAR2019 Updated version lookup to find last version listed on page, as datto has forked versions and now lists multiple #08NOV2018 Updated Datto agent regex lookup to includ any number of sub versions. Will now find 2.0.7.1 and not just 2.0.7 Start-transcript -path C:\Ambitions\DattoUpdate.log -force -append #=====================================================================================================================# #Script logic #Determine if Shadowsnap agent is installed #If so # Designate #Designate Registry key to read version number from hklm:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\ShadowSnap # Designate download update location https://www.datto.com/downloads/shadowsnap-installer.exe # Designate download location to be C:\Ambitions\shadowsnap-installer.exe # Designate which services to work with "ShadowProtect Service, StorageCraft ImageReady, StorageCraft Raw Agent, and StorageCraft Shadow Copy Provider" # Wait to continue if a Shadowsnap agent backup is running # #Determine if Datto Windows agent is installed #If so # Designate #Designate Registry key to read version number from hklm:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\DattoWindowsAgent # Designate download update location https://www.datto.com/downloads/DattoWindowsAgent.exe # Designate download location to be C:\Ambitions\DattoWindowsAgent.exe # Designate which services to work with "Datto Backup Agent Service and DattoProvider" # Wait to continue if a Datto Windows agent backup is running # #If server does not have either, exit! # #Record existing version number #Backup any existing installer and compare the new download to the existing one, only continue if it's different #Stop designated services #Install agent silently (wait till finished) #Set designated services to auto restart if stopped #Start designated services #Log the old and new version number #Exit script #=====================================================================================================================# #Create Prerequisite folder If(!(test-path C:\Ambitions)) { New-Item -ItemType Directory -Force -Path C:\Ambitions } #Determine if Shadowsnap agent is installed if (Get-Service "StorageCraft Raw Agent" -ErrorAction SilentlyContinue) { #Designate Registry key to read version number from $RegKeyVer = "hklm:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\ShadowSnap" #Designate name of agent $agent = "ShadowSnap Agent" #Designate download location $download = "http://download.ambitionsgroup.com/shadowsnap-installer.exe" #Designate save location $installer = "C:\Ambitions\shadowsnap-installer.exe" #Designate which services to work with $services = @("ShadowProtect Service", "StorageCraft ImageReady", "StorageCraft Raw Agent", "StorageCraft Shadow Copy Provider") #Website to look at for the latest version number $website = "https://kb.datto.com/hc/en-us/articles/200555585-Installing-the-ShadowSnap-Agent.html" $InstalledVer = (Get-ItemProperty "$RegKeyVer" -Name "DisplayVersion" -ErrorAction SilentlyContinue).DisplayVersion Function DattoPattern {Set-Variable -Name OnlineDattoversion -Value ((Select-String -Path "C:\Ambitions\dattowebver.txt" -Pattern "current version")[-1]) -Scope Global} #Wait to continue if a backup is running While (Get-Process sbrun -ErrorAction SilentlyContinue) { Write-Host "A backup is running at $(get-date). Waiting 1 minute." start-sleep 60 } Write-Host "No backup is running, continuing update" } #Determine if Datto Windows agent is installed if (Get-Service "Datto Backup Agent Service" -ErrorAction SilentlyContinue) { #Designate Registry key to read version number from $FileVer = "C:\Program Files\Datto\Datto Windows Agent\DattoBackupAgent.exe" #Designate name of agent $agent = "Datto Windows Agent" #Designate download location $download = "https://cf-dl.datto.com/dwa/DattoWindowsAgent.exe" #Designate save location $installer = "C:\Ambitions\DattoWindowsAgent.exe" #Designate which services to work with $services = @("Datto Backup Agent Service", "DattoProvider") #Website to look at for the latest version number $website = "https://kb.datto.com/hc/en-us/articles/360024388231" $InstalledVer = (Get-Item $FileVer).VersionInfo.ProductVersion Function DattoPattern {Set-Variable -Name OnlineDattoversion -Value ((Select-String -Path "C:\Ambitions\dattowebver.txt" -Pattern "Agent Version")[0]) -Scope Global} #Wait to continue if a backup is running While (vssadmin list writers | Select-String "Waiting" | Select-String "for responses." -NotMatch) { Write-Host "A backup is running at $(get-date). Waiting 1 minute."; Start-Sleep 60 } Write-Host "No backup is running, continuing update" } #If neither agent is installed, EXIT! if (!$agent) { Write-Host "No agent is installed, EXITING!" #Exit the script Stop-transcript Exit } #Get Version number [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 (New-Object System.Net.WebClient).DownloadFile("$website", "C:\Ambitions\dattowebver.txt") DattoPattern [void] ($OnlineDattoversion -match '((\d)+\.)+(\d)+') $OnlineDattoVersion = $matches.values.split()[-1] $OnlineDattoversion = $OnlineDattoversion.Trim() If ($InstalledVer -match $OnlineDattoVersion) { Write-Host -ForegroundColor Green 'The'$agent' version '$InstalledVer' is currently installed and it is up to date' #Exit the script Stop-transcript Exit } Else { Write-Host -ForegroundColor Red 'The'$agent' Version '$InstalledVer' is currently installed and it is NOT up to date. The latest version available is '$OnlineDattoVersion } #If existing installer is less then a week old, exit #$CurrentDate = Get-Date #if (Test-Path "$installer") { # if((Get-ChildItem "$installer").LastWriteTime -ge ($CurrentDate.AddDays(-7))){ # Write-Host "Previous installer is less then a week old, skipping update." # Exit # } #} #Don't let old installer be overwritten if it exists #if (Test-Path "$installer") { # Write-Host "An old installer has been detected. Saving it for comparison." # Move-Item -Path "$installer" -Destination "$installer.old" -Force #} #Download update Write-Host "Downloading the update from $download to $installer" (New-Object System.Net.WebClient).DownloadFile("$download", "$installer") #Test if the new and old files match if (Test-Path "$installer.old") { if ((Get-FileHash "$installer").hash -eq (Get-FileHash "$installer.old").hash) { Write-Host "This update has already been downloaded, canceling update." Remove-Item -path "$installer.old" #Exit the script Stop-transcript Exit } Else { Write-Host "The download is newer, proceeding with update." Remove-Item -path "$installer.old" } } #Stop Services foreach ($service in $services) { Write-Host "Stopping service $service" Stop-Service -displayname "$service" } #Let services have time to fully stop Start-Sleep -s 5 #Kill any hung services Stop-Process -processname raw_agent_svc, ShadowProtect, ShadowProtectSvc, vsnapvss, DattoBackupAgent -ErrorAction SilentlyContinue #Install agent silently (wait till finished) Write-Host Installing the new update & "$installer" /S /norestart # Let installer have time to fully wrap up Start-Sleep -s 30 #Configure services foreach ($service in $services) { Write-Host "Setting auto-restart for service $service" #Get service name from Display Name $servicename = (Get-Service -DisplayName "$service" | Format-Table -hidetableheaders Name | Out-String) $servicename = $servicename -replace '(^\s+|\s+$)','' -replace '\s+',' ' #Set services to auto restart if stopped sc.exe failure $servicename actions= restart/180000/restart/180000/""/180000 reset= 86400 #Start services Write-Host "Starting service $service" Start-Service -displayname "$service" } #Show if services are running foreach ($service in $services) { Get-Service -displayname "$service" } #Get Version number If ($agent -eq "ShadowSnap Agent") { $NewInstalledVer = (Get-ItemProperty "$RegKeyVer" -Name "DisplayVersion" -ErrorAction SilentlyContinue).DisplayVersion $NewInstalledVer = $NewInstalledVer -replace '(^\s+|\s+$)','' -replace '\s+',' ' } If ($agent -eq "Datto Windows Agent") { $NewInstalledVer = (Get-Item $FileVer).VersionInfo.ProductVersion $NewInstalledVer = $NewInstalledVer -replace '(^\s+|\s+$)','' -replace '\s+',' ' } #$PreInstalledVer = $InstalledVer #Remove-Variable InstalledVer Write-Host $agent" version $NewInstalledVer has been installed over version $InstalledVer" #Log the major system Change $date = Get-Date $date = $date.ToShortDateString() Add-Content C:\Ambitions\DattoUpdateLog.txt "$date Scripted The $agent was updated from version $InstalledVer to $NewInstalledVer . More info: C:\Ambitions\DattoUpdate.log" #Exit the script Stop-transcript Exit