How to use exit-pssession with invoke-command

Hi all
how to apply exit-pssession when using invoke-command? I wrote a script with the launch of a function on a remote PC that creates a base in the program, the function does not work if you use only invoke-command, since there is a process launch there. using new-pssession everything works, but I don’t understand where to specify exit-pssession. below is the script.

$Cred = Get-Credential -UserName domain\user -Message 'Enter Password'
$NameBase = Read-Host "Enter name base"
$session = New-PSSession -ComputerName 'MyPC.domain.local' -Credential $Cred
function CreateDB ($param1) {
$Path1C = "C:\Program Files\1cv8\common\1cestart.exe"
$CreateDB = "CREATEINFOBASE Srvr=""localhost"";`
Ref=""$param1"";`
DBMS='MSSQLServer';`
DBSrvr='serverDB';`
DB=""$param1"";`
DBUID='userDB';`
DBPwd='passuserDB';`
CrSQLDB=""Y"";`
SchJobDn=""N"";`
 /AddInList $param1`
 /Out C:\Temp\create.log"
Start-Process $Path1C $CreateDB
}
Invoke-Command -Session $session -ScriptBlock $function:CreateDB -ArgumentList $NameBase

or in this case it is not necessary to use exit-pssession? session will close automatically?

With …

… you end an interactivce session with a remote computer you initiated with

To remove a PSSession you created with …

… you can use …

:wink: :+1:t3: