param ( [string]$server, [string]$token, [string]$location, [string]$devFolder, [string]$serverIP, [string]$build, [string]$type, [string]$username, [string]$admpass, [string]$obslst, [string]$obsBuild ) $scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Definition $rootDir = Join-Path $scriptDir ".." # example run script is # .\updateServer.ps1 -username "username" -admpass "password" -token "test3" -server "usmcd1vms036" -serverIP "10.193.0.56" -build "C:\Users\matthes01\Documents\logistics_support_tool\releases" -location "E:\LST" # Convert the plain-text password to a SecureString $securePass = ConvertTo-SecureString $admpass -AsPlainText -Force $credentials = New-Object System.Management.Automation.PSCredential($username, $securePass) # lets get the version of the app we are updating $pkgFile = "$rootDir\package.json" $package = Get-Content -Path $pkgFile -Raw | ConvertFrom-Json $buildNumberFile = Join-Path $rootDir "BUILD_NUMBER" if (Test-Path $buildNumberFile) { $buildNumber = Get-Content $buildNumberFile -Raw $buildNumber = $buildNumber.Trim() Write-Host "Current build number: $buildNumber" } else { Write-Warning "BUILD_NUMBER file not found at $buildNumberFile" } $version = "$($package.version)-$($buildNumber)" # Checking to make sure the server is up and online Write-Output "Checking if $($token) is online to update." $pingResult = Test-Connection -ComputerName $serverIP -Count 2 -Quiet if (-not $pingResult) { Write-Output "Server $($server) $($serverIP) is NOT reachable. Exiting script." exit 1 # Terminate the script with a non-zero exit code } Write-Output "Server $($server) ($serverIP) is online." $buildZip = "release-v$version.zip" if (-Not (Test-Path -Path "$($build)\$($buildZip)")) { Write-Host "Build is missing from the build folder." Write-host $buildZip exit } else { Write-Host "We have the build lets move to next step" } Write-Host "Starting the update Process" Write-Host "File to be copied over is $buildZip" Write-Host "Coping files to $($server)" $destination = "\\$($server)\$($location)" -replace ":", "$" Write-Host $destination Write-Host "Forcing the removal of the mapped drive, just incase it was lingering" Get-PSDrive -Name "z" -ErrorAction SilentlyContinue | Remove-PSDrive -Force # Create a mapped drive with credentials using New-PSDrive for the current session try { net use $server /delete /y 2>$null #New-PSDrive -Name "z" -PSProvider FileSystem -Root $destination -Credential $credentials New-PSDrive -Name "Z" -PSProvider FileSystem -Root $destination -Persist -Credential $credentials # Create the update folder if it doesn't exist if (-not (Test-Path -Path $destination)) { New-Item -ItemType Directory -Path $destination -Force } # Copying files to the server Write-Host "Copying files to $($server)" Copy-Item -Path "$($build)\$($buildZip)" -Destination "Z:\" -Recurse -Force Write-Host "Files copied to $($server)" } catch { Write-Host "Error: $_" } finally { # Remove the mapped drive after copying if (Get-PSDrive -Name "Z" -ErrorAction SilentlyContinue) { Write-Host "Removing mapped drive..." Remove-PSDrive -Name "Z" } } # create the extracted path $extractedFolderPath = "$server\$location$(if ($token -eq "usiow2") { "_2" })" $plantFunness = { param ($server, $token, $location, $buildFile, $buildLoc) $localPath = $location -replace '\$', ':' $serverFile = "$($localPath)\$buildFile" $serverPath = "$($localPath)" # $appPath = $extractedFolderPath $nssmPath = $serverPath + "\nssm.exe" $npmPath = "C:\Program Files\nodejs\npm.cmd" $serviceApp = "LST_app$(if ($token -eq "usiow2") { "_2" })" #$serviceFrontEnd = "LST-frontend$(if ($token -eq "usiow2") { "_2" })" $serviceOcme = "LST-Ocme$(if ($token -eq "usiow2") { "_2" })" $serviceLstV2 = "LSTV2$(if ($token -eq "usiow2") { "_2" })" Write-Host "In the plant we go!!!!!" # $servicesScript = Join-Path $PSScriptRoot "scripts\services.ps1" # $iisContorl = Join-Path $PSScriptRoot "scripts\iisControls.ps1" ###################################################################################### # Stop the services and iis to do the updates. ###################################################################################### Write-Host "Stopping the services to do the updates, pkgs and db changes." Write-Host "Stopping $serviceApp" Stop-Service -DisplayName $serviceApp -Force Start-Sleep -Seconds 1 Write-Host "Stopping $serviceLstV2" Stop-Service -DisplayName $serviceLstV2 -Force Start-Sleep -Seconds 1 if($token -eq "usday1"){ Write-Host "Stopping $($serviceOcme)" Stop-Service -DisplayName $serviceOcme -Force } Write-Host "Stopping iis application" Stop-WebAppPool -Name LogisticsSupportTool #-ErrorAction Stop Start-Sleep -Seconds 3 ###################################################################################### # Removing the cashed stuff to keep the folder clean. ###################################################################################### # Delete the directories after extraction Write-Host "Deleting app and other cache stuff" Set-Location $serverPath if (-not (Test-Path "app")) { Write-Host "The app folder is already deleted nothing else to do" } else { Remove-Item -LiteralPath "app" -Force -Recurse } if (-not (Test-Path "lstWrapper")) { Write-Host "The lstWrapper folder is already deleted nothing else to do" } else { Remove-Item -LiteralPath "lstWrapper" -Force -Recurse } if (-not (Test-Path "scripts")) { Write-Host "The scripts folder is already deleted nothing else to do" } else { Remove-Item -LiteralPath "scripts" -Force -Recurse } if (-not (Test-Path "tmp")) { Write-Host "The scripts folder is already deleted nothing else to do" } else { Remove-Item -LiteralPath "tmp" -Force -Recurse } ###################################################################################### # Extract everything the cashed stuff to keep the folder clean. ###################################################################################### Write-Host "Unzipping the folder..." try { # Expand the archive Expand-Archive -Path $serverFile -DestinationPath $serverPath -Force # Delete the zip file after extraction Write-Host "Deleting the zip file..." Remove-Item -Path $serverFile -Force } catch { Write-Host "Error: $_" exit 1 # Exit with a non-zero code if there's an error } Write-Host "Unzip the LSTV2 app" #lstv2 loc $lstv2 = Get-ChildItem -Path "$($localPath)\tmp" -Filter *.zip write-host $lstv2[0].FullName if ($lstv2.Count -eq 0) { Write-Host "No zip files found in $localPath\tmp" } else { $zipToExtract = $lstv2[0].FullName $destination = "$localPath\lstv2" Expand-Archive -Path $zipToExtract -DestinationPath $destination -Force Write-Host "Extracted $zipToExtract to $destination" $removePath = "$($localPath)\tmp" Remove-Item -Path $removePath -Force /Y } ###################################################################################### # Doing the update stuff. # for now just on the old version. ###################################################################################### Push-Location $serverPath/lstv2 npm i npm run db:migrate ###################################################################################### # Start the services and iis backup. ###################################################################################### Write-Host "Starting the services up." Write-Host "Starting $serviceApp" Start-Service -DisplayName $serviceApp Start-Sleep -Seconds 1 Write-Host "Starting $serviceLstV2" Start-Service -DisplayName $serviceLstV2 Start-Sleep -Seconds 1 if($token -eq "usday1"){ Write-Host "Starting $($serviceOcme)" Start-Service -DisplayName $serviceOcme } Write-Host "Starting iis application" Start-WebAppPool -Name LogisticsSupportTool } Invoke-Command -ComputerName $server -ScriptBlock $plantFunness -ArgumentList $server, $token, $location, $buildZip, $buildLoc -Credential $credentials