GAMADV Windows Auto update

629 views
Skip to first unread message

Gabriel Clifton

unread,
Mar 29, 2021, 5:29:42 PM3/29/21
to GAM for Google Workspace
I have created a batch / powershell script that auto updates GAMADV. I have a few, not many, servers that run GAM scripts daily and since most of those servers are barely ever touched, I decided to write an auto update script to ensure GAM is always up to date without me needing to log in preventing GAM from getting too far behind on versions. I suck at powershell so if someone wants to rewrite it to all powershell, GREAT. (Watch word wrap!)

Batch script.
@echo off
cls
REM Searches current directory for existing GAMADV installer and deletes it.
for /f %%a in ('dir /b /s %~dp0gamadv-xtd3-*-windows-x86_64.msi') do del "%%a" /f /s /q 2>NUL 1>NUL
REM Use gam version check to get current installations version and current version
for /f "delims=: tokens=2" %%a in ('gam version check ^|find "Current:"') do set current=%%a
for /f "delims=: tokens=2" %%c in ('gam version check ^|find "Latest:"') do set latest=%%c

REM Compare version numbers
set current=%current: =%
set latest=%latest: =%
if %current% GEQ %latest% goto :eof
if %current% LSS %latest% goto :download
exit /b

:download
powershell.exe -noprofile -File "%~dp0Download GAM-ADV.ps1" %latest% -verb RunAs
msiexec.exe /I %~dp0gamadv-xtd3-*-windows-x86_64.msi /QN


Powershell script:
param($latest)
Clear-Host
write-host "`n"

$client = new-object System.Net.WebClient
$client.DownloadFile("https://github.com/taers232c/GAMADV-XTD3/releases/download/v$latest/gamadv-xtd3-$latest-windows-x86_64.msi","$PSScriptRoot\gamadv-xtd3-$latest-windows-x86_64.msi")

Gabriel Clifton

unread,
Mar 29, 2021, 5:40:04 PM3/29/21
to GAM for Google Workspace
Edit, I have found that on some of my servers, I must include the following before I can invoke a download so I have it prior to the Clear-Host line.
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

Kim Nilsson

unread,
Jul 14, 2021, 12:09:02 PM7/14/21
to GAM for Google Workspace
You might be able to simplify your code using the built-in version checker.

gam version checkrc

sets the return code to 0 if the current version is the latest version
sets the return code to 1 if the current version is not the latest

I am going to try to see if I can put something together in bash for myself. :-)

JayP

unread,
Oct 4, 2021, 7:05:53 PM10/4/21
to GAM for Google Workspace
Hi! Curious to see if you were able to put a script together for this. :)

Maj Marshall Giguere

unread,
Oct 4, 2021, 8:47:34 PM10/4/21
to google-ap...@googlegroups.com
See attached bash script. I use it manually, but it could be put into your crontab.  I don't bother.  I just wait for Ross to issue a notice of update,  then run gitGAM.
--
Maj Marshall E Giguere
NH Wing Director of IT
Nashua Composite Squadron IT Officer
Civil Air Patrol, U.S. Air Force Auxiliary




Volunteers serving America's communities, saving lives, and shaping futures.


--
You received this message because you are subscribed to the Google Groups "GAM for Google Workspace" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-man...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-manager/7eff1727-4ca1-4b98-8014-7bbdb8388c89n%40googlegroups.com.
gitGAM

Kim Nilsson

unread,
Oct 5, 2021, 9:41:32 AM10/5/21
to Google Apps Manager
I too run mine manually, but at least it doesn't download a version unless there's a new version.

$ cat bin/update
#!/bin/bash
#
# Download and install latest compiled GAMADV-XTD3
#
if /home/kim/gamadv-xtd3/gam version checkrc; then
        echo $? #This line isn't necessary. I have it just to see the error code from the checkrc command. 0 means TRUE and that the versions match.
        echo "You are using the current version!"
        exit
