region: User Defined Functions
function RunCmd_AND_Log {
Param(

[Parameter(mandatory=$true)]
[String]$cmdString,
[Parameter(mandatory=$true)]
[String]$logFile,
[Parameter(mandatory=$false)]
[String]$params,
[Parameter(mandatory=$false)]
[String]$outputFile = ""
)
if($outputFile -ne ""){
    #Run Input Command
    Start-Process -filepath $cmdString -ArgumentList $params -NoNewWindow -RedirectStandardOutput $outputFile -Wait
    }

else{
    Start-Process -filepath $cmdString -ArgumentList $params -NoNewWindow -Wait 
    }
#Depending on output, Log if cmd Succeeded or failed
switch($?)
    {
        $true{Add-Content $logFile $($cmdString+ $params +" Ran.")}
        $false{Add-Content $logFile $($cmdstring +$params +" Failed to Run.")}
    }


}
#endregion: User Defined Functions

set-location c:\yourfolder\yoursubfolder

#region: Setup

#Create Log File 
$EZDLogFile = New-Item -ItemType file -Path $(".\FullEZDInstall_$(get-date -f yyyy-MM-dd_hhmmss).log")

#Test if posapp is running, if so quit install
$posapp = Get-Process -name "posapp" -ErrorAction SilentlyContinue 
if($posapp){
    Add-Content $EZDLogFile "posapp is still running! Install Terminated."
    exit
}

#first test for smu.exe, without it we cannot proceed with anything
if((Test-Path -path .\smu.exe)-eq $false){
    Add-Content $EZDLogFile "Folder does not contain smu.exe! Install Terminated."
    exit
    }

#Need to check if plugin is already installed
c:\yourfolder\yoursubfolder\smu.exe /cmd plgdir. | out-file .\plugindir.txt -force
$EasyDLPlugin = select-string -path .\plugindir.txt -pattern "EasyDL_2_0.plugin"
if($EasyDLPlugin){
    add-content $EZDLogFile "EasyDL Plugin Already Installed! Install Terminated."
    exit
}

$missingSerialFilePath = ".\MissingSerial.txt"
    
# Check to see if missing serial file exists if so delete
if((Test-Path $missingSerialFilePath)){
    Remove-Item  -Path $missingSerialFilePath
}

#endregion: Setup

#region: Check Firmware


do{
$reRun = $false

#get rev info
If (Test-Path C:\yourfolder\yoursubfolder\rev.txt) {
    Remove-Item C:\yourfolder\yoursubfolder\rev.txt -force
    }

C:\yourfolder\yoursubfolder\smu.exe /cmd revinf. | out-file C:\yourfolder\yoursubfolder\rev.txt -Force

$DeviceNotFound = Select-String -path .\rev.txt -Pattern "No Device Found"

if($DeviceNotFound){
    Add-Content $EZDLogFile "Scanner Not Found! Install Terminated!"
    exit
}

$serial = (gc c:\yourfolder\yoursubfolder\rev.txt)[10]
$ser = $serial.Substring(15)
$firmware = (gc c:\yourfolder\yoursubfolder\rev.txt)[6]
$fw = $firmware.Substring(22)


#if firmware not upgraded, run upgrade and rerun code
If ($fw -ne " CB000107BAA" ) {
   
   Add-Content $EZDLogFile "Firmware needs to be upgraded.."
    
        if(test-path -path .\ CB000107BAA.moc)
        {
            c:\posapps\honeywell\smu.exe /upg CB000107BAA.moc | out-file C:\yourfolder\yoursubfolder\fwlog.txt -Force
            Add-Content $EZDLogFile "Firmware Upgraded."
            $reRun -eq $true
        }
        
        else
        {
            Add-Content $EZDLogFile "Firmware Upgrade File Not Found! Install Terminated."
            Exit
        }

    
    }
    else{Add-Content $EZDLogFile "Firmware Version CB000107BAA Confirmed"}
    

}until($reRun -eq $false)

#endregion: Firmware


#region : EZDL Install 

#Check to make sure all required files exists
$EasyDlExists = Test-Path -Path ".\EasyDL.moc"
$ssLKExists = test-path -path ".\ss_lk.txt"
$EZDLConfigExists = test-path ".\EZDL_GS1Parse_Config.exm"

#Search for Serial in license file
$SerialFound = Select-String -Path c:\yourfolder\yoursubfolder\ss_lk.txt -Pattern $ser -quiet

if($EasyDlExists -eq $false){
    Add-Content $EZDLogFile "EasyDL.moc Missing." 
}

if($ssLKExists -eq $false){
    Add-Content $EZDLogFile "ss_lk.txt Missing." 
}

if($EZDLConfigExists -eq $false){
    Add-Content $EZDLogFile "EZDL_GS1Parse_Config.exm Missing."
}

if($SerialFound -eq $false){
    
    Add-Content $EZDLogFile "Serial Not Found in ss_lk.txt."
    #add missing serial number to text file
    Add-Content $missingSerialFilePath $ser
}

$ExePath = "c:\yourfolder\yoursubfolder\smu.exe"

#if serial is found and all reqiured files exist, proceed
If ($SerialFound -and ($EasyDlExists -and $ssLKExists  -and $EZDLConfigExists)) {
    
    Add-Content $EZDLogFile "Starting EZDL Install..."

        #install ezdl
    RunCmd_AND_Log -cmdString $ExePath -params "/upg EasyDL.moc" -logFile $EZDLogFile 

    #license ezdl 
    RunCmd_AND_Log -cmdString $ExePath -params "/manifest ss_lk.txt" -logFile $EZDLogFile

    #reset scanner
    RunCmd_AND_Log -cmdString $ExePath -params "/cmd reset_." -logFile $EZDLogFile

    #send config 
    RunCmd_AND_Log -cmdString $ExePath -params "/cmd 9902A0022[remainderofserialnumber]." -logFile $EZDLogFile
    #                                                9902A0022[remainderofserialnumber].
    #send exm settings
    RunCmd_AND_Log -cmdString $ExePath -params "/exmset EZDL_GS1Parse_Config.exm" -logFile $EZDLogFile

    #verify plugin
    RunCmd_AND_Log -cmdString $ExePath -params "/cmd plginf."  -logFile $EZDLogFile -outputFile ".\license.txt"

    Add-Content $EZDLogFile "EZDL Install Complete." 

    }

    Else {
        #Add-Content $EZDLogFile "Plugin Not Done;Only exm applied."

        #send exm settings
        RunCmd_AND_Log -cmdString $ExePath -params "/exmset C:\yourfolder\yoursubfolder\EZDL_GS1Parse_Config.exm" -logFile $EZDLogFile
        #& C:\yourfolder\yoursubfolder\smu.exe /exmset C:\yourfolder\yoursubfolder\EZDL_GS1Parse_Config.exm

        }

    
#endregion : EZDL Install
