[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2266
  • Last Modified:

Using Powershell to run a program Uninstall and Installs Remotely

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
compdigit44
Asked:
compdigit44
  • 10
  • 6
  • 2
  • +1
2 Solutions
 
yo_beeDirector of ITCommented:
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
 
compdigit44Author Commented:
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
 
yo_beeDirector of ITCommented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
compdigit44Author Commented:
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
 
yo_beeDirector of ITCommented:
Can you post the batch file you have?
0
 
compdigit44Author Commented:
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
 
compdigit44Author Commented:
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
 
compdigit44Author Commented:
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
 
compdigit44Author Commented:
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
 
yo_beeDirector of ITCommented:
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
 
Lionel MMSmall Business IT ConsultantCommented:
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
 
compdigit44Author Commented:
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
 
yo_beeDirector of ITCommented:
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
 
Lionel MMSmall Business IT ConsultantCommented:
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
 
compdigit44Author Commented:
We I just saw that in the article you posted... Can Winrs be used on multiple computer at once?
0
 
compdigit44Author Commented:
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
 
NVITCommented:
/REBOOT=R/q)

Is that right parenthesis supposed to be there? It may be a factor in a batch file
0
 
compdigit44Author Commented:
Thanks for the help everyone. With your guidance I was able to get through this and learned something at the same time....
0
 
yo_beeDirector of ITCommented:
That is great.

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

Featured Post

Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

  • 10
  • 6
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now