Windows upgrade script

Hi,

I’m new to this community and dont know if this is the right way to go but i want to contribute a bit.

Im working on an powershell update script for windows

i’ve set up a little draft:

$openhabinstallfolder = "C:\openhab-2.4.0_test" #the directory of the current installation
$backupdir = "c:\temp\bckdir\" #directory to backup the current installation

if ($openhabinstallfolder.Substring($openhabinstallfolder.length - 1, 1) -eq "\")
{
	$openhabinstallfolder = $openhabinstallfolder.Substring(0, $openhabinstallfolder.length - 1)
}
$filestoremovehash = @{
	"files" = "userdata\etc\branding.properties", "userdata\etc\branding-ssh.properties", "userdata\etc\config.properties", "userdata\etc\custom.properties", "userdata\etc\custom.system.properties", "userdata\etc\distribution.info", "userdata\etc\jre.properties", "userdata\etc\org.openhab.addons.cfg", "userdata\etc\org.ops4j.pax.url.mvn.cfg", "userdata\etc\overrides.properties", "userdata\etc\profile.cfg", "userdata\etc\startup.properties", "userdata\etc\system.properties", "userdata\etc\version.properties"
	"startwith" = "userdata\etc\org.apache.karaf"
	"dirs" = "userdata\cache", "userdata\tmp", "runtime"
}

if (test-path "$openhabinstallfolder\runtime\bin\karaf.bat")
{
	
	write-host "Checking if Openhab is running"
	if ((Get-Process | where-object { $_.ProcessName -eq "openHAB2-wrapper" }).count -eq 0)
	{
		Write-Host "Creating backup"
		
		if (((Test-Path $backupdir) -eq $false))
		{
			New-Item -ItemType directory -Path $backupdir -Force
			
		}
		Copy-Item -Path $openhabinstallfolder -Destination $backupdir -Recurse -Verbose
		
		foreach ($filetodelete in $filestoremovehash.files)
		{
			if((Test-Path "$openhabinstallfolder\$filetodelete") -eq $true){
			Remove-Item "$openhabinstallfolder\$filetodelete" -Force -Verbose
			}
			else
			{
				Write-Host "Skipping $openhabinstallfolder\$filetodelete" 	
			}
			
		}
		foreach ($startwith in $filestoremovehash.startwith)
		{
			
			Remove-Item "$openhabinstallfolder\$startwith*" -Force -Verbose
			
		}
		foreach ($dirstodelete in $filestoremovehash.dirs)
		{
			Remove-Item "$openhabinstallfolder\$dirstodelete" -Force -Recurse -Verbose
			
		}
		
		
		write-host "Starting installation process"
		$json = (Invoke-WebRequest -UseBasicParsing -uri https://ci.openhab.org/job/openHAB-Distribution/api/json).content | ConvertFrom-Json
		$selectedbuild = (Invoke-WebRequest -UseBasicParsing -uri "$($json.lastBuild.url)api/json").content | ConvertFrom-Json
		$selectedartifact = ($selectedbuild.artifacts | where-object { $_.filename -like "*.zip" })
		$downloadpath = $selectedbuild.url + "artifact/" + $selectedartifact.relativePath
		write-host "Downloading latest snapshot"
		Invoke-WebRequest -UseBasicParsing -Uri $downloadpath -OutFile "$env:temp\$($selectedartifact.filename)"
		$expandfolder = "$env:temp\$($($selectedartifact.filename).substring(0, $($selectedartifact.filename).length - 4))"
		write-host "Expanding archive"
		Expand-Archive -Path "$env:temp\$($selectedartifact.filename)" -DestinationPath $expandfolder -Force
		Set-Location $expandfolder
		$files = get-childitem  $expandfolder -Recurse -file | resolve-path -Relative
		foreach ($file in $files)
		{
			$destination = "$openhabinstallfolder\$($file)"
			if (test-path $destination)
			{
				write-host "File $file already exists, skipping" -BackgroundColor Yellow
			}
			else
			{
				
				if ((test-path $destination.Substring(0, $destination.LastIndexOf("\"))) -eq $false)
				{
					write-host "Creating folder $($destination.Substring(0, $destination.LastIndexOf("\"))) " -BackgroundColor Green
					New-Item -ItemType directory -Path $destination.Substring(0, $destination.LastIndexOf("\"))
					
				}
				write-host "Copying $file to $destination " -BackgroundColor Green
				Copy-Item -Path $file -Destination $destination -force
			}
			
		}
		write-host "Cleaning up"
		Set-Location $env:HOMEPATH
		Remove-Item $expandfolder -Recurse -Force
		Remove-Item "$env:temp\$($selectedartifact.filename)" -Force
	}
	Else
	{
		Write-host "Openhab is running please shutdown before running this script" -BackgroundColor Red
		
	}
}
else
{
	write-host "Openhab installation folder is not correct" -BackgroundColor Red
}

Does anyone has any comments or can someone test this?

1 Like

Nice job.
Does the integrated update script for Windows not work?

Nice!

HA! tbh i wasn’t aware of such script because if this article:

There is currently no automatic update script for Windows. To update manually, download a later version of the openHAB distribution zip file and follow these steps:

I helped on writing the update script and didn’t know about that doc issue - I’ll post a PR shortly to fix that. Thanks!

Also - not many OH users are windows users - love to get your feedback on the script. Also - I wrote a windows installer I’d love to hear feedback on as well (see the last post in https://github.com/openhab/openhab-distro/issues/807 - go to the link shown in github and press the “Releases” link on that page to download it)…

EDIT: the installer will allow you to setup backup schedule, update schedule and provide a windows service to use (much better than the karaf windows service)

3 Likes