Last active
November 12, 2018 12:05
-
-
Save techdecline/84171f6c1f657e2e85c94e6e8b290f25 to your computer and use it in GitHub Desktop.
PowerShell function for removing machine Kerberos Tickets and invoking Group Policy Update
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <# | |
| .Synopsis | |
| Refresh Kerberos Ticket for machine and update Group Policy | |
| .Description | |
| Refresh Kerberos Ticket for machine and update Group Policy. Group Policy Refresh can be controlled using Switch Parameter | |
| .Example | |
| PS> Refresh-ComputerKerberosTicket -SessionName "LocalSystem" -UpdateGroupPolicy | |
| Remote Kerberos Tickets for LocalSystem and refresh Group Policy | |
| #> | |
| function Refresh-ComputerKerberosTicket { | |
| [CmdletBinding()] | |
| param ( | |
| # Select Session Name | |
| [Parameter(Mandatory=$false)] | |
| [ValidateSet('LocalSystem', 'NetworkService')] | |
| [string]$SessionName = "LocalSystem", | |
| # Toogle GP Refresh | |
| [Parameter(Mandatory=$false)] | |
| [switch]$UpdateGroupPolicy | |
| ) | |
| begin { | |
| Write-Verbose "Selected Session is: $SessionName" | |
| switch ($SessionName) { | |
| 'LocalSystem' { | |
| $ID = '0x3e7' } | |
| 'NetworkService' { | |
| $ID = '0x3e4' } | |
| Default { Write-Error 'Invalid Session Name' -ErrorAction Stop} | |
| } | |
| } | |
| process { | |
| $cmdLine = "klist -li $ID purge" | |
| Write-Verbose "Command Line is: $cmdLine" | |
| $returnCmd = Invoke-Expression $cmdLine | |
| if ((Get-UICulture).Name -eq "de-DE") { | |
| Write-Verbose "Running in Language Mode: de-DE" | |
| If ($returnCmd[4].TrimStart() -like 'Ticket(s) gelöscht') { | |
| Write-Verbose "Cleared Kerberos Tickets" | |
| if ($UpdateGroupPolicy) { | |
| Write-Verbose "Triggering GP Update" | |
| $cmdLine = "gpupdate /force" | |
| Invoke-Expression $cmdLine | |
| } | |
| } Else { | |
| Write-Error "Could not purge Kerberos Tickets" | |
| } # End If | |
| } | |
| else { | |
| Write-Verbose "Running in Language Mode: $((Get-UICulture).Name)" | |
| If ($returnCmd[4].TrimStart() -like 'Ticket(s) Purged!') { | |
| Write-Verbose "Cleared Kerberos Tickets" | |
| if ($UpdateGroupPolicy) { | |
| Write-Verbose "Triggering GP Update" | |
| $cmdLine = "gpupdate /force" | |
| Invoke-Expression $cmdLine | |
| } | |
| } Else { | |
| Write-Error "Could not purge Kerberos Tickets" | |
| } # End If | |
| } | |
| } | |
| end { | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment