Skip to content

Instantly share code, notes, and snippets.

@rbleattler
Last active December 18, 2020 15:44
Show Gist options
  • Save rbleattler/52aa84e47e03a5222aedee2bd32be34b to your computer and use it in GitHub Desktop.
Save rbleattler/52aa84e47e03a5222aedee2bd32be34b to your computer and use it in GitHub Desktop.
Stops SCCM Services and Scheduled tasks on local client to stop unannounced reboots
## Stop-SCCMOppression.ps1 ##
function Stop-SCCMOppression {
[CmdletBinding()]
param (
[switch]
$Enable
)
begin {
Write-Debug "Enter [$($PSCmdlet.MyInvocation.MyCommand.Name)]..."
$PSBoundParameters.Keys.ForEach{
if ($PSBoundParameters.PSItem -is [string]) {
Write-Debug "$_ : $($PSBoundParameters.Item($_))"
} else {
Write-Debug "$_ : $($PSBoundParameters.Item($_).GetType())"
}
}
}
process {
$CCMService = Get-Service 'CcmExec'
$VerboseMessage = "Checking Startup Type..."
Write-Verbose -Message $VerboseMessage
$VerboseMessage = "Startup Type : {0}" -f $CCMService.StartupType
Write-Verbose -Message $VerboseMessage
$CCMScheduleTasks = Get-ScheduledTask -TaskPath '\Microsoft\Configuration Manager\'
$VerboseMessage = "Checking State of Oppressive Scheduled Tasks..."
Write-Verbose -Message $VerboseMessage
$CCMScheduleTasks.ForEach{
$VerboseMessage = '{0} : {1}' -f $PSItem.TaskName, $PSItem.State
Write-Verbose -Message $VerboseMessage
}
if ($Enable) {
if ($CCMService.StartupType -eq 'Disabled') {
$VerboseMessage = "Enabling Oppressive Service..."
Write-Verbose -Message $VerboseMessage
$CCMService | Set-Service -StartupType AutomaticDelayedStart
$CCMService | Start-Service
} else {
$VerboseMessage = "Oppressive Service is Enabled..."
Write-Verbose -Message $VerboseMessage
}
$CCMScheduleTasks.ForEach{
#$EnumStates = [Microsoft.PowerShell.Cmdletization.GeneratedTypes.ScheduledTask.StateEnum].GetEnumNames()
if ($PSItem.State -ne 'Disabled') {
$VerboseMessage = '{0} : {1}' -f $PSItem.TaskName, $PSItem.State
Write-Verbose -Message $VerboseMessage
} else {
$VerboseMessage = '{0} : {1}' -f $PSItem.TaskName, $PSItem.State
Write-Verbose -Message $VerboseMessage
$VerboseMessage = 'Enabling: {0}...' -f $PSItem.TaskName
Write-Verbose -Message $VerboseMessage
$null = Enable-ScheduledTask -InputObject $PSItem
}
}
} else {
if ($CCMService.StartupType -ne 'Disabled') {
$VerboseMessage = "Disabling Oppressive Service..."
Write-Verbose -Message $VerboseMessage
$CCMService | Set-Service -StartupType Disabled
$CCMService | Stop-Service
} else {
$VerboseMessage = "Oppressive Service is Disabled..."
Write-Verbose -Message $VerboseMessage
}
$CCMScheduleTasks.ForEach{
#$EnumStates = [Microsoft.PowerShell.Cmdletization.GeneratedTypes.ScheduledTask.StateEnum].GetEnumNames()
if ($PSItem.State -eq 'Disabled') {
$VerboseMessage = '{0} : {1}' -f $PSItem.TaskName, $PSItem.State
Write-Verbose -Message $VerboseMessage
} else {
$VerboseMessage = '{0} : {1}' -f $PSItem.TaskName, $PSItem.State
Write-Verbose -Message $VerboseMessage
$VerboseMessage = 'Disabling: {0}...' -f $PSItem.TaskName
Write-Verbose -Message $VerboseMessage
$null = Disable-ScheduledTask -InputObject $PSItem
}
}
}
}
end {
Write-Debug "Exit [$($PSCmdlet.MyInvocation.MyCommand.Name)]..."
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment