Remove multiple versions of OneDrive as logged on user

I have instances where OneDrive is installed for the logged on user when it should not be and want to remove it. I have the below fixlet that runs as the logged on user and runs the command based on the location of the OneDriveSetup.exe file:

override run
hidden=true
detached=true
runas=currentuser
run "{pathnames of files "OneDriveSetup.exe" of folders of folder "OneDrive" of folder "Appdata\Local\Microsoft" of folder (name of current user) of folder "c:\users"}" /uninstall

This works fine if the user only has one version installed however it fails if more than one version is installed:

   Command succeeded override run (action:21694)
   Command succeeded override hidden=true (action:21694)
   Command succeeded override detached=true (action:21694)
   Command succeeded override runas=currentuser (action:21694)
   Command failed (Process creation failed - 2) run "c:\users\<user>\Appdata\Local\Microsoft\OneDrive\18.143.0717.0002\OneDriveSetup.exec:\users\<user>\Appdata\Local\Microsoft\OneDrive\19.043.0304.0013\OneDriveSetup.exe" /uninstall (action:21694)

How can I get this to run for each version successfully? Can concatenate be used here and if so how?

You’re on the right path with the concatenate. I’m afraid I’m typing on a phone and can’t do the full solution at the moment, but the basic idea is to build a batch file, where each line contains an uninstall command; concatenate the lines with “%0d%0a”, the ascii codes for Carriage Return / Line Feed. Wrap the file pathname with “%22”, the ascii code for doublequotes, in case the path contains a space

Delete __appendfile
Appendfile { concatenation "%0d%0a" of ("%22" & pathname of it & "%22 /uninstall") of (relevance that finds the files) }
Delete uninstall.cmd
Move __appendfile uninstall.cmd
// Do the user overrides here
Wait uninstall.cmd
1 Like

I ran the following:

Delete __appendfile
Appendfile { concatenation "%0d%0a" of ("%22" & pathname of it & "%22 /uninstall") of (files "OneDriveSetup.exe" of folders of folder "OneDrive" of folder "Appdata\Local\Microsoft" of folder (name of current user) of folder "c:\users") }
Delete uninstall.cmd
Move __appendfile uninstall.cmd
// Do the user overrides here
override run
hidden=true
detached=true
runas=currentuser
run uninstall.cmd

And it errors out with:

   Command succeeded delete No 'C:\Program Files (x86)\BigFix Enterprise\BES Client\__BESData\CustomSite_All_Computers\__appendfile' exists to delete, no failure reported (action:21698)
   Command succeeded appendfile { concatenation "%0d%0a" of ("%22" & pathname of it & "%22 /uninstall") of (files "OneDriveSetup.exe" of folders of folder "OneDrive" of folder "Appdata\Local\Microsoft" of folder (name of current user) of folder "c:\users") } (action:21698)
   Command succeeded (file created) appendfile "c:\users\<user>\Appdata\Local\Microsoft\OneDrive\18.143.0717.0002\OneDriveSetup.exe" /uninstall
"c:\users\<user>\Appdata\Local\Microsoft\OneDrive\19.043.0304.0013\OneDriveSetup.exe" /uninstall (action:21698)
   Command succeeded appendfile "c:\users\<user>\Appdata\Local\Microsoft\OneDrive\18.143.0717.0002\OneDriveSetup.exe" /uninstall
"c:\users\<user>\Appdata\Local\Microsoft\OneDrive\19.043.0304.0013\OneDriveSetup.exe" /uninstall (action:21698)
   Command succeeded delete No 'C:\Program Files (x86)\BigFix Enterprise\BES Client\__BESData\CustomSite_All_Computers\uninstall.cmd' exists to delete, no failure reported (action:21698)
   Command succeeded move __appendfile uninstall.cmd (action:21698)
   Command succeeded override run (action:21698)
   Command succeeded override hidden=true (action:21698)
   Command succeeded override detached=true (action:21698)
   Command succeeded override runas=currentuser (action:21698)
   Command failed (Process creation failed - 2) run uninstall.cmd (action:21698)

