I have a function that allows me to enter a remote PowerShell session using encrypted credentials from an XML file. It usually works great, but occasionally I get an access denied error. The strange part is that if I open a new tab and try to connect to the same device, it works fine again. Then later, it might stop working in that tab too, forcing me to open a new one each time. Has anyone experienced something like this? Any ideas on how to fix it?
2 Answers
It might be worth trying to run your script outside of PowerShell ISE. I've noticed that ISE can have some weird quirks that mess with remote sessions sometimes.
Could you share your function code? I've run into cases where scope issues in modules can affect how other modules behave, leading to these kinds of errors.

Here’s the function:
Function Enter-PSSessionAADJ {
param ($computer)
[string] $IP = Get-IPFromSCCM $computer
If ($IP) {
$cred = (Get-EncryptedCredentials)
$Session = New-PSSession -ComputerName $IP -ConfigurationName 'Microsoft.PowerShell' -Credential $cred
Enter-PSSession -Session $Session
}
}
I also set an alias EPS for this function which calls another one to get the device's current IP.