Skip to content

Instantly share code, notes, and snippets.

@joerodgers
Created October 6, 2020 14:00
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joerodgers/06304f41a8f8309ac12e8ebd450153ff to your computer and use it in GitHub Desktop.
Save joerodgers/06304f41a8f8309ac12e8ebd450153ff to your computer and use it in GitHub Desktop.
Trick to force an update to the LastLogon Timestamp for an Active Directory account. This is sometimes necessary for the portal cache accounts if a companies AD team disables accounts based on the LastLogon timestamp of a user.
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
function Update-LastLogonTimestamp
{
[CmdletBinding()]
param
(
[parameter(Position=0, Mandatory=$true)][string]$UserName
)
begin
{
$upn = $UserName
$impersonationSuccessful = $true
}
process
{
if ($UserName.IndexOf("\") -gt 0)
{
# convert to user@domain format
$domain = $UserName.Split("\")[0]
$user = $UserName.Split("\")[1]
$upn = "$user@$domain"
}
try
{
$windowsIdentity = New-Object System.Security.Principal.WindowsIdentity($upn)
$impersonatedContext = $windowsIdentify.Impersonate()
}
catch
{
$impersonationSuccessful = $false
}
finally
{
if($impersonatedContext)
{
$impersonatedContext.Undo()
}
}
}
end
{
$impersonationSuccessful
}
}
# update non-managed accounts
# Update-LastLogonTimestamp -UserName "portalsuperreader@contoso.com"
# Update-LastLogonTimestamp -UserName "portalsuperuser@contoso.com"
# update all managed accounts
Get-SPManagedAccount | % { Update-LastLogonTimestamp -UserName $_.UserName }
@caricas91
Copy link

Hi, i can run the script with success but the LastLogonTimestamp in AD is not getting updated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment