Auto-updating a script

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 7 years and 5 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
swindmiller
Posts: 64
Last visit: Tue Aug 22, 2023 11:59 am

Auto-updating a script

Post by swindmiller »

I have a HelpDesk Tool GUI. I wanted a way to have it check if there is an updated version, kill the running copy, copy down the latest and then run the new copy.

In my form load I have this (I am leaving out the part about checking the version because it works fine):
  1. if ([System.Windows.Forms.MessageBox]::Show("There is a newer version of the HelpDesk Tool available.`nWould you like to update it now?", "Updated Version Available", [System.Windows.Forms.MessageBoxButtons]::YesNo) -eq "Yes")
  2.         {
  3.             \\MyFileServer\HelpDeskTool\Updatetool.ps1
  4.         }
Then the Updatetool.ps1:
  1. Get-Process "HelpDesk Tool" |   Foreach-Object { $_.CloseMainWindow() | Out-Null } | stop-process –force
  2. sleep 1
  3. copy "\\MyFileServer\HelpDeskTool\HelpDesk Tool.exe" $env:USERPROFILE"\Desktop\"
  4. invoke-item "$env:USERPROFILE\Desktop\HelpDesk Tool.exe"
The problem with this is it seems like the Updatetool script which is called from the Main script is keeping the Main script open until it is done preventing it from killing the Main script.

I had read a few things that basically said to run the update script from the start and have it call the Main script. I can do this but was hoping to find a way to do it the way I originally wanted.....if possible.

After some searching I tried this:
  1. if ([System.Windows.Forms.MessageBox]::Show("There is a newer version of the HelpDesk Tool available.`nWould you like to update it now?", "Updated Version Available", [System.Windows.Forms.MessageBoxButtons]::YesNo) -eq "Yes")
  2.         {
  3.             start-job -filepath \\MyFileServer\HelpDeskTool\Updatetool.ps1
  4.         }
by adding the start-job and this did kill the process of the Main script (but did nothing else), I think also killed the UpdateTool script as well. Is that because it was called from the Main script which has now been killed?

What would be the best way to set something like this up?

Thanks,
Scott
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Auto-updating a script

Post by jvierra »

You can just exit from the script after you have started the new copy.
You cannot copy a file over a running exe.

Launch new copy and copy it's file over the old exe then re-launch the newly copied file.
User avatar
swindmiller
Posts: 64
Last visit: Tue Aug 22, 2023 11:59 am

Re: Auto-updating a script

Post by swindmiller »

Sorry but you lost me a bit. I understand you can't copy the exe while it's open, that seems to be where I am getting stuck. When I launch something from the original script I cannot get it to close the original before it starts trying to copy.

Do you have an example?

Sorry but this one is stumping me pretty good.

Thanks,
Scott
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Auto-updating a script

Post by jvierra »

You have to launch the external EXE and it has to have a delay before it starts the copy. Use Start-Sleep before the copy and wait long enough for the first EXE to exit.
User avatar
swindmiller
Posts: 64
Last visit: Tue Aug 22, 2023 11:59 am

Re: Auto-updating a script

Post by swindmiller »

Thanks! Is there a certain way that would be preferred to launch it from the main script.
It seems like no matter how I launch it, it seems like it runs through the whole 2nd (external) script before it lets go of the main script.
It could just be me but that's what it seems like.

Thanks for all your help!
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Auto-updating a script

Post by jvierra »

If it is not an EXE thenyou would launch it like this:

Start-Process powershell -Args '<path to new script' + any args>
exit
User avatar
swindmiller
Posts: 64
Last visit: Tue Aug 22, 2023 11:59 am

Re: Auto-updating a script

Post by swindmiller »

Worked great! Start-Process is what I was lacking, I see now I was calling a 2nd script still running from the 1st. The start-process runs separate, learning every day :D

This is what I wound up with. In my main script:
  1. if ([System.Windows.Forms.MessageBox]::Show("There is a newer version of the HelpDesk Tool available.`nWould you like to update it now?", "Updated Version Available", [System.Windows.Forms.MessageBoxButtons]::YesNo) -eq "Yes")
  2. {
  3. Start-Process powershell -Args '-ExecutionPolicy Bypass -File "\\MyFileserver\HelpDeskTool\Updatetool.ps1"'
  4. }
and the UpdateTool.ps1:
  1. Write-Output "Updating HelpDesk Tool.....please wait"
  2. Get-Process "HelpDesk Tool" |   Foreach-Object { $_.CloseMainWindow() | Out-Null } | stop-process –force
  3. Sleep 1
  4. copy "\\MyFileserver\HelpDeskTool\HelpDesk Tool.exe" $env:USERPROFILE"\Desktop\"
  5. Sleep 1
  6. invoke-item "$env:USERPROFILE\Desktop\HelpDesk Tool.exe"ool.ps1:
Thanks for all your help!!

Scott
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Auto-updating a script

Post by jvierra »

Good luck.
User avatar
dan.potter
Posts: 709
Last visit: Wed Nov 14, 2018 11:39 am

Re: Auto-updating a script

Post by dan.potter »

and why didn't we return to the powershell.org forum to get your answer? :D
User avatar
swindmiller
Posts: 64
Last visit: Tue Aug 22, 2023 11:59 am

Re: Auto-updating a script

Post by swindmiller »

Honestly because the form.close was not working for me. It seemed to keep the 1st script open and unable to kill it from the 2nd.
I then was have a hard time trying to explain what it was doing (or what I was doing wrong). I figured since I was solely using Powershell Studio I would try here.
I was planning on writing back what had worked for me once I was sure it was working and it is.
Looks like my hang up was not using start-process but was unaware earlier that was my issue :D
Learning something new everyday!
This topic is 7 years and 5 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked