McAfee Consumer Product Removal Silent Script

2.1k Views Asked by At

I made a script that works some of the time to remove McAfee products. The issue is that there are sometimes 2 tmp folders that get created. Then, there's a chance that it renames and copies the wrong folder. I'm using Where-Object and LastWriteTime to rename and copy a tmp folder that was created within the last 10 seconds. There has to be a better way in finding a randomly named tmp folder. How can I fix the code? Thank you.

New-Item "C:\temp" -ItemType Directory
$url = "https://download.mcafee.com/molbin/iss-loc/SupportTools/MCPR/MCPR.exe"
$dest = "C:\temp\MCPR.exe" 
Invoke-WebRequest -Uri $url -OutFile $dest -verbose
start-process C:\temp\MCPR.exe -verb runas  -verbose
start-sleep -Seconds 3
stop-process -Name "McClnUI" -verbose
cd $Env:LocalAppData\Temp
$Now = Get-Date
Get-ChildItem $Env:LocalAppData\Temp\*.tmp | Where-Object { $_.LastWriteTime -gt $Now.AddSeconds(-10) } | Rename-Item -NewName "MCPRtemp" -verbose -ErrorAction SilentlyContinue
Copy-Item -Path "$Env:LocalAppData\Temp\MCPRtemp*" -Destination "C:\temp" -Recurse
cd C:\temp\MCPRtemp
.\Mccleanup.exe -p StopServices,MFSY,PEF,MXD,CSP,Sustainability,MOCP,MFP,APPSTATS,Auth,EMproxy,FWdiver,HW,MAS,MAT,MBK,MCPR,McProxy,McSvcHost,VUL,MHN,MNA,MOBK,MPFP,MPFPCU,MPS,SHRED,MPSCU,MQC,MQCCU,MSAD,MSHR,MSK,MSKCU,MWL,NMC,RedirSvc,VS,REMEDIATION,MSC,YAP,TRUEKEY,LAM,PCB,Symlink,SafeConnect,MGS,WMIRemover,RESIDUE -v -s
1

There are 1 best solutions below

6
On

Since you appear to know the exe name within the tmp directory, you can simply find the latest directory that contains it. You can also rename it while copying further simplifying the code.

New-Item "C:\temp" -ItemType Directory
$url = "https://download.mcafee.com/molbin/iss-loc/SupportTools/MCPR/MCPR.exe"
$dest = "C:\temp\MCPR.exe" 
Invoke-WebRequest -Uri $url -OutFile $dest -verbose
start-process C:\temp\MCPR.exe -verb runas  -verbose
start-sleep -Seconds 3
stop-process -Name "McClnUI" -verbose
$tempfolder = Get-ChildItem $Env:LocalAppData\Temp\*.tmp -Recurse -Directory | 
    Where-Object {$_ | Get-Childitem -Recurse *mccleanup.exe} |
        Select-Object -First 1
Copy-Item -Path $tempfolder.fullname -Destination "C:\temp\MCPRTemp" -Recurse
cd C:\temp\MCPRtemp
.\Mccleanup.exe -p StopServices,MFSY,PEF,MXD,CSP,Sustainability,MOCP,MFP,APPSTATS,Auth,EMproxy,FWdiver,HW,MAS,MAT,MBK,MCPR,McProxy,McSvcHost,VUL,MHN,MNA,MOBK,MPFP,MPFPCU,MPS,SHRED,MPSCU,MQC,MQCCU,MSAD,MSHR,MSK,MSKCU,MWL,NMC,RedirSvc,VS,REMEDIATION,MSC,YAP,TRUEKEY,LAM,PCB,Symlink,SafeConnect,MGS,WMIRemover,RESIDUE -v -s