Powershell - detect the removal of applications
Hi All,
I have been learning Powershell and a bit stuck with compiling a count. My script is
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)
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
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
2 Comments
[ + ] Show comments
Answers (1)
Answer Summary:
Please log in to answer
Posted by:
Tempril
7 years ago
Top Answer
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
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
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 - Tempril 7 years ago