You can't tell if a session is still valid strictly from the client
since session management is owner by the server. Sessions may time out
or be killed by administrators. So the only way to tell is to initiate
a request to the server. We might want to add this as something we
explicitly support but given what you currently have available, you
could try something like:
function test-viserver {
$needSession = $false
$session = $null
trap {
Set-Variable needSession $true -Scope 1
continue
}
Get-OSCustomizationSpec -name __foo__
if ($needSession -eq $true) {
$session = Get-VIServer -Server (Read-Host "Enter server name")
if ($session -ne $null) {
$needSession = $false
}
}
return !$needSession
}
And use it like this:
PS C:\> . test-viserver
Enter server name: junk
False
PS C:\> . test-viserver
Enter server name: server
True
PS C:\> . test-viserver
True