Pages

Thursday, 2 February 2023

Archi Install script- PS

 #AUARC002020ENGC1 - Autodesk AutoCAD Architecture 2020 - English (8.2.47.0)


$errorActionPreference = "SilentlyContinue"

If ($ENV:PROCESSOR_ARCHITEW6432 -eq "AMD64") {

    Try {

        &"$ENV:WINDIR\SysNative\WindowsPowershell\v1.0\PowerShell.exe" -ExecutionPolicy bypass -NoProfile -File $PSCOMMANDPATH

    }

    Catch {

        Throw "Failed to start $PSCOMMANDPATH"

    }

    Exit

}

#App Details

$str_App = 'AUARC002020ENGC1 - Autodesk AutoCAD Architecture 2020 - English (8.2.47.0)'

$str_PackageCode = 'AUARC002020ENGC1'

#Create Log folder

$Str_ProgramData = $env:ProgramData

$Str_LogFolder = "$Str_ProgramData\IntuneWin32Apps"

if(!(Test-Path -Path $Str_LogFolder )){

  New-Item -ItemType directory -Path $Str_LogFolder

}

$str_LogFile = "$Str_LogFolder\IntuneWin32Apps.log"

#Write App

$str_LogEntry = "$(Get-Date) ; $str_App - Create Scheduled Task to Install App"

Add-Content $str_LogFile $str_LogEntry

#Write Version

$str_LogEntry = "$(Get-Date) ; Version 1"

Add-Content $str_LogFile $str_LogEntry

#Write Start to Log File

$str_LogEntry = "$(Get-Date) ; Install Starting"

Add-Content $str_LogFile $str_LogEntry

#Create Temp Install Folder

