Solved

Using Powershell to run a program Uninstall and Installs Remotely

Posted on 2016-08-31
20
94 Views
Last Modified: 2016-09-07
We have recently taken over a small company who's network is a bit of a mess. I need to uninstall an old AV program and install a update one. They do not have SCCM or another or remote software deployment total. I did create a series of batch files to run as a startup script as to run under the local system account BUT requires a system reboot in order to take effect. My next though was to see if I can enable WinRM in the domain so I can excute all commads via powershell.

Here is what I am thinking
1) Get a list of all devices into a text file
2) Create a powershell script that will uninstall the first program but not continue until the uninstall completes. It will then check for the presence of a folder path and if so run a specific program, if it is not present continue on with the next program and old this complete will then install the 3  and file program. Also need some way to know if a step failed or not

I would like to be able to use powershell to run this script agaist select devices at will. My problem is that I an not that strong with powershell yet.
0
Comment
Question by:compdigit44
  • 10
  • 6
  • 2
  • +1
20 Comments
 
LVL 21

Expert Comment

by:yo_bee
ID: 41779015
What I would is start a remote session to a test machine and run some basic command against the remote machine like Ping.  This will get you familiar with the process with human intervention. Then you can move on to the uninstalling the AV application. If that works keep on moving up in the latter of your ultimate goal.
Once you are able to complete automate the script against a single computer then you can loop through all your machines.
0
 
LVL 19

Author Comment

by:compdigit44
ID: 41779032
I already have the command to uninstall and install the software which i have been testing I just need help putting this altogether in a powershell script since I am in a bit of a time crunch and what would take be days to figure out a E.E expert would know the answer a lot faster... :o)
0
 
LVL 21

Accepted Solution

by:
yo_bee earned 250 total points
ID: 41779154
Do you know much about WMI?

You can use the instructions from this BLOG:
https://blogs.technet.microsoft.com/heyscriptingguy/2011/12/14/use-powershell-to-find-and-uninstall-software/

Why do you need to do the part with the folders and files?

Here is a modified snippet from the BLOG
#you will need to get 3 values for what you are look to uninstall
#replace the IdentifiyingNumber,Name and Version with your product.

$classKey=”IdentifyingNumber=`”`{89F4137D-6C26-4A84-BDB8-2E5A4BB71E00`}`”,Name=`”Microsoft Silverlight`”,version=`”4.0.60831.0`”” 
$machines = Get-Content -Path Filename 
Foreach ($machine in $machines)
{
    { ([wmi]”\\$machine\root\cimv2:Win32_Product.$classKey”).uninstall() }
}

Open in new window

0
 
LVL 19

Author Comment

by:compdigit44
ID: 41779821
I was thinking of using powershell to target a list of workstations. I already have the batch files to uninstall the program I and looking for a way to "invoke" the process on a bunch of workstation at once with out having to reboot then for the Startup script to kick in
0
 
LVL 21

Expert Comment

by:yo_bee
ID: 41780528
Can you post the batch file you have?
0
 
LVL 19

Author Comment

by:compdigit44
ID: 41780870
I am not at my office right now but I will give you an example of what I have

batch file1
"C:\program files\SomeProgram\uninstall.exe"

batch file 2
\\server\share$\Software\FrmInst.exe /Forceuninstall
\\server\share$\Software\Framepkg.exe /ForceInstall

Batch File 3
\\server\share$\Software\Setup.exe /q"

This is from memory but right now I have setup set as a startup script at set the GP to run synchronously. I would like to be able to open powershell target select workstation on a text file and have them run each bath on my command without having them invoke each one without a reboot
0
 
LVL 19

Author Comment

by:compdigit44
ID: 41781837
SO here is what I have so far... I have confirmed each of my batch files works as expected..

I am going the following
$device= get-content "c:\workstations.txt"

invoke-command -computername $device -scriptblock {"\\server\path\script.bat"}

The command does not produce an error but at the same time does not run on the device. Can the script block accept UNC paths?
0
 
LVL 19

Author Comment

by:compdigit44
ID: 41783133
I have confirmed that I am able to run commands and the remote workstation without issue. But will not run my scripts which are on a network share when I reference them in a UNC path

invoke-command -computername $device -scriptblock {"\\server\path\script.bat"}
0
 
LVL 19

Author Comment

by:compdigit44
ID: 41783903
I have been working on this a lot and here is what I have come up with so far

I have been doing my best to change my batch file to compliant Powershell script struggling but trying

1) I tried switch from using invoke-command -computername $devices -Filepath "\\server\share\script.ps1"

