Easy way to install software remotely using PowerShell (2021)

Installing software remotely using powershell

Today I will discuss how to install software remotely using PowerShell. I was assigned a task a few days back to install Opsview on 500+ servers, I am not sure why my manager is after me sigh…but the script which I will share a script will help you in a billion ways.

Installing software using Msiexec

Before we proceed we need to understand Msiexec briefly and what is Msiexec.

Msiexec allows you to install, modify, and run Windows Installer commands from the command line.

Syntax

				
					msiexec.exe [/i][/a][/j{u|m|/g|/t}][/x] <path_to_package>
				
			

Parameters

Credits: Microsoft

ParameterDescription
/iSpecifies normal installation.
/aSpecifies administrative installation.
/juAdvertise the product to the current user.
/jmAdvertise the product to all users.
/j/gSpecifies the language identifier used by the advertised package.
/j/tApplies transform to the advertised package.
/xUninstalls the package.
<path_to_package>Specify the location and name of the installation package file.


Read more on Microsoft Website.

PowerShell script to install software on remote servers.

Let’s understand step-by-step how I created the script to install the software remotely.

Step #1

				
					$servers = gc C:\users\admin\desktop\server.txt ## providing the list of servers##
$source = "\\TEB-TS2\OpsView_Agent" ### share the folder in which the installation file is present ##
$tempath ="\\TEB-TS2\OpsView_Agent" ##temporary path ##
$destination = "c$" ## destination where the file will copy##
				
			

Step #2

In case you are still wondering how the for-each loop work visit the link –> https://powershellguru.com/powershell-for-loop/

				
					foreach ($server in $servers)
{
$server
$session = New-PSSession -ComputerName $server ##create a temporary session on remote server##
if(test-connection -Cn $server -quiet) ## check the connectivity of the servers ##
{
Copy-Item $source -Destination \\$server\$destination -Recurse -Force ##copies the folder with the software to the remote server.
				
			

Step #3

Invoke-Command usually creates a temporary session on the remote server to execute the commands mentioned in the script block.

Start-sleep -seconds 120, the script will pause for 120 seconds and let the installation runs in the background and complete.

Start-service -Name “service name” give the service name to start the service if it is required.

Get-Service -Name “Service name” fetch the status of the service on the remote server.

				
					if(Test-Path -path $tempath) ##check the tempath on the remote servers
{
Invoke-command -session $session -ScriptBlock {Msiexec /i C:\OpsView_Agent\Opsview_Windows_Agent_x64_11-03-21-1343.msi /quiet /qn /norestart
start-sleep -Seconds 120
Start-Service -Name "OpsviewAgent"
get-service -Name "OpsviewAgent"
} 
Write-Host -ForeGroundColor Green "Installation successful on $server"
}
}
else
{
Write-Host -ForeGroundColor red "Installation failed on $server"
}
}
				
			

Conclusion

I hope you have liked this post and will implement this whenever it is required to install software on multiple remote servers. Things you should always remember is to use only 1 server for testing purposes and the rest you can try to install if everything is working fine. Let me know if you want a blog post on some other script that might amaze you.

We are working continuously to provide you with the better and the best scripts daily. We will publish weekly hence don’t forget to subscribe to our newsletter.

Leave a Reply

Please disable your adblocker or whitelist this site!