I want to have in a .ps1 file some code that creates a PSSession that can be used in other .ps1 scripts (in order to avoid code duplication).
At first I thought i need a function that creates a PSSession and returns it but I am confused about how they the function outputs work.
Here is my function:
function newRemoteSession
{
    param([string]$ipAddress)
    $accountName = 'admin'
    $accountPassword = 'admin'
    $accountPasswordSecure = ConvertTo-SecureString $accountPassword -AsPlainText -Force
    $accountCredential = New-Object System.Management.Automation.PSCredential ($accountName, $accountPasswordSecure)
    Try
    {
        $remoteSession = New-PSSession -ComputerName $ipAddress -UseSSL -Credential $accountCredential -SessionOption (New-PSSessionOption -SkipCACheck -SkipCNCheck) -ErrorAction Stop
    }
    Catch [System.Management.Automation.RuntimeException] #PSRemotingTransportException
    {
        Write-Host 'Could not connect with default credentials. Please enter credentials...'    
        $remoteSession = New-PSSession -ComputerName $ipAddress -UseSSL -Credential (Get-Credential) -SessionOption (New-PSSessionOption -SkipCACheck -SkipCNCheck) -ErrorAction Stop
        Break
    }
    return $remoteSession
}
However when I call $s = newRemoteSession(192.168.1.10), $s is empty.
When I run a script with
Write-Host '00'
$s = newRemoteSession('192.168.1.10')
$s
Write-Host '02'
function newRemoteSession
{
        ........
    Write-Host '01'
    $remoteSession
}
I get only '00' in the console, but I know the function runs because I get the credential prompt.
EDIT:
Ok, now it works:
- The Break in the Catch was stopping everything.
 - The function call must be without parenthesis.
 - The second code was wrong because the function must be defined before the call.