2) My script is very simple and tried to change the command prompt "If Exist GoTo" statemes to powershell ones the problem is my script contain command line switchs which it does not seem to like since it keeps stating missing parameters etc..

For example, inside my script contains the following:  \\server\share\SetpVse.exe /q Reboot=q/R

I have tried using quote around the whole string and Powershell does not like everything after the SetupVse.exe. When I remove the switch the script does not error out but at the same does not appear to run on the target host since I do not see the MSiexe or SetupVSe.exe processes running..

Thoughts?
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 21

Expert Comment

by:yo_bee
ID: 41784350
Have you looked at using PSEXEC from sysinternals by Microsoft?

This will give you the ability to run remote commands against remote machines.

I would try this method. It is more straight forward and you do not need to know scripting.

https://technet.microsoft.com/en-us/sysinternals/pxexec

Run each of the command from a shared UNC rather than the batch from what I can see in the replies.

Also on a side note I am on vacation in Italy and my replies will be few and far between.
0
 
LVL 24

Assisted Solution

by:lionelmm
lionelmm earned 250 total points
ID: 41784581
This is a great site to check out for questions like this--her is a link that shows you how to find and then uninstall software https://blogs.technet.microsoft.com/heyscriptingguy/2011/12/14/use-powershell-to-find-and-uninstall-software/ and if the software happens to be an app then this script suggestion helps with that https://blogs.technet.microsoft.com/heyscriptingguy/2013/07/27/weekend-scripter-use-powershell-to-uninstall-modern-apps/ and then this link has several different approaches to install via powershell --hope this helps http://social.technet.microsoft.com/wiki/contents/articles/7703.powershell-running-executables.aspx
0
 
LVL 19

Author Comment

by:compdigit44
ID: 41784757
Thank you all for your replies. I have not had a chance to review all of the links but I believe psexec can only work on one device at a time and cannot read a list of devices from a test file
0
 
LVL 21

Expert Comment

by:yo_bee
ID: 41784810
You can pass the computer via a file.

Please review the link I posted for all the switches.

There should be one for @file or something like that
0
 
LVL 24

Expert Comment

by:lionelmm
ID: 41785018
yo_bee is correct, you can use the @file option to specify a list of computers. You can also  pass it a variable(s) using the FOR command.
0
 
LVL 19

Author Comment

by:compdigit44
ID: 41785019
We I just saw that in the article you posted... Can Winrs be used on multiple computer at once?
0
 
LVL 19

Author Comment

by:compdigit44
ID: 41785052
I have been doing to base level testing and find that even though I do not  get an error running psexec with the full UNC patch to the install file and switch it just does not run and have confirmed this by running the running processes on the remote computer. I did trying to copy the batch file to the remote computer and it did not error out but at the same time did not run the install.

Please see the samplew from my batch file and powershell version as well.

Batch File
IF EXIST "C:\Program Files\McAfee\Agent" GoTo Exit

"\\Server\Share$\VSE880LMLRP7\SetupVSE.exe" /x /q /REBOOT=R/q)

"\\Server\Share$\FramePkg.exe" /INSTALL=AGENT /FORCEINSTALL /SILENT

:Exit

Open in new window


PowerShell
IF ("C:\Program Files (x86)\McAfee\VirusScan Enterprise")
{
Exit
}
Else
{
"\\server\share$\VSE880LMLRP7\SetupVSE.exe"
}

Open in new window

0
 
LVL 23

Expert Comment

by:NVIT
ID: 41785165
/REBOOT=R/q)

Is that right parenthesis supposed to be there? It may be a factor in a batch file
0
 
LVL 19

Author Closing Comment

by:compdigit44
ID: 41788817
Thanks for the help everyone. With your guidance I was able to get through this and learned something at the same time....
0
 
LVL 21

Expert Comment

by:yo_bee
ID: 41789064
That is great.

What did you learn?  That WMI is powerful tool.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

New Windows 7 Installations take days for Windows-Updates to show up and install. This can easily be fixed. I have finally decided to write an article because this seems to get asked several times a day lately. This Article and the Links apply to…
Synchronize a new Active Directory domain with an existing Office 365 tenant
This tutorial will give a an overview on how to deploy remote agents in Backup Exec 2012 to new servers. Click on the Backup Exec button in the upper left corner. From here, are global settings for the application such as connecting to a remote Back…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now