Public/New-IdracSession.ps1

Function New-IdracSession{
    [CmdletBinding(SupportsShouldProcess=$true,ConfirmImpact='None')]
    Param (
        [Parameter(Mandatory = $true)]
        [string]$iDRACName,

        [System.Management.Automation.PSCredential]$Credential
    )

    begin{
        if (!$Credential){
            Write-Verbose -Message "Credential does not exist, please enter a credential"
            $Credential = Get-Credential -UserName "root" -Message "Enter your credentials to connect iDRAC console."
        }
    }

    process{
        Write-Verbose -Message "Check if ther is any session related to this server.."
        $session = Get-SSHSession | Where-Object { $_.Host -eq $iDRACName -and $_.Connected -eq $true}

        if(!$session){
            Write-Verbose -Message "Session was not exist. trying to create new sesion ..."
            if($PSCmdlet.ShouldProcess( "Verbose Description !", "New Session " + "Caption")){
                $session = New-SSHSession -ComputerName $iDRACName -Credential $Credential -AcceptKey
            }
        }
        else{
            Write-Verbose -Message "Previous active session is fined !"
        }
    }

    End{
        return ($session)
    }
}