$TempInstallFolder = -join ("c:\programdata\", $str_PackageCode, "_Install")

if(!(Test-Path -Path $TempInstallFolder )){

  New-Item -ItemType directory -Path $TempInstallFolder

}

New-Item -Path $TempInstallFolder -ItemType Directory -Force -Confirm:$false | out-null

$str_LogEntry = "$(Get-Date) ; Folder created: $TempInstallFolder"

Add-Content $str_LogFile $str_LogEntry

#Copy Img Folder

$currentfolder = split-path $script:MyInvocation.MyCommand.Path



Copy-Item -path "$currentfolder\Img" -Destination $TempInstallFolder -Recurse -Force

$str_LogEntry = "$(Get-Date) ; Img folder copied to $TempInstallFolder"

Add-Content $str_LogFile $str_LogEntry

#Copy Licensing Script

$currentfolder = split-path $script:MyInvocation.MyCommand.Path

Copy-Item -path "$currentfolder\Set_autodesknlm_license_server.vbs" -Destination $TempInstallFolder -Recurse -Force

$str_LogEntry = "$(Get-Date) ; Set_autodesknlm_license_server.vbs file copied to $TempInstallFolder"

Add-Content $str_LogFile $str_LogEntry

#Create Install Script

$install =

@'

$errorActionPreference = "SilentlyContinue"



$str_App = 'AUARC002020ENGC1 - Autodesk AutoCAD Architecture 2020 - English (8.2.47.0)'

$str_PackageCode = 'AUARC002020ENGC1'

$str_LogFile = "C:\ProgramData\IntuneWin32Apps\IntuneWin32Apps.log"

#Write App

$str_LogEntry = "$(Get-Date) ; $str_App - Install"

Add-Content $str_LogFile $str_LogEntry

#Write Version

$str_LogEntry = "$(Get-Date) ; Version 1"

Add-Content $str_LogFile $str_LogEntry

#Write Start to Log File

$str_LogEntry = "$(Get-Date) ; Install Starting"

Add-Content $str_LogFile $str_LogEntry

#Uninstall Previous Versions

$Array = @(

    [pscustomobject]@{MSIGUID='{28B89EEF-0004-0000-3102-CF3F3A09B77D}';APPNAME='AutoCAD Architecture 2017'}

    [pscustomobject]@{MSIGUID='{28B89EEF-0004-0000-0102-CF3F3A09B77D}';APPNAME='AutoCAD Architecture 2017 Core'}

    [pscustomobject]@{MSIGUID='{28B89EEF-0004-0000-4102-CF3F3A09B77D}';APPNAME='AutoCAD Architecture 2017 Shared'}

    [pscustomobject]@{MSIGUID='{28B89EEF-0001-0000-0102-CF3F3A09B77D}';APPNAME='AutoCAD 2017'}

    [pscustomobject]@{MSIGUID='{28B89EEF-0001-0409-1102-CF3F3A09B77D}';APPNAME='AutoCAD 2017 Language Pack - English'}

    [pscustomobject]@{MSIGUID='{28B89EEF-0004-0000-5102-CF3F3A09B77D}';APPNAME='ACA & MEP 2017 Object Enabler'}

    [pscustomobject]@{MSIGUID='{28B89EEF-0004-0409-1102-CF3F3A09B77D}';APPNAME='AutoCAD Architecture 2017 Language Core - English'}

    [pscustomobject]@{MSIGUID='{28B89EEF-0004-0409-2102-CF3F3A09B77D}';APPNAME='AutoCAD Architecture 2017 - English'}

    [pscustomobject]@{MSIGUID='{28B89EEF-0004-0409-4102-CF3F3A09B77D}';APPNAME='AutoCAD Architecture 2017 Language Shared - English'}

)

#Loop Through Array

For ($num = 0 ; $num -le $Array.count-1 ; $num++) {

    $AppName = $Array[$num].APPNAME

    $MSIGUID = $Array[$num].MSIGUID

    $UninstallExe = 'MsiExec.exe'

    $Arguments = "/X $MSIGUID REBOOT=ReallySuppress MSIRESTARTMANAGERCONTROL=Disable /QN"

       $process = (start-process -filepath $UninstallExe -argumentlist $Arguments -wait -PassThru)

    $exitcode = $process.ExitCode

    #Check if another MSI install already in progress

    While($exitcode -eq '1618'){

        $str_LogEntry = "$(Get-Date) ; Exit Code: $exitcode, MSI install in progress - wait 10 seconds and reinstall"

        Add-Content $str_LogFile $str_LogEntry

        #Wait patiently

        Start-Sleep -Seconds 10

        #Update regCheck

        $process = (start-process -filepath $UninstallExe -argumentlist $Arguments -wait -PassThru)

        $exitcode = $process.ExitCode

    }

    If($exitcode -eq '1605') {

        $str_LogEntry = "$(Get-Date) ; $AppName not installed"

        Add-Content $str_LogFile $str_LogEntry

    } Else{

        $str_LogEntry = "$(Get-Date) ; Uninstalling $AppName"

        Add-Content $str_LogFile $str_LogEntry

        $str_LogEntry = "$(Get-Date) ; Uninstall Command: $UninstallExe $Arguments"

        Add-Content $str_LogFile $str_LogEntry

                $str_LogEntry = "$(Get-Date) ; Exit Code: $exitcode"

        Add-Content $str_LogFile $str_LogEntry

    }

}

#Delete old install folder if it exists

$Str_INSTALLDIR = "c:\Program Files\Autodesk\AutoCAD 2017"

If(Test-Path -Path $Str_INSTALLDIR ){

    Remove-Item $Str_INSTALLDIR -Recurse -Force

    $str_LogEntry = "$(Get-Date) ; INSTALLDIR Deleted - $Str_INSTALLDIR"

    Add-Content $str_LogFile $str_LogEntry

}

#Install App

$str_InstallCommand = 'c:\programdata\AUARC002020ENGC1_Install\Img\Setup.exe'

$str_Arguments = "/w /q /l ""c:\programdata\AUARC002020ENGC1_Install\Img\AUARC002020ENGC1.ini"" /language en-us"



$str_LogEntry = "$(Get-Date) ; Installing Autodesk AutoCAD Architecture 2020"

Add-Content $str_LogFile $str_LogEntry

$str_LogEntry = "$(Get-Date) ; Install Command: $str_InstallCommand $str_Arguments"

Add-Content $str_LogFile $str_LogEntry

$process = (start-process -filepath $str_InstallCommand -argumentlist $str_Arguments -wait -PassThru)

$exitcode_app = $process.ExitCode

Check if another MSI install already in progress

While($exitcode_app -eq '1618')

{

    $str_LogEntry = "$(Get-Date) ; Exit Code: $exitcode, MSI install in progress - wait 10 seconds and reinstall"

    Add-Content $str_LogFile $str_LogEntry



    #Wait patiently

    Start-Sleep -Seconds 10



    #Update regCheck

    $process = (start-process -filepath $str_InstallCommand -argumentlist $str_Arguments -wait -PassThru)

    $exitcode_app = $process.ExitCode

}



$str_LogEntry = "$(Get-Date) ; Exit Code: $exitcode_app"

Add-Content $str_LogFile $str_LogEntry

#Configure the licensing server

$str_InstallCommand = """C:\ProgramData\AUARC002020ENGC1_Install\Set_autodesknlm_license_server.vbs"""

$str_Arguments = "RUN"

$str_LogEntry = "$(Get-Date) ; Configure the Licensing Server"

Add-Content $str_LogFile $str_LogEntry

$str_LogEntry = "$(Get-Date) ; $str_InstallCommand $str_Arguments"

Add-Content $str_LogFile $str_LogEntry

$process = (start-process -filepath $str_InstallCommand -argumentlist $str_Arguments -wait -PassThru)

$exitcode_license = $process.ExitCode

$str_LogEntry = "$(Get-Date) ; Exit Code: $exitcode_license"

Add-Content $str_LogFile $str_LogEntry

$str_InstallCommand = """c:\Program Files (x86)\Common Files\Autodesk Shared\AdskLicensing\Current\helper\AdskLicensingInstHelper.exe"""

$str_Arguments = 'change --pk "185L1" --pv "2020.0.0.F" --lm "NETWORK" --ls """" --lt """"'                                

$str_LogEntry = "$(Get-Date) ; $str_InstallCommand $str_Arguments"

Add-Content $str_LogFile $str_LogEntry

$process = start-process -filepath $str_InstallCommand -argumentlist $str_Arguments -wait -PassThru

$exitcode_helper = $process.ExitCode

$str_LogEntry = "$(Get-Date) ; Exit Code: $exitcode_helper"

Add-Content $str_LogFile $str_LogEntry

#Delete Scheduled Task

If((($exitcode_app -eq '0') -OR ($exitcode_app -eq '3010')) -AND ($exitcode_license -eq '0') -AND ($exitcode_helper -eq '0'))

{

    #Install Successful

    $str_LogEntry = "$(Get-Date) ; Install Successful"

    Add-Content $str_LogFile $str_LogEntry

    $str_LogEntry = "$(Get-Date) ; Scheduled Task Deleted: Install $str_PackageCode"

    Add-Content $str_LogFile $str_LogEntry

    #Delete Desktop Shortcut

    $Str_PUBLIC = $env:PUBLIC

    Remove-Item -Path "$Str_PUBLIC\Desktop\AutoCAD Architecture 2020*.lnk" -Force -Recurse

    $str_LogEntry = "$(Get-Date) ; Desktop Shortcut deleted"

    Add-Content $str_LogFile $str_LogEntry

    #Delete Startmenu shortcut

    Remove-Item -Path "C:\programdata\Microsoft\Windows\Start Menu\Programs\Autodesk\Uninstall Tool.lnk" -Force

    $str_LogEntry = "$(Get-Date) ; Startmenu shortcut deleted - C:\programdata\Microsoft\Windows\Start Menu\Programs\Autodesk\Uninstall Tool.lnk"

    Add-Content $str_LogFile $str_LogEntry

    #Deleting Temp Install Folder

    $TempInstallFolder = -join ("c:\programdata\", $str_PackageCode, "_Install")

    Remove-Item "$TempInstallFolder\Img" -Recurse -Force

    $str_LogEntry = "$(Get-Date) ; Temp Install Folder Deleted: $TempInstallFolder\Img"

    Add-Content $str_LogFile $str_LogEntry

    #Deleting Detection File

    $TempInstallFolder = -join ("c:\programdata\", $str_PackageCode, "_Install")

    Remove-Item "$TempInstallFolder\$str_PackageCode - Detection.txt" -Recurse -Force



    $str_LogEntry = "$(Get-Date) ; Detection File Deleted: $TempInstallFolder\$str_PackageCode - Detection.txt"

    Add-Content $str_LogFile $str_LogEntry

    #Install Completed

    $str_LogEntry = "$(Get-Date) ; Install Completed"

    Add-Content $str_LogFile $str_LogEntry

    #Close Log File

    $str_LogEntry = "-----------------------"

    Add-Content $str_LogFile $str_LogEntry

    Unregister-ScheduledTask -TaskName "Install $str_PackageCode" -Confirm:$false

    Start-Sleep -Seconds 5

}Else{

    #Install Completed

    $str_LogEntry = "$(Get-Date) ; Install Failed - will try again on next reboot"

    Add-Content $str_LogFile $str_LogEntry

    #Close Log File

    $str_LogEntry = "-----------------------"

    Add-Content $str_LogFile $str_LogEntry

}

'@

$InstallFile = "$TempInstallFolder\$str_PackageCode - Install.ps1"

Out-File -FilePath $InstallFile -Encoding unicode -Force -InputObject $install -Confirm:$false

$str_LogEntry = "$(Get-Date) ; Install Script created: $InstallFile"

Add-Content $str_LogFile $str_LogEntry

#Create Scheduled Task

Stop-ScheduledTask -TaskName "Install $str_PackageCode"

Start-Sleep -Seconds 5

Unregister-ScheduledTask -TaskName "Install $str_PackageCode" -Confirm:$false

Start-Sleep -Seconds 5

$action = New-ScheduledTaskAction -Execute 'PowerShell.exe' -Argument "-ex bypass -file ""$InstallFile"""

$STPrin = New-ScheduledTaskPrincipal -UserId "SYSTEM" -LogonType ServiceAccount  -RunLevel Highest

$STTrigger = New-ScheduledTaskTrigger -AtLogon

$STSet = New-ScheduledTaskSettingsSet -RunOnlyIfNetworkAvailable -AllowStartIfOnBatteries

Register-ScheduledTask -Action $action -TaskName "Install $str_PackageCode" -Description "Install $str_PackageCode" -Principal $STPrin -Settings $STSet -Trigger $STTrigger

Start-Sleep -Seconds 5

$str_LogEntry = "$(Get-Date) ; Scheduled Task created: Install $str_PackageCode"

Add-Content $str_LogFile $str_LogEntry

#Create Detection File

$DetectionFile = "$TempInstallFolder\$str_PackageCode - Detection.txt"

New-Item $DetectionFile

$str_LogEntry = "$(Get-Date) ; Creating Detection File: $DetectionFile"

Add-Content $str_LogFile $str_LogEntry

#Close Log File

$str_LogEntry = "-----------------------"

Add-Content $str_LogFile $str_LogEntry

No comments:

Post a Comment

User based detection

function getloggedindetails () {     ##Find logged in username     $user = Get-WmiObject Win32_Process - Filter "Name='explorer...

Search This Blog