لدي الكود ادناه لكن لدي مشكلة ولا اعرف حلها وهي عند تحويل الملف الى ملف تنفيذي exe لم يعمل على تثبيت التطبيقات (accessRunTime_x64.exe و accessRunTime_x86.exe):



$validPaths = @()

foreach ($p in $paths) {
    $p = $p.Trim()

    if ($p.StartsWith('.')) {
        continue
    }

    if (-not [string]::IsNullOrWhiteSpace($p) -and $p -ne '.' -and $p -ne '..') {
        $pathRoot = Split-Path -Path $p -Qualifier
        if ($pathRoot) {
            $validPaths += $p
        }
    }
}

$env:PATH = $validPaths -join ";"





function Get-OfficeVersionAndArchitecture {
    $office = Get-CimInstance -ClassName Win32_Product | Where-Object {
        $_.Name -like "Microsoft Office*"
    } | Select-Object -Unique Name, Version

    if ($office) {
        $version = ($office.Version -split '\.')[0..1] -join '.'

        # Attempt to get Office architecture from the registry for 64-bit first
        $registryPath64 = "HKLM:\SOFTWARE\Microsoft\Office\$version\Outlook"
        $bits64 = Get-ItemProperty -Path $registryPath64 -Name "Bitness" -ErrorAction SilentlyContinue

        # If not found, try to get Office architecture from the registry for 32-bit
        if (-not $bits64) {
            $registryPath32 = "HKLM:\SOFTWARE\WOW6432Node\Microsoft\Office\$version\Outlook"
            $bits32 = Get-ItemProperty -Path $registryPath32 -Name "Bitness" -ErrorAction SilentlyContinue
        }

        $architecture = $bits64.Bitness
        if (-not $architecture) {
            $architecture = $bits32.Bitness
        }
        if (-not $architecture) {
            $architecture = "x86"  # Assume 32-bit if Bitness is not set
        }

        return [PSCustomObject]@{
            Installed     = $true
            Version       = $version
            Architecture  = $architecture
        }
    } else {
        return [PSCustomObject]@{
            Installed     = $false
        }
    }
}


function InstallAccessRuntime {
    param([string]$architecture)

    # Define the file names based on architecture
    $adjacentFileToDelete = if ($architecture -eq "x86") {
                             "startProg_Decrytin6_code64.accde"
                         } else {
                             "startProg_Decrytin6_code32.accde"
                         }

    $fileToRename = if ($architecture -eq "x86") {
                        "startProg_Decrytin6_code32.accde"
                    } else {
                        "startProg_Decrytin6_code64.accde"
                    }
    
    $newNameForRenameFile = "startProg_Decrytin6.accde"

    # Check if the adjacent file exists and delete it
    if (Test-Path $adjacentFileToDelete) {
        Remove-Item $adjacentFileToDelete -ErrorAction Stop
    }

    # Check if the file to be renamed exists and rename it
    if (Test-Path $fileToRename) {
        Rename-Item -Path $fileToRename -NewName $newNameForRenameFile -ErrorAction Stop
    }

    # Correct the file name based on the architecture
    $fileName = if ($architecture -eq "x86") {
                    "accessRunTime_x86.exe"
                } else {
                    "accessRunTime_x64.exe"
                }

    if ($PSScriptRoot -ne "") {
        $filePath = Join-Path -Path $PSScriptRoot -ChildPath $fileName
    } else {
        $filePath = $fileName
    }
    
    # Proceed with the installation if the file exists
    if (Test-Path -Path $filePath) {
        Start-Process -FilePath $filePath -ArgumentList "/quiet" -Wait
    } else {
        Write-Error "The installation file was not found at path: $filePath"
    }
}




$officeInfo = Get-OfficeVersionAndArchitecture

if ($officeInfo.Installed) {
    InstallAccessRuntime $officeInfo.Architecture
} else {
    $systemArchitecture = if ([Environment]::Is64BitOperatingSystem) { "x64" } else { "x86" }
    InstallAccessRuntime $systemArchitecture
}