Powershell script silently installs the software , if the software already exists then the script has to giver message that the s/w already installed

Viewed 175
        # Source file location
$source1="https://ftp.mozilla.org/pub/firefox/releases/11.0/win32/enUS/Firefox%20Setup%2011.0.exe"

$source2="https://sourceforge.net/projects/codeblocks/files/Binaries/20.03/Windows/codeblocks-20.03-setup.exe"

$source3 ="https://github.com/x64dbg/x64dbg/releases/download/snapshot/snapshot_2021-07-01_23-17.zip"

$source4 ="https://www.python.org/ftp/python/3.9.6/python-3.9.6-amd64.exe"

$source5 ="https://www.nasm.us/pub/nasm/releasebuilds/2.15.05/win64/nasm-2.15.05-installer-x64.exe"
    
    
    # Destination to save the file
    $destination1 = "C:\Users\cdac\Desktop\Shravan\Softwares\firefox.exe"
    $destination2 = "C:\Users\cdac\Desktop\Shravan\Softwares\codeblocks.exe"
    $destination3 = "C:\Users\cdac\Desktop\Shravan\softwares\xdbg.zip"
    $destination4 = "C:\Users\cdac\Desktop\Shravan\softwares\python.exe"
    $destination5 = "C:\Users\cdac\Desktop\Shravan\softwares\nasm.exe"
    
    
    Invoke-WebRequest -Uri $source1 -OutFile  $destination1   
    Invoke-WebRequest -Uri $source2 -OutFile  $destination2
    Invoke-WebRequest -Uri $source3 -OutFile  $destination3
    Invoke-WebRequest -Uri $source4 -OutFile  $destination4
    Invoke-WebRequest -Uri $source5 -OutFile  $destination5
    
    
    
    #Installing softwares one by one
    
    
    
    # Array of installer details
    [Hashtable[]]$Installers = @();
    # Firefox
    $Installers += @{
        SoftwareName = "Firefox"
        Url = "https://ftp.mozilla.org/pub/firefox/releases/11.0/win32/enUS/Firefox%20Setup%2011.0.exe"
        #Uri = "https://ftp.mozilla.org/pub/firefox/releases/11.0/win32/enUS/Firefox%20Setup%2011.0.exe"
        Destination = "C:\Users\cdac\Desktop\Shravan\Softwares\firefox.exe"
        Arguments = '/s'
    }
    #Code Blocks
    $Installers += @{
        SoftwareName = "CodeBlocks"
        Url = "https://sourceforge.net/projects/codeblocks/files/Binaries/20.03/Windows/codeblocks-20.03-setup.exe/"
       #Uri = "https://www.fosshub.com/Code-Blocks.html?dwl=codeblocks-20.03-setup.exe"
        Destination = "C:\Users\cdac\Desktop\Shravan\Softwares\codeblocks.exe"
        Arguments = '/s'
    }
    
    #X64_dbg
    $Installers += @{
        SoftwareName = "x64dbg"
        Url = "https://github.com/x64dbg/x64dbg/releases/download/snapshot/snapshot_2021-07-01_23-17.zip"
        #Uri = "https://github.com/x64dbg/x64dbg/releases/download/snapshot/snapshot_2021-07-01_23-17.zip"
        Destination = "C:\Users\cdac\Desktop\Shravan\Softwares\xdbg.zip"
        Arguments = '/s'
    }
    
    
    #Python
    $Installers += @{
        SoftwareName ="python"
        Url = "https://www.python.org/ftp/python/3.9.6/python-3.9.6-amd64.exe/"
        #Uri = "https://www.python.org/ftp/python/3.9.6/python-3.9.6-amd64.exe"
        Destination = "C:\Users\cdac\Desktop\Shravan\Softwares\python.exe"
        Arguments = '/s'
    }
    
    
    #Netwide Assembler
    $Installers += @{
        SoftwareName = "Netwide Assembler"
        Url = "https://www.nasm.us/pub/nasm/releasebuilds/2.15.05/win64/nasm-2.15.05-installer-x64.exe"
        #Uri = "https://www.nasm.us/pub/nasm/releasebuilds/2.15.05/win64/nasm-2.15.05-installer-x64.exe"
        Destination = "C:\Users\cdac\Desktop\Shravan\Softwares\nasm.exe"
        Arguments = '/s'
    }
    function Install-Software([Hashtable]$installer)
     {
        Write-Host "Installing $($installer.SoftwareName)"
        Write-Host "Invoke-WebRequest -Uri $($installer.Url) -OutFile $($installer.Destination)"
        Write-Host "Start-Process -FilePath $($installer.Destination) -ArgumentList $($installer.Arguments) -Wait"
        
        # Remove Write Host above - uncomment the following lines:
        Invoke-WebRequest -Uri $installer.Url -OutFile $installer.Destination
        Start-Process -FilePath $installer.Destination -ArgumentList $installer.Arguments -Wait
        
    }
    
    foreach($installer in $Installers)
     {
    
        Install-Software -installer $installer
    }

I have written Powershell script which downloads and installs the software. The script is working but now I have to put condition in my script that if the software is already installed the script has to give message saying the software is already installed otherwise software has to install .please tell me how to do i am new to Powershell please help me its my humble request.

Thanking you

1 Answers

Assuming you know where the executables are located when installed, the simplest solution is to include an InstalledPath entry to your hashtables, and then use the output from Test-Path to infer whether a given software is currently installed or not.

Given that you've already added the download URL (Url) and download path (Destination) as entries to your installer hashtables, you don't need the code above the [Hashtable[]]$Installers = @() line; instead, incorporate the Invoke-WebRequest in the foreach loop, so you only perform the download if actually needed.

[Hashtable[]]$Installers = @();

# Firefox
$Installers += @{
        SoftwareName = "Firefox"
        Url = "https://ftp.mozilla.org/pub/firefox/releases/11.0/win32/enUS/Firefox%20Setup%2011.0.exe"
        #Uri = "https://ftp.mozilla.org/pub/firefox/releases/11.0/win32/enUS/Firefox%20Setup%2011.0.exe"
        Destination = "C:\Users\cdac\Desktop\Shravan\Softwares\firefox.exe"
        Arguments = '/s'
        # EXAMPLE PATH - replace it with the software-appropriate path
        InstalledPath = 'C:\Program Files\FireFox\firefox.exe'
}

# ...

foreach($installer in $Installers) {
  if (Test-Path -LiteralPath $installer.InstalledPath) {
    Write-Verbose -Verbose "$($installer.SoftwareName) is already installed."
  }
  else {
    # Download...
    Invoke-WebRequest -ErrorAction Stop -Uri $installer.Url -OutFile  $installer.Destination
    # ... and install.
    Install-Software -installer $installer
  }
}
Related