Ah, yeah.
The logged-on user doesn’t have permission to the BESData directory where the uninstall.cmd is, so the user can’t run the script.

See the notes in my tip at Tip - Action Override User settings about copying the uninstall to a UserFolder parameter for more details.

It’ll be something like

parameter "UserFolder"="{(preceding text of first "|" of it | it) of concatenation "|" of profile folders of users of logged on users whose (active of it)}\MyBESContent\{id of action as string}"
Folder create "{parameter "UserFolder"}"
 Copy __appendfile "{parameter "UserFolder"}\uninstall.cmd"
// Override options here
Run CMD.exe /c "cd "{parameter "UserFolder"}" & uninstall.cmd"

That creates a folder for the script, in a directory the user can access, and copies the script there. Then the CMD shell running as the user changes directory to that folder before running the uninstall.cmd

1 Like

Thank you this worked. I had to remove the detached=true parameter though as the script “ran” successfully but nothing happened until it was no longer detached.

Here is the final actionscript for anyone else needing it along with the relevance I used. I cleaned up the MyBESContent folder after running as well as the now mostly empty OneDrive folder in the user’s profile:

delete __appendfile
appendfile { concatenation "%0d%0a" of ("%22" & pathname of it & "%22 /uninstall") of (files "OneDriveSetup.exe" of folders of folder "OneDrive" of folder "Appdata\Local\Microsoft" of folder (name of current user) of folder "c:\users") }

parameter "UserFolder"="{(preceding text of first "|" of it | it) of concatenation "|" of profile folders of users of logged on users whose (active of it)}\MyBESContent\{id of action as string}"
folder create "{parameter "UserFolder"}"
copy __appendfile "{parameter "UserFolder"}\uninstall.cmd"

override wait
hidden=true
runas=currentuser
wait CMD.exe /c "cd "{parameter "UserFolder"}" & uninstall.cmd"

folder delete "{(preceding text of first "|" of it | it) of concatenation "|" of profile folders of users of logged on users whose (active of it)}\MyBESContent"

if {( NOT exists file "OneDrive.exe" of folder "OneDrive" of folder "Appdata\Local\Microsoft" of folder (name of current user) of folder "C:\Users" ) AND ( exists folder "OneDrive" of folder "Appdata\Local\Microsoft" of folder (name of current user) of folder "C:\Users" )}
	folder delete "{pathname of folder "OneDrive" of folder "Appdata\Local\Microsoft" of folder (name of current user) of folder "c:\users"}"
endif

Relevance:

exists file "OneDrive.exe" of folder "OneDrive" of folder "Appdata\Local\Microsoft" of folder (name of current user) of folder "C:\Users"

4 Likes

Just as info sharing, an approach I like to use for looping through commands and avoid creating batch files is to build a parameter of the files to be executed then loop those using a CMD “for” command, eg

parameter "mycommands" = "{"%22" & (concatenation "%22,%22" of (pathnames of files "OneDriveSetup.exe" of folders of folder "OneDrive" of folder "Appdata\Local\Microsoft" of folder (name of current user) of folder "C:\Users")) & "%22"}"

override wait
hidden=true
runas=currentuser

wait cmd.exe /c "for %a in ({parameter "mycommands"}) do (%a /uninstall)"

I would use the debugger to test first replacing the /c with /k and maybe use an echo command so I can see the CMD output is syntaxly correct before actaully running it, i.e.

parameter "mycommand" = "{"%22" & (concatenation "%22,%22" of (pathnames of files "OneDriveSetup.exe" of folders of folder "OneDrive" of folder "Appdata\Local\Microsoft" of folder (name of current user) of folder "c:\users")) & "%22"}"

wait cmd.exe /k "for %a in ({parameter "mycommand"}) do (echo %a /uninstall)"

5 Likes