Hi @HKPR , Thanks for reaching out Microsoft Q&A.
Here is the function snippet that will help you to check if have a valid session or not.
function Login()
{
$context = Get-AzContext
if (!$context)
{
Connect-AzAccount
}
else
{
Write-Host " Already connected"
}
}