0

This code will cause 2 iterations before continuing to my next code. Running this off a USB drive.

write-Host "McAfee Consumer Product Removal" -ForegroundColor Cyan
$drives = Get-PSDrive
foreach ($root in $drives.root)  {
if (test-path "$root`mccleanup\mccleanup.exe") {
cd "$root`mccleanup" 
.\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 
write-Host "McAfee Consumer Product Removal completed" -ForegroundColor Magenta
    }
}
Moxadonis
  • 23
  • 4
  • 3
    It probably loops more than twice. Try running `(Get-PSDrive).Root` and seeing how many you actually come up with. – Daniel Aug 09 '22 at 05:48
  • Where exactly? Sorry I'm a novice. – Moxadonis Aug 09 '22 at 15:59
  • In the foreach loop `foreach ($root in $drives.root) { ... }` $drives.root is going to contain a root for each drive on the computer which also includes so-called registry drives and mapped drives so you will likely have at least 4 but probably more. Each of those roots will be a loop. Your next line limits which loops do anything by testing if that path exists in that root before doing anything. Most of the drive roots will not have that path and will skip over the code contained in that IF block however if any other drive roots do contain that path the code will run. – Daniel Aug 09 '22 at 16:22
  • Also, as a side note, for readability it probably would be better to change `"$root`mccleanup\mccleanup.exe"` to `"${root}mccleanup\mccleanup.exe"` and `"$root`mccleanup"` to `"${root}mccleanup"` – Daniel Aug 09 '22 at 16:30
  • It runs it twice, see code: `write-Host "McAfee Consumer Product Removal completed" -ForegroundColor Magenta)` Do you have any answer? – Moxadonis Aug 09 '22 at 18:13

1 Answers1

0
$drives = Get-PSDrive -PSProvider filesystem
Moxadonis
  • 23
  • 4