Avatar of compdigit44
compdigit44
 asked on

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.
PowershellWindows Server 2008

Avatar of undefined
Last Comment
yo_bee

8/22/2022 - Mon
yo_bee

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.
compdigit44

ASKER
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)
ASKER CERTIFIED SOLUTION
yo_bee

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
compdigit44

ASKER
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
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
yo_bee

Can you post the batch file you have?
compdigit44

ASKER
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
compdigit44

ASKER
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?
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
compdigit44

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

ASKER
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_bee

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.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
compdigit44

ASKER
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_bee

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 MM

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.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
compdigit44

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

ASKER
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

NVIT

/REBOOT=R/q)

Is that right parenthesis supposed to be there? It may be a factor in a batch file
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
compdigit44

ASKER
Thanks for the help everyone. With your guidance I was able to get through this and learned something at the same time....
yo_bee

That is great.

What did you learn?  That WMI is powerful tool.