We help IT Professionals succeed at work.

Using Powershell to run a program Uninstall and Installs Remotely

5,416 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.
Comment
Watch Question

yo_beeDirector of Information Technology
CERTIFIED EXPERT

Commented:
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.

Author

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)
Director of Information Technology
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

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
yo_beeDirector of Information Technology
CERTIFIED EXPERT

Commented:
Can you post the batch file you have?

Author

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

Author

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?

Author

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"}

Author

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?
yo_beeDirector of Information Technology
CERTIFIED EXPERT

Commented:
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.
Lionel MMSmall Business IT Consultant
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

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
yo_beeDirector of Information Technology
CERTIFIED EXPERT

Commented:
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
Lionel MMSmall Business IT Consultant
CERTIFIED EXPERT

Commented:
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.

Author

Commented:
We I just saw that in the article you posted... Can Winrs be used on multiple computer at once?

Author

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

NVITEnd-user support
CERTIFIED EXPERT

Commented:
/REBOOT=R/q)

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

Author

Commented:
Thanks for the help everyone. With your guidance I was able to get through this and learned something at the same time....
yo_beeDirector of Information Technology
CERTIFIED EXPERT

Commented:
That is great.

What did you learn?  That WMI is powerful tool.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.