/build/static/layout/Breadcrumb_cap_w.png
08/02/2017 2079 views
Hi All,
I have been learning Powershell and a bit stuck with compiling a count.  My script is
$users = Get-ChildItem -Path "C:\Users"
$users | ForEach-Object {
$counter =Get-ChildItem -Path "C:\Users\$($_.Name)\AppData\Local" -Recurse -Include "file.txt" -ea 0 | Measure-Object | Select-Object -ExpandProperty Count
if ($counter -lt "1" ) {Write-Host "files not exist"}
}




What I'm trying to do is write a custom SCCM script for detecting the presence of (in this case the absence of) a deployment type.
SCCM will detect a screen echo as deployment type present.  My script above looks for the file "file.txt" and outputs an echo every time it doesn't see that file.  There is 4 profiles on this VM and so the output says  ({blank} is for an empty line)
{blank}
{blank}
files not exist
files not exist


I need the script to count the screen echo's and output a counter, so then I can say if $totalcounter -lt 1 write-host "something"

I hope that says it all.  Please ask questions if this doesn't help

Thanks
Answer Summary:
2 Comments   [ + ] Show comments

Comments

  • For a PowerShell detection method, shouldn't you just be writing out if the file is detected (not writing out if the file isn't detected)? Also are your installations not per-machine? Just wondering why you're searching through multiple profiles for a file. And finally can you not utilise Test-Path to check for the presence of a file instead?
  • I have been tasked with removing Firefox from all machines. This includes anybody who has installed into appdata.

    light bulb - I think I know what you are asking - I will get breakfast and a coffee and I will investigate. I spent too much time yesterday on a single path, from something we did in VBS a long time ago
    thanks

Answer Chosen by the Author

0
I finally had a break through in the times I could afford to devote to this
First I had to run compliance rules to detect Firefox in the user directories, this is the detection for setting up compliance
$users = Get-ChildItem -Path "C:\Users"
$users | ForEach {

$counter =Get-ChildItem -Path "C:\Users\$($_.Name)\AppData\Local" -Recurse -Include "Firefox.exe" -ea 0 | Measure-Object | Select-Object -ExpandProperty Count
if ($counter -gt "0" ) {exit}
}
Write-Host "done"

From what I have read and tested, SCCM has script detection from an echo command, so if you want something to say when it's not installed, you have to detect the absence of the application then echo something.

After that, the uninstall script

#Uninstall Firefox User rights installed
get-childitem "C:\users" firefox.exe -Recurse -Force | foreach-object {
$setup = $_.directory.tostring() + "\uninstall\helper.exe"
$args = " /s"
$uninst = Start-Process $setup -PassThru -ArgumentList $args -wait
$uninst.WaitForExit()
}

#Delete registry key
$null = New-PSDrive -Name HKU -PSProvider Registry -Root Registry::HKEY_USERS
Set-Location HKU:
Get-ChildItem -path HKU: | ForEach-Object {$uninstall = $_.Name + "\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*"

if((Test-Path "hklm:\Software\Mozilla\Firefox") -eq 'true'){
Remove-Item "hklm:\Software\Mozilla\Firefox" -Recurse -Force
}
if((Test-Path "hklm:\SOFTWARE\Wow6432Node\Mozilla\Firefox") -eq 'true'){
Remove-Item "hklm:\SOFTWARE\Wow6432Node\Mozilla\Firefox" -Recurse -Force
}

If ($uninstall -contains "*firefox*"){
$firefox = get-ChildItem -path $uninstall | ?{$_.Name -like "*Firefox*"}

Remove-Item -path $firefox -Recurse -ErrorAction SilentlyContinue
}
}

Then the detection to make sure the uninstall script ran.  This rule has to be the opposite of the compliance rule.

$users = Get-ChildItem -Path "C:\Users"
$users | ForEach {

$counter =Get-ChildItem -Path "C:\Users\$($_.Name)\AppData\Local" -Recurse -Include "Firefox.exe" -ea 0 | Measure-Object | Select-Object -ExpandProperty Count
if (!($counter -eq $false)) {exit}

Write-Host "done"}

I hope this helps people to uninstall their unwanted stuff from their domain.

Please don't forget to rate this if it's helpful

Answered 08/07/2017 by: Tempril
Purple Belt