else
        echo $? #This line isn't necessary. Just like the one above.
        echo "There's a newer version of GAMADV-XTD3. Downloading it now."
        bash <(curl -s -S -L https://git.io/fhZWP) -l -d /home/kim
        more /home/kim/gamadv-xtd3/GamUpdate.txt #This line opens the GamUpdate.txt file, so I can see what new features or bugfixes has been added.
        #If you are to fully automate the updating, you need to disable this line completely.
fi

/Kim
--
There is No Substitute!

Maj Marshall Giguere

unread,
Oct 5, 2021, 11:32:15 AM10/5/21
to google-ap...@googlegroups.com
That's what the -L switch is for


Maj Marshall E. Giguere, CAP
NH Wing Director of IT
U.S. Air Force Auxiliary
   



--
You received this message because you are subscribed to the Google Groups "GAM for Google Workspace" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-man...@googlegroups.com.

Chris River

unread,
Oct 5, 2021, 5:00:44 PM10/5/21
to GAM for Google Workspace
If you'd like a pure Powershell script, I think you can adapt this as needed (this assumes the script is running in the same directory that GAM is installed to):

if (.\gam version checkrc) {
  $releases = curl "https://api.github.com/repos/taers232c/GAMADV-XTD3/releases" | ConvertFrom-Json
  $dlurl = ($releases[0].assets | where {$_.name -like "*windows*64.zip"}).browser_download_url
  $dir = (Get-Location).Path
  (new-object System.Net.WebClient).DownloadFile($dlurl, "$dir\gamadv-xtd3-latest-windows-x86_64.zip")
  Expand-Archive "$dir\gamadv-xtd3-latest-windows-x86_64.zip" "$dir\" -Force
  mv "$dir\gamadv-xtd3\*" "$dir\" -Force
  rm "$dir\gamadv-xtd3\"
} else {
  # Nothing, GAM is already up-to-date

Paul Ogier

unread,
Nov 5, 2021, 7:22:43 AM11/5/21
to GAM for Google Workspace
Hey Chris great script!

Used on this video https://youtu.be/bUMHupsFq-w about how to upgrade GAMADV-XTD3 on Windows, Linux and macOS. 

The formatting of the Google Group seemed to remove your last } from your script. Not sure if you can update that for others that come and see it?

Chris River

unread,
Nov 5, 2021, 10:02:42 AM11/5/21
to GAM for Google Workspace
Ah, thanks for the heads up, and the mention! It looks like the last closing curly brace was hidden under the 3 dots in Google Groups, apparently it thinks it's part of a signature or something.

Here's the code again, but slightly updated. This code block shows all of the changes between the old version and the updated version, so you can easily see what has changed. This won't help if you have the script run via a scheduled task (as it will update in the background and you won't see the output); I prefer to update manually in case I have a GAM script that needs to run overnight or something. The output will also be overwhelming if you're updating from a rather old version as well. :)

if (.\gam version checkrc) {
  $releases = curl "https://api.github.com/repos/taers232c/GAMADV-XTD3/releases" | ConvertFrom-Json
  $dlurl = ($releases[0].assets | where {$_.name -like "*windows*64.zip"}).browser_download_url
  $dir = (Get-Location).Path
  (new-object System.Net.WebClient).DownloadFile($dlurl, "$dir\gamadv-xtd3-latest-windows-x86_64.zip")
  $oldchangeloglinescount=(Get-Content .\GamUpdate.txt | Select-String .*).count
  Expand-Archive "$dir\gamadv-xtd3-latest-windows-x86_64.zip" "$dir\" -Force
  mv "$dir\gamadv-xtd3\*" "$dir\" -Force
  rm "$dir\gamadv-xtd3\"
  $newchangeloglinescount=(Get-Content .\GamUpdate.txt | Select-String .*).count
  Get-Content .\GamUpdate.txt -Head ($newchangeloglinescount-$oldchangeloglinescount-1)
} else {
  # Nothing, GAM is already up-to-date
}

The changelog is stored in the GamUpdate.txt file. The updated script gets the number of lines in the old GamUpdate file and the new one, subtracts them to get the count of new lines added, then reads this number of lines in that file to the console.

For Linux users, I use this bash script to update GAM (and display the changelog):
oldchangeloglinescount=$(wc -l ~/bin/gamadv-xtd3/GamUpdate.txt | awk '{print $1;}')
newchangeloglinescount=$(wc -l ~/bin/gamadv-xtd3/GamUpdate.txt | awk '{print $1;}')
GREEN='\033[0;32m'
NC='\033[0m' # No Color
printf "${GREEN}This update includes the following changes:${NC}\n"
head -n $(($newchangeloglinescount - $oldchangeloglinescount)) ~/bin/gamadv-xtd3/GamUpdate.txt

Hopefully some others find that helpful, and hopefully with this last sentence here Google Groups won't hide any lines from the above scripts. :)

Paul Ogier

unread,
Nov 8, 2021, 11:21:34 AM11/8/21
to GAM for Google Workspace
Thanks Chris!
Message has been deleted

Paul Ogier

unread,
Dec 15, 2021, 7:58:36 AM12/15/21
to GAM for Google Workspace
Hey James, 

What are the errors you are getting when trying to run this? 

Check this video to see how it works https://youtu.be/bUMHupsFq-w

On Wednesday, 15 December 2021 at 14:49:19 UTC+2 James Hatz wrote:
I'm getting an error trying to run this.. do I need to edit it in any way? 

James Hatz

unread,
Dec 15, 2021, 8:00:49 AM12/15/21
to GAM for Google Workspace
I hadn't read up far enough to see that the script needed to be in the gam directory.  I just moved and tested it - worked great! Thank you!

Paul Ogier

unread,
Dec 15, 2021, 8:02:57 AM12/15/21
to GAM for Google Workspace
It happens to the best of us!

Paul Ogier

unread,
Jan 3, 2023, 6:45:35 AM1/3/23
to GAM for Google Workspace
Hey Chris,

Is your script to update GAM still working? Mine just lists the whole release page now. 
2023-01-03_13-44-36.png

Chris River

unread,
Jan 3, 2023, 9:57:12 AM1/3/23
to GAM for Google Workspace
Hm, it looks like the return code value is flipped between Windows/Powershell and Linux/Bash for gam version checkrc. The documentation (https://github.com/taers232c/GAMADV-XTD3/wiki/Version-and-Help) says the return code will be set to 0 if the current version is the latest version, and 1 if not; and this aligns with what I'm seeing in my Linux environment. But in Windows, I'm getting a 1 if the current version is the latest, and a 0 when not.

So, here's an updated script that performs the version checking ourselves instead. I also removed the extra " - 1" from the get-content line; this was just there to remove the extra blank line at the end, but it resulted in the entire contents being displayed when the update downloads the same version, which might be less desirable.

$version = gam version check
$latest = ($version | Select-String -Pattern '^\s+Latest: (\d+.+)$').Matches[0].Groups[1].Value
$current = ($version | Select-String -Pattern '^\s+Current: (\d+.+)$').Matches[0].Groups[1].Value
if ($current -ne $latest) {
  $releases = curl "https://api.github.com/repos/taers232c/GAMADV-XTD3/releases" | ConvertFrom-Json
  $dlurl = ($releases[0].assets | where {$_.name -like "*windows*64.zip"}).browser_download_url
  $dir = (Get-Location).Path
  (new-object System.Net.WebClient).DownloadFile($dlurl, "$dir\gamadv-xtd3-latest-windows-x86_64.zip")
  $oldchangeloglinescount=(Get-Content .\GamUpdate.txt | Select-String .*).count
  Expand-Archive "$dir\gamadv-xtd3-latest-windows-x86_64.zip" "$dir\" -Force
  mv "$dir\gamadv-xtd3\*" "$dir\" -Force
  rm "$dir\gamadv-xtd3\"
  $newchangeloglinescount=(Get-Content .\GamUpdate.txt | Select-String .*).count
  Get-Content .\GamUpdate.txt -Head ($newchangeloglinescount-$oldchangeloglinescount)
} else {
  # Nothing, GAM is already up-to-date
}

Chris River

unread,
Jan 4, 2023, 3:43:07 PM1/4/23
to GAM for Google Workspace
Ross kind of pointed me in the right direction, as he confirmed that the return code is identical between platforms. Powershell sets $? to True (or 1) if the previous command has a return code of 0 (for success), while Bash sets $? to the value of the return code. So, "if (gam version checkrc)" ends up being reversed between Powershell and Bash. For Powershell, "$lastexitcode" stores the actual return code value.

I thought I had tested the original script and it was behaving as expected, but maybe I missed something when I was testing or something.

In any case, here's a corrected version of the script, where we don't compare the version values ourselves in Powershell, as it is likely more reliable to just use the return code from gam version checkrc:

$version = gam version checkrc
if ($lastexitcode -eq 1) {

  $releases = curl "https://api.github.com/repos/taers232c/GAMADV-XTD3/releases" | ConvertFrom-Json
  $dlurl = ($releases[0].assets | where {$_.name -like "*windows*64.zip"}).browser_download_url
  $dir = (Get-Location).Path
  (new-object System.Net.WebClient).DownloadFile($dlurl, "$dir\gamadv-xtd3-latest-windows-x86_64.zip")
  $oldchangeloglinescount=(Get-Content .\GamUpdate.txt | Select-String .*).count
  Expand-Archive "$dir\gamadv-xtd3-latest-windows-x86_64.zip" "$dir\" -Force
  mv "$dir\gamadv-xtd3\*" "$dir\" -Force
  rm "$dir\gamadv-xtd3\"
  $newchangeloglinescount=(Get-Content .\GamUpdate.txt | Select-String .*).count
  Get-Content .\GamUpdate.txt -Head ($newchangeloglinescount-$oldchangeloglinescount)
} else {
  # Nothing, GAM is already up-to-date
}

Paul Ogier

unread,
Jan 5, 2023, 9:15:15 AM1/5/23
to GAM for Google Workspace
Apologies for the delay. 

So I tried yours and it works great. Thanks for consulting with Ross on this. 

I decided to add more commenting into it, in case others want to update. This way it is easy to see what each line does. 

# Check the version of GAM

$version = gam version checkrc

# If the last exit code is 1, GAM is not up-to-date
if ($lastexitcode -eq 1) {
  # Get the latest release from the GAMADV-XTD3 repository on GitHub

  $releases = curl "https://api.github.com/repos/taers232c/GAMADV-XTD3/releases" | ConvertFrom-Json
  # Get the download URL for the latest release

  $dlurl = ($releases[0].assets | where {$_.name -like "*windows*64.zip"}).browser_download_url
  # Get the current location
  $dir = (Get-Location).Path
  # Download the latest release

  (new-object System.Net.WebClient).DownloadFile($dlurl, "$dir\gamadv-xtd3-latest-windows-x86_64.zip")
  # Save the number of lines in the change log

  $oldchangeloglinescount=(Get-Content .\GamUpdate.txt | Select-String .*).count
  # Extract the contents of the zip file

  Expand-Archive "$dir\gamadv-xtd3-latest-windows-x86_64.zip" "$dir\" -Force
  # Move the extracted files to the current location

  mv "$dir\gamadv-xtd3\*" "$dir\" -Force
  # Remove the empty gamadv-xtd3 directory
  rm "$dir\gamadv-xtd3\"
  # Save the number of lines in the updated change log

  $newchangeloglinescount=(Get-Content .\GamUpdate.txt | Select-String .*).count
  # Get the new lines in the change log
  Write-Host "Latest Changes in GAMADV-XTD3" -ForegroundColor Red -BackgroundColor White

  Get-Content .\GamUpdate.txt -Head ($newchangeloglinescount-$oldchangeloglinescount)

  # Display a message saying that it has been updated.
  Write-Host "GAMADV-XTD3 has been updated" -ForegroundColor Red -BackgroundColor White
} else {
  # GAM is already up-to-date
    Write-Host "GAMADV-XTD3 is already up-to-date" -ForegroundColor Green

}

Pause





I added a couple of lines at the bottom to explain that it is either updated or not. Also labelled the latest updates. The pause at the bottom, just allows the person to see what happened before the window closes. 

This was tested by myself and a couple of the GAMADV-XTD3 course students. 

For future reference, I will update this thread and this file https://drive.google.com/file/d/19pw82g0tNhOw6eMcIVaF-WYyXIjG9tfk/view?usp=sharing




Reply all
Reply to author
Forward
0 new messages