Storing variable in a session

I have a function within a module that our local technicians use to query ALL of our terminal servers to find out which server a particular client is logged into. It will verify the account via Active Directory and then query the login name among an external list of terminal servers and return the one that is logged in.

 

What I would like to do is when a technician authenticates once, he/she does not have to authenticate again during the duration of the PowerShell session. For example, I would like it to work like the command ‘Get-ADComputer -filter*’. When run from a local workstation, it will prompt for credentials one time and then any further ‘Get-ADcomputer’ command, it will not ask for credentials again unless I start another session.

 

I am looking at Global Scopes as a solution, but I am not sure if I am going down the right path to the best solution.

 

function Get-TPTSsession { [CmdletBinding()] Param ( # SamAccount name of user to find in TS cluster [Parameter(Mandatory=$true)] [string] $User )

Begin
{
$Credential= Get-Credential XXXX
}
Process
{

try{

$asp=Get-Content S:\TechPro\TPtservers\TPTserverIP.txt

write-host “Validating $user” -ForegroundColor Yellow
Invoke-Command -ComputerName 10.221.21.3 -ScriptBlock {get-aduser $using:user} -Credential $credential -ErrorAction Stop | Out-Null

Write-host “Searching for $User” -ForegroundColor Yellow
$Command = {qwinsta | ForEach-Object { $.Trim() -replace “\s+”,“,”} | ConvertFrom-Csv | Where-Object {$.USERNAME -EQ $using:User}}

#Need to grab all ‘possible’ types of errors here and create corresponding Catch
$Results = Invoke-Command -ComputerName $asp -ScriptBlock $Command -Credential $Credential -ErrorAction Stop
If ($Results -eq $null){
Write-Host “$User is not logged into Remote enviornment” -ForegroundColor Yellow -ErrorAction Stop
}

foreach ($Result in $Results){
$hostname = Invoke-Command -ComputerName $Result.PSComputerName -ScriptBlock{$env:COMPUTERNAME} -Credential $Credential
$Result | Add-Member -MemberType NoteProperty -Name hostname -Value $hostname
Write-Output $Result
}

}#end Try

catch [System.Management.Automation.Remoting.PSRemotingTransportException]
{
Write-Host “There was an Authentication Problem!!! Username or password is incorrect” -ForegroundColor Red
}

catch [System.Management.Automation.RemoteException]
{
Write-Host “User not found in Active Directory. Check your spelling.” -ForegroundColor Red
}

catch
{
Write-Host “Something unanticipated went wrong” -ForegroundColor Red

} #end Catch

finally{

$Results = $null
}

} #end Process
End
{
}
}

okay, lets start by formatting your code. You can use gist.github.com and paste the link here.

For your requirement, you would use case. Its better to create a PSSession first then use the session variable across executions. PFB an example.

$Global:ExistingSession = Get-PSSession -ComputerName ServerName -ErrorAction SilentlyContinue
If(-not $Global:ExistingSession){
    $Global:ExistingSession = New-PSSession -ComputerName ServerName -Credential (Get-Credential)
}

Invoke-Command -Session $Global:ExistingSession -ScriptBlock {
#Whatever
}