jeudi 17 août 2017

Powershell Code if statement failure

I have an interesting situation going on where an agent was installed and it communicates back to the server. This agent is now EOL and support with the vendor was not renewed. Many of our agents are no longer communicating back to the server because its on stuck calling back to our dmz network where the server was decommed. The agent has a tamper protection with password and the old admin doesn't know the password. What I found we can do is on these servers, install the agent again and then the appliance that is on our inside network change the password to whatever, for my case I did blank. To make things worse the agent is installed in either programfiles, program files (x86) and in some cases its installed twice on the same server.

That's the background of the script I'm trying to develop. My idea was to download the agent from the appliance, detect the architecture and run the install then run an uninstall and loop it for each programfiles and or program files(x86). I have it working ok, but its not removing the application form the x86 directory. Also, I was hoping to grab some formatting tips :).

Thanks for your help in advance.

 if ((Get-WmiObject Win32_OperatingSystem).OSArchitecture -eq "64-bit")
    {
        #Download Application
        $source = "http://heartrate0001/x64/HeartRate.exe"
        $filename = [System.IO.Path]::GetFileName($source)
        $destination = "$ENV:USERPROFILE\Desktop\$filename"
        $webclient = New-Object System.Net.WebClient
        $webclient.DownloadFile($source,$destination)
        $patha = "C:\Program Files\ICU\HeartRate.exe"
        $pathb ="C:\Program Files (x86)\ICU\HeartRate.exe"
        $Folder1Path = 'C:\Program Files\ICU\HeartRate.exe'
        $Folder2Path = 'C:\Program Files (x86)\ICU\HeartRate.exe'
    }

    #Install Application

        $process = start-process $destination -PassThru -Wait
        $process.ExitCode

        write-host $process.Exitcode

    #Uninstall
        if ((test-path -Path $Folder1Path) -eq "true") {
            {  
               $Folder1Path = "C:\Program Files\ICU\HeartRate.exe"
               $arg1 = "-uninstall"
               & $Folder1Path $Arg1
            }
            }

        if ((test-path -Path $Folder2Path) -eq "true") {
            {  
               $Folder2Path = "C:\Program Files\ICU\HeartRate.exe"
               $arg1 = "-uninstall"
               & $Folder2Path $Arg1
            }
            }



    elseif ((Get-WmiObject Win32_OperatingSystem).OSArchitecture -eq "32-bit")
    {
        #Download Application
        $source = "http://heartrate0001/HeartRate.exe"
        $destination = "$ENV:USERPROFILE\Desktop\$filename"
        $webclient = New-Object System.Net.WebClient
        $webclient.DownloadFile($source,$destination)

    #Install Application

        $process = start-process $destination -PassThru -Wait
        $process.ExitCode

        write-host $process.Exitcode

    #Uninstall
        $app = "C:\Program Files\ICU\HeartRate.exe"
        $arg1 = "-uninstall"
        & $app $Arg1
    }

Aucun commentaire:

Enregistrer un commentaire