[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

PowerShell - Remote Software Install

Posted on 2013-12-17
10
Medium Priority
?
6,588 Views
Last Modified: 2013-12-31
Hello,

Here is what I have to do manually every time. Log into every computer and connect to a "Share" on my network that has a shortcut to run this "MSI" file.

1. I log in and I run the MSI file
2. log off and let the user log back in to run the program

I want to automate this process using PowerShell.  

here is the location to the file - "\\10.0.2.10\Production\TBA Client\TBA client Update.msi"
I have my computer names in a txt file. So I can use "Get-Content" command.

HELPPP!!!
0
Comment
Question by:learn2earn
  • 6
  • 4
10 Comments
 
LVL 41

Expert Comment

by:footech
ID: 39727579
I've been able to do this with PowerShell Remoting (has to be enabled on all the target computers).
Steps are:
- create new pssession for target machine
- If needed by the installation process, copy the file(s) to the target machine via UNC path
- Run the .MSI file.
- You may want/need to perform some cleanup of files, and/or unneeded pssessions.
Here's the core code that would actually run the .MSI.
$computers = Get-Content computers.txt
foreach ($comp in $computers)
{
   $deploy = { & msiexec.exe /I installwizard.msi /quiet }
   $session = New-PSSession -ComputerName $comp
   Invoke-Command -Session $session -ScriptBlock $deploy
}

Open in new window

0
 
LVL 2

Author Comment

by:learn2earn
ID: 39727722
Thanks for the info,

Now will the "Invoke-Command work on a ShortCut to the MSI file on the server"?
Also with your code I do not see the path to where compters or the msi is being stored.

I can store it in c:\Temp\  

then it would look like this

$computers = Get-Content 'c:\Temp\computers.txt'
$m = "c:\temp\software install.lnk'

can you make it a little more clear?
0
 
LVL 41

Assisted Solution

by:footech
footech earned 2000 total points
ID: 39728720
Now will the "Invoke-Command work on a ShortCut to the MSI file on the server"?
I don't know for certain... never tried.  One way for you to find out.  Keep in mind that sometimes trying to perform an unattended installation can be different than just running the .MSI through a GUI (which is why I am calling msiexec which references the actual .MSI file).  But if the shortcut points to a file on another machine I'm certain you will encounter problems.

Also with your code I do not see the path to where compters or the msi is being stored.
I don't think I understand your question.  You can reference whatever path you need.  If your computers.txt file is in c:\temp, well, you've already shown you know how to read the file with the path explicitly defined.  In my example I just showed how you could run it with the files in the current working directory.  Since working with remote filesystems within a remote PS session involves other complications (search for kerberos second hop and credssp), which is a whole other topic I'll avoid it for this question and just use local paths inside the remote session.

$deploy = {
    Set-Location "c:\temp"
    & msiexec.exe /I installwizard.msi /quiet 
}

$computers = Get-Content "c:\whatever\path\you\want\computers.txt"
foreach ($comp in $computers)
{
   Copy-Item -path \\server\share\installwizard.msi -Destination \\$comp\c$\temp
   $session = New-PSSession -ComputerName $comp
   Invoke-Command -Session $session -ScriptBlock $deploy
}

Open in new window

After the installation has completed you will probably want to do another foreach loop through the computers list to remove the .MSI from each machine with Remove-Item.  And you can clean up all the now-unneeded sessions with Get-PsSession | Remove-PsSession.
0
Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

 
LVL 2

Author Comment

by:learn2earn
ID: 39732655
Okay so here is what works for me.

$computers = Get-Content "c:\temp\computers.txt"
foreach ($comp in $computers)
{
   Copy-Item -path "T:\TBA Client\TBA Client Update.msi" -Destination \\$comp\c$\temp
{

That part actually does what I want.  Which is copy the files to the TEMP directory.
Now I want to Establish a PSSession with all those computers in my $computers variable.
I want to (invoke-item  "c:\temp\*msi") on all those computers to run the msiexe.exe

I have tested it out on one machine PSSession and it works. I just do not know how to get multiple PSSessions to execute the (invoke-item)
0
 
LVL 41

Expert Comment

by:footech
ID: 39732710
If Invoke-Item works for you, then you can just substitute that for line 3 in my post above.  The code I posted creates a new session and then uses that with Invoke-Command to run all the commands that are stored in the $deploy variable.
0
 
LVL 2

Author Comment

by:learn2earn
ID: 39732916
I will test it on Monday as my Day is done here at work!

Thank you, and I will keep you posted.
0
 
LVL 41

Expert Comment

by:footech
ID: 39733072
No problem.  If you have any issues I may not be able to respond until the following week.
Happy Holidays!
0
 
LVL 2

Accepted Solution

by:
learn2earn earned 0 total points
ID: 39740502
Below is what I came up with using the "help (get-help)" commands to get it to work.

I did use your variables to get it started.


$computers = Get-Content "c:\temp\computers.txt"
$rs = Get-PSSession

######
## Functions
################

Function PushMSI {
 Write-Host "------------------------------------------------"
 Write-Host "This will copy the MSI file from localhost c:\temp\"
 write-Host "------------------------------------------------"
 Write-Host ""
 Write-Host ""
 foreach ($comp in $computers)
{
   Copy-Item -path "c:\temp\UPS Client Update.msi" -Destination \\$comp\c$\temp
   
}
}

Function RemoteConnect
{
 Write-Host "------------------------------------------------"
 Write-Host "This will establish a PSSession with all computers in c:\temp\computers.txt"
 write-Host "------------------------------------------------"
 Write-Host ""
 Write-Host ""
Get-Content C:\temp\computers.txt | New-PSSession -ThrottleLimit 50
}

Function InstallMSI
{
 Write-Host "------------------------------------------------"
 Write-Host "This will Install UPS Update on all computers with an Established PSSession"
 write-Host "------------------------------------------------"
 Write-Host "After the Install PSSessions will be removed"
 Write-Host ""
Invoke-Command -Session $rs -ScriptBlock {invoke-item "c:\temp\UPS Client Update.msi"}
}

Get-PSSession | Remove-PSSession

Thank you for your help with getting me started in the right direction!!
0
 
LVL 2

Author Comment

by:learn2earn
ID: 39740592
_
0
 
LVL 2

Author Closing Comment

by:learn2earn
ID: 39747886
I did some reading and found what I was looking for by using the PowerShell Help.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I was prompted to write this article after the recent World-Wide Ransomware outbreak. For years now, System Administrators around the world have used the excuse of "Waiting a Bit" before applying Security Patch Updates. This type of reasoning to me …
A walk-through example of how to obtain and apply new DID phone numbers to your cloud PBX enabled users that are configured in Office 365. Whether you have 1, 10 or 100+ users in your tenant, it's quite easy to get them phone-enabled and making/rece…
This tutorial will show how to configure a new Backup Exec 2012 server and move an existing database to that server with the use of the BEUtility. Install Backup Exec 2012 on the new server and apply all of the latest hotfixes and service packs. The…
This tutorial will show how to configure a single USB drive with a separate folder for each day of the week. This will allow each of the backups to be kept separate preventing the previous day’s backup from being overwritten. The USB drive must be s…

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question