Skip to content

Instantly share code, notes, and snippets.

@grintor
Last active February 28, 2023 16:11
Show Gist options
  • Save grintor/02b37565c0a86f776bf3f84fb96725c3 to your computer and use it in GitHub Desktop.
Save grintor/02b37565c0a86f776bf3f84fb96725c3 to your computer and use it in GitHub Desktop.
eClinicalworks Automatic Upgrade Script
# A tool to automatically update eClinicalworks EMR to the server version
#
# Copyright (c) 2022 "Chris Wheeler" <[email protected]>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
$inst_path = "C:\Program Files (x86)\eClinicalWorks"
# first lets make sure ecw is even installed
if (-Not (Test-Path -Path "$inst_path\configuration.xml" -PathType Leaf)) {
echo "eCw configuration not found"
$host.SetShouldExit(3)
exit
}
# make sure the script it running elevated
$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
if (-Not $currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)){
echo "script must be run with elevated privileges"
$host.SetShouldExit(4)
exit
}
# parse the config xml to get the app server hostname
$conf_xml = (Select-Xml -Path "$inst_path\configuration.xml" -Xpath /configdata)
$app_server = $conf_xml.Node.server
echo "app server is at: $app_server"
# parse the config xml to figure out if we need to use TLS
$app_server_protocol = 'http'
if ($conf_xml.Node.https.ToLower() -eq 'yes'){
$app_server_protocol = 'https'
}
echo "using protocol: $app_server_protocol"
# get the installed ecw version
$exe_version = [System.Diagnostics.FileVersionInfo]::GetVersionInfo("$inst_path\eclinicalworks.exe").FileDescription
echo "exe version is $exe_version"
# get the server ecw version
$item_key_uri = $app_server_protocol + "://$app_server/mobiledoc/jsp/catalog/xml/getAllItemKeys.jsp"
try {
$item_key_response = Invoke-RestMethod -UseBasicParsing -Uri $item_key_uri
$item_key_data = $item_key_response.Envelope.Body.return.resultset.data.row
$server_version = ($item_key_data | Where-Object { $_.name -eq 'ClientVersion' }).Value
echo "Server Version is $server_version"
} catch {
echo "error getting server version info"
$host.SetShouldExit(5)
exit
}
# check if the server version is the same as the installed version, exit if so
if ($server_version -eq $exe_version) {
echo "local version matches server version, nothing to do."
$host.SetShouldExit(0)
exit
}
# figure out where to download the update from
$update_url = ($item_key_data | Where-Object { $_.name -eq 'UpgradeFtpServer' }).Value
echo "downloading update from $update_url"
# actually download the update
try {
(New-Object Net.WebClient).DownloadFile($update_url, "$inst_path\CwClient.zip")
} catch {
echo "error downloading update"
$host.SetShouldExit(6)
exit
}
# we can't update if ecw is in use, so kill it if so
echo "killing any running ecw instances"
Stop-Process -Force -Name "eClinicalWorks" -ea SilentlyContinue
# overwrite the install directory with contents of the update zip file
echo "extracting update"
Expand-Archive -Force -LiteralPath "$inst_path\CwClient.zip" -DestinationPath $inst_path
# run CwReg.bat against the newly extracted files
echo "registering Dlls"
Start-Process -FilePath "$inst_path\CwReg.bat" -WorkingDirectory $inst_path -Wait
echo "update complete."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment