1

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
Moxadonis
  • 23
  • 4

1 Answers1

1

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
Doug Maurer
  • 8,090
  • 3
  • 12
  • 13
  • Copy-Item : Cannot bind argument to parameter 'Path' because it is null. – Moxadonis Aug 08 '22 at 06:09
  • Sounds like the file may be deeper than one level. Try the edited code please. – Doug Maurer Aug 08 '22 at 06:40
  • Get-Childitem : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input. The file is in the first folder of the .tmp folder. – Moxadonis Aug 08 '22 at 15:10
  • Then the original code should've worked fine. You'll need to debug why `Get-ChildItem $Env:LocalAppData\Temp\*.tmp` returns nothing – Doug Maurer Aug 08 '22 at 15:23
  • I've edited the code once more. You may try again and if still receive errors go line by line and figure out what isn't working and why. – Doug Maurer Aug 08 '22 at 15:25
  • Doesn't work but thanks for getting me closer. – Moxadonis Aug 08 '22 at 18:00