Skip to content

Instantly share code, notes, and snippets.

@kennystrawnmusic
Last active December 10, 2025 19:15
Show Gist options
  • Select an option

  • Save kennystrawnmusic/5588f7849661037f9ca570373736d85b to your computer and use it in GitHub Desktop.

Select an option

Save kennystrawnmusic/5588f7849661037f9ca570373736d85b to your computer and use it in GitHub Desktop.
function Invoke-PSADSession {
[CmdletBinding(DefaultParameterSetName="PasswordAuth")]
param(
[Parameter(ParameterSetName="PasswordAuth", Mandatory=$true)]
[System.Management.Automation.PSCredential]$Credential,
[Parameter(Mandatory=$true)]
[string]$ComputerName,
[bool]$Interactive = $true,
[Parameter(ParameterSetName="PassTheTicket")]
[switch]$PTT
)
$ad = (Import-Module ActiveDirectory -PassThru -AsCustomObject).Clone()
$s = if ($PTT) {
New-PSSession -ComputerName $ComputerName -Authentication Kerberos
} else {
New-PSSession -ComputerName $ComputerName -Credential $Credential
}
Invoke-Command -Session $s -ScriptBlock {
Import-Module $Using:ad -Global
}
if ($Interactive) {
Enter-PSSession $s
} else {
return $s
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment