Functions/Invoke-DeploymentShow.ps1

function Invoke-DeploymentShow {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        [string]$App,
        [Parameter(Mandatory = $true)]
        [string]$AppExe,
        [Parameter(Mandatory = $true)]
        [string]$Verzija,
        [Parameter(Mandatory = $true)]
        [ValidateScript( {Test-Path $_ })]
        [System.IO.FileInfo]$DestinationFolder,
        [Parameter(Mandatory = $true)]
        [string]$Citrix,
        [Parameter(Mandatory = $true)]
        [ValidateScript( {Test-Path $_ })]
        [System.IO.FileInfo]$DeploymentLogFolder,
        [Parameter(Mandatory = $true)]
        [ValidateScript( {Test-Path $_ })]
        [System.IO.FileInfo]$SourceFolder,
        [Parameter(Mandatory = $true)]
        [ValidateSet("All", "OnlyExe")]
        [string]$DeploymentType,
        [string]$CurrentUser = $env:USERNAME,
        [string]$LogSQLInstance = "DENU01MS0068",
        [string]$LogDB = "RS10DEPLOYMENT",
        [bool]$Done = $true
    )

    #dodatni parametri


    BEGIN {
        try {
            Write-LogApp INFO "Deployment show pocinje" $App
            Write-Verbose -Message "Proveravam da li ima fajlova na destinaciji" -Verbose
            switch ($DeploymentType) {
                "All" {
                    $OldFiles = Get-ChildItem -Path $DestinationFolder -ErrorAction SilentlyContinue
                    if ($OldFiles) {
                        Write-LogApp INFO "Ima fajlova na destinaciji iniciram brisanje" $App
                        $OldFiles | Remove-Item -Recurse -Force -Verbose -ErrorAction Stop
                        Write-Verbose -Message "Obrisani su svi fajlovi na destinaciji" -Verbose
                        Write-LogApp SUCCESS "Obrisani su svi fajlovi na destinaciji" $App
                    }
                }
                "OnlyExe" {
                    $OldFile = Get-ChildItem -Path $DestinationFolder\$AppExe -ErrorAction SilentlyContinue
                    if ($OldFile) {
                        Write-LogApp INFO "Stari exe se nalazi na destinaciji iniciram brisanje" $App
                        $OldFile | Remove-Item -Force -Verbose -ErrorAction Stop
                        Write-Verbose -Message "Stari exe obrisan na destinaciji" -Verbose
                        Write-LogApp SUCCESS "Stari exe obrisan na destinaciji" $App
                    }
                }
            }
        }
        catch {
            Write-Warning -Message "Greska prilikom brisanja fajlova na destinaciji. Error: $_" -Verbose
            Write-LogApp ERROR "Greska prilikom brisanja fajlova na destinaciji | Error: $_" $App
            $Done = $false
            Prekid
            break
        }
    }

    PROCESS {

        $DateTime = Get-Date -Format "ddMMyyy_HHmmss"
        $RobocopyLogName = "{0}_{1}_{2}_{3}_{4}{5}" -f $App, $($Verzija -replace ",|\.| ", "_"), "RobocopyLog", $Citrix, $DateTime, ".txt"
        Write-Verbose -Message "Kopiranje verzije u toku molim sacekaj" -Verbose
        Write-LogApp INFO "Startujem kopiranje | Source: $SourceFolder Destination: $DestinationFolder" $App
        switch ($DeploymentType) {
            "All" {$RBArguments = '"{0}" "{1}" /MIR /LOG:"{2}\{3}" /TEE' -f $SourceFolder, $DestinationFolder, $DeploymentLogFolder, $RobocopyLogName}
            "OnlyExe" {$RBArguments = '"{0}" "{1}" "{2}" /MIR /LOG:"{3}\{4}" /TEE' -f $SourceFolder, $DestinationFolder, $AppExe, $DeploymentLogFolder, $RobocopyLogName}
        }
        #$RBArguments
        Start-Process Robocopy.exe -ArgumentList $RBArguments -NoNewWindow -Wait
        Write-Verbose -Message "Zavrseno kopiranje verzije" -Verbose
        Write-LogApp SUCCESS "Zavrseno kopiranje verzije" $App
        try {
            $DestItem = Get-ItemProperty -Path $DestinationFolder\$AppExe -ErrorAction Stop
            $DestVerzija = ($DestItem.VersionInfo.FileVersion).Trim()
            if ($DestVerzija -eq $Verzija) {
                Write-Verbose -Message "Verzija na destinaciji je dobra" -Verbose
                Write-LogApp SUCCESS "Verzija na destinaciji je dobra" $App
            }
            else {
                Write-Warning -Message "Verzija na destinaciji $DestVerzija nije ista kao verzija u source folderu $Verzija" -Verbose
                Write-LogApp ERROR "Verzija na destinaciji $DestVerzija nije ista kao verzija u source folderu $Verzija" $App
                $Done = $false
                Prekid
                break
            }
        }
        catch {
            Write-Warning -Message "Greska u povlacenju $AppExe verzije na destinaciji $DestinationFolder | Error: $_" -Verbose
            Write-LogApp ERROR "Greska u povlacenju $AppExe verzije na destinaciji $DestinationFolder | Error: $_" $App
            $Done = $false
            Prekid
            break
        }

        #Specijalan handling za SIB fajl na URI10
        if ($App -eq "URA10") {
            if (Test-Path -Path $SourceFolder\*.sib) {
                $SibDestinationFolder = "\\sbcfile01\bms$\URA-Templates\TRANSLATE\BMS\URA10"
                Write-Verbose -Message "Imamo novi SIB fajl" -Verbose
                Write-LogApp INFO "Imamo novi SIB fajl" $App
                foreach ($Sib in (Get-ChildItem -Path $SourceFolder\*.sib)) {
                    $SibName = $Sib.Name
                    Write-Verbose -Message "Kopiranje SIB fajla $SibName u toku molim sacekaj" -Verbose
                    Write-LogApp INFO "Iniciram kopiranje SIB fajla $SibName na destinaciju $SibDestinationFolder" $App
                    $RBSibArguments = '"{0}" "{1}" "{2}" /LOG+:"{3}\{4}" /TEE' -f $SourceFolder, $SibDestinationFolder, $SibName, $DeploymentLogFolder, $RobocopyLogName
                    Start-Process Robocopy.exe -ArgumentList $RBSibArguments -NoNewWindow -Wait
                    Write-Verbose -Message "Zavrseno kopiranje SIB fajla $SibName" -Verbose
                    Write-LogApp SUCCESS "Zavrseno kopiranje SIB fajla $SibName" $App
                }
            }
            $Msg2 = [System.Windows.Forms.MessageBox]::Show("Da li zelis da otvoris Robocopy LOG?", "Potvrda", "YesNo", "Question")
            if ($Msg2 -eq "Yes") {
                if (Test-Path $DeploymentLogFolder\$RobocopyLogName) {
                    Invoke-Item -Path $DeploymentLogFolder\$RobocopyLogName
                }
            }
        }
    }

    END {
        Write-Verbose -Message "Robocopy log se nalazi na putanji $DeploymentLogFolder\$RobocopyLogName" -Verbose
        Write-Verbose -Message "Script log upisan u fajl na putanji $RunningFromFolder\Deployment_Log.txt" -Verbose
        Write-Verbose -Message "Deployment zavrsen iniciram unos logova u DeployLog bazu" -Verbose
        Write-LogApp INFO "Deployment zavrsen iniciram unos logova u DeployLog bazu" $App
        if ($Done) {
            $InsertQuery = "INSERT into dbo.DeployLog (TimeStamp,Application,Version,Environment,Administrator,LogPath,DeploymentType,SourcePath,DestinationPath)
                            VALUES (GETDATE(),
                                    '$App',
                                    '$Verzija',
                                    '$Citrix',
                                    '$CurrentUser',
                                    '$(Join-Path $DeploymentLogFolder $RobocopyLogName)',
                                    '$DeploymentType',
                                    '$SourceFolder',
                                    '$DestinationFolder')"

            try {
                Invoke-Sqlcmd -ServerInstance $LogSQLInstance -Database $LogDB -Query $InsertQuery -ErrorAction Stop
                Write-Verbose -Message "Logovi insertovani u DeployLog bazu" -Verbose
                Write-LogApp SUCCESS "Logovi insertovani u DeployLog bazu" $App
            }
            catch {
                Write-Warning -Message "Dogodila se greska prilikom unosa logova u DeployLog bazu | Error: $_" -Verbose
                Write-Warning -Message "Insert query | $InsertQuery" -Verbose
                Write-LogApp ERROR "Dogodila se greska prilikom unosa logova u DeployLog bazu | Error: $_" $App
                Write-LogApp ERROR "Insert query | $InsertQuery" $App
            }
        }
        else {

        }
        Write-Verbose -Message "Deployment show zavrsen" -Verbose
        Write-LogApp INFO "Deployment show zavrsen" $App
        Prekid
        break
    }
}