1

I was wondering if anyone knew of a way to have a powershell script check for updates to itself prior to running.

I have a script that I am going to be dispatching out to multiple computers, and don't want to have to redeploy it to each computer each time a change in the script is made. I would like to have it check a certain location to see if there is a newer version of itself (and update itself if needed).

I can't seem to think of a way to do it. Please let me know if anyone can help out. Thanks.

CodingRiot
  • 197
  • 1
  • 4
  • 16

2 Answers2

3

Well, one way maybe to create a simple batch file that runs your actual script and the first line in that batch file may be to check for existence of a ps1 in your update folder. If there is one, it can copy it down first, and then start your powershell script

Eg. whenever there is an update, you put your 'Mypowershellscript.ps1' script in c:\temp\update\ folder

and let's assume your script will be running from

c:\temp\myscriptfolder\

then you can create batch file like this

if NOT exist C:\temp\update\mypowershelscript.ps1 goto :end

copy /Y c:\temp\update\MyPowerShellScript.ps1 c:\temp\MyScriptFolder\

:END

%systemroot%\System32\WindowsPowerShell\v1.0\powershell.exe -nologo -noprofile -file "c:\temp\myscriptfolder\mypowershellscript.ps1"
zdan
  • 28,667
  • 7
  • 60
  • 71
Adil Hindistan
  • 6,351
  • 4
  • 25
  • 28
  • Is there a way that you know of to check online (in a specific url such as - "www.testserver.com/updates/powershellprogram.ps1") and if the update file exists, then download it to the directory? Or would it only be available through a local network resource – CodingRiot Apr 03 '13 at 21:06
  • If that's the case, I would probably use Powershell instead of batch, which would be fine for local and network file checking but not so suitable for checking existence of a file over the internet "unless" you will use a helper tool like 'wget' for windows. You might want to take a look at here [link](http://stackoverflow.com/questions/4619088/windows-batch-file-file-download-from-a-url) – Adil Hindistan Apr 03 '13 at 21:25
0

Here's a function I put together. Pass it the path of the file that might hold a newer release. This will update itself and then re-run with any arguments handed to the original script. Do this early in the process, other function results will be lost. I typically check the network is up and I can see the share holding the newer file, then run this:

 function Update-Myself
{
    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory = $true,
                   Position = 0)]
        [string]$SourcePath
    )
    #Check that the file we're comparing against exists
    if (Test-Path $SourcePath)
    {
    #The path of THIS script
    $CurrentScript = $MyInvocation.ScriptName
        if (!($SourcePath -eq $CurrentScript ))
        {
            if ($(Get-Item $SourcePath).LastWriteTimeUtc -gt $(Get-Item $CurrentScript ).LastWriteTimeUtc)
            {
                write-host "Updating..."
                Copy-Item $SourcePath $CurrentScript 
                #If the script was updated, run it with orginal parameters
                &$CurrentScript $script:args
                exit
            }
        }
    }
    write-host "No update required"
}

Update-Myself "\\path\to\newest\release\of\file.ps1"
Fred B
  • 175
  • 1
  • 3
  • 11