Um ein EXE-File gegen eine neuere Version auszutauschen, muss evtl auch ein laufender Prozess gestoppt werden. Folgendes Script kümmert sich darum…

param
	(
	[string]$SrcPfad,		# Pfad auf dem zentralen Filer "\\FileServer\Share\Bla\test.exe"
	[string]$DstPfad,		# Pfad auf dem einzelnen Server "c:\programfiles\bla\test.exe"
	[switch]$Debug,			# Logausgabe auch auf Desktop
	[switch]$ForceUpdate	        # Immer updaten
	)
$Error.Clear()
if ( -not $PSScriptRoot ) { $PSScriptRoot = Split-Path -Parent -Path $MyInvocation.MyCommand.Definition }

#--------------------------------------------------------------------------------------------------
# Write LOG
#--------------------------------------------------------------------------------------------------
function write-log
	{
	param
		(
		[string]$String,
		[switch]$SizeCheck
 		)
	$LogHistory=5
	$ErrCount = $Error.count
	$LogFile = Join-Path -Path $PSScriptRoot -ChildPath "LogFile.log"
	if ( $SizeCheck )
		{
		$LF = Get-Item -Path $LogFile -ErrorAction silentlycontinue
		if ( $LF.Length -gt 1024KB)
			{
			if ( $LogHistory )
				{
				for($i=$LogHistory - 1; $i -ge 1; $i--)
					{
					if ( test-path $($LF.FullName + "." + $i.tostring() ) )
						{
						Move-Item -Path $($LF.FullName + "." + $i.tostring() ) -Destination $($LF.FullName + "." + $($i + 1).tostring() ) -force
						}
					}
				}
			Move-Item -Path $LF.FullName -Destination $($LF.FullName + ".1" ) -force
			write-log "Moved LogFile $LogFile to $($LF.FullName + ".1" ) because of size: $($LF.Length)"
			}
		}
	$Prefix = "`($($myInvocation.ScriptName.Split('\')[-1])`[$($MyInvocation.ScriptLineNumber)`]`)"
	
	$TMP = get-date -Format G
	$TMP += " $($Prefix): "
	$TMP += $String
	if ( $Debug )
		{ $TMP }
	$TMP | Out-File -append -encoding UTF8 -FilePath $LogFile -ErrorAction silentlycontinue
	if ($ErrCount -eq 0)
		{ $Error.Clear() }
	}
#--------------------------------------------------------------------------------------------------

Write-Log "---Start Script---" -SizeCheck

if ( -not ( test-path $DstPfad ) )
	{
	Write-Log "Error: File $DstPfad not found!"
	exit
	}

if ( -not ( test-path $SrcPfad ) )
	{
	Write-Log "Error: File $SrcPfad not found!"
	exit
	}

Write-Log "Calculating Hashes for Source and Destination."
$md5 = New-Object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider
$DstHash = [System.BitConverter]::ToString($md5.ComputeHash([System.IO.File]::ReadAllBytes($DstPfad)))
$SrcHash = [System.BitConverter]::ToString($md5.ComputeHash([System.IO.File]::ReadAllBytes($SrcPfad)))


if ( $DstHash -eq $SrcHash -and -not $ForceUpdate)
	{
	Write-Log "Source and Destination is equal. Nothing to do..."
Write-Log "--- Job done ---"
	exit
	}

Write-Log "Get Running Process of $DstPfad"
$P = Get-Process | where { $_.Path -eq $DstPfad }

if ( -not $P )
	{
	Write-Log "No Process Found."
	}
else
	{
	Write-Log "Stop Process of $DstPfad"
	Stop-Process -Id $P.Id
	}

$P = Get-Process | where { $_.Path -eq $DstPfad }
if ( $P )
	{
	Write-Log "Cant Stop Process! Exit Script."
	exit
	}

Write-Log "Copy File $SrcPfad to $DstPfad"
Copy-Item $SrcPfad -Destination $DstPfad -Force

if ( $Error )
	{
	Write-Log "A Error occurred. Exit"
	exit
	}

Write-Log "Start Process $DstPfad."
Start-Process $DstPfad

Write-Log "--- Job done ---"

 

EXE-Files tauschen

Das könnte dir auch gefallen

Schreibe einen Kommentar