Public/Get-MyNewCimSession.ps1

function Get-MyNewCimSession
{
<#
    .SYNOPSIS
        Create CIM session to computer
     
    .DESCRIPTION
        This functions creates a new Microsoft Common Information Model remote session to the
        specified computer. It will default to using WSMan. If that fails an attempt to establish
        a session using DCOM will be tried.
     
    .PARAMETER ServerName
        FQDN of remote server
     
    .PARAMETER Credential
        PSCredential object
     
    .EXAMPLE
        PS C:\> .\Get-MyNewCimSession.ps1 -ServerName user.example.com -Credential (Get-Credential)
     
    .NOTES
        THIS CODE IS MADE AVAILABLE AS IS, WITHOUT WARRANTY OF ANY KIND.
        THE ENTIRE RISK OF THE USE OR THE RESULTS FROM THE USE OF THIS
        CODE REMAINS WITH THE USER.
#>

    [CmdletBinding()]
    [OutputType([Microsoft.Management.Infrastructure.CimSession])]
    param
    (
        [Parameter(Mandatory = $true)]
        [string]$ServerName,
        [Parameter(Mandatory = $true)]
        [pscredential]$Credential
    )

    begin
    {
        $VerbosePreference = 'Continue'
        $WarningPreference = 'Continue'
        
        $so = New-CimSessionOption -Protocol Dcom
        
        $Params = @{
            Authentication = 'Negotiate'
            ErrorAction    = 'Continue'
            ErrorVariable  = 'CIMSessionError'
        }
        
        if ($PSBoundParameters.ContainsKey('Credential'))
        {
            $Params.Add('Credential', $Credential)
        }
    }
    process
    {
        foreach ($server in $ServerName)
        {
            $Params.Add('ComputerName', $server)
            if ((Test-WSMan -ComputerName $Server -ErrorAction SilentlyContinue).productversion -match 'Stack: ([3-9]|[1-9][0-9]+)\.[0-9]+')
            {
                try
                {
                    Write-Verbose -Message ("Attempting connection to {0} using the default protocol." -f $ServerName) -Verbose
                    New-CimSession @Params
                    if ($CIMSessionError.Count)
                    {
                        Write-Warning -Message "Unable to connect to {0}" -f $CIMSessionError.OriginInfo.PSComputerName
                    }
                }
                catch
                {
                    $errorMessage = "{0}: {1}" -f $Error[0], $Error[0].InvocationInfo.PositionMessage
                    Write-Error -Message $errorMessage -ErrorAction Continue
                }
            }
            else
            {
                $Params.Add('SessionOption', $so)
                
                try
                {
                    Write-Verbose -Message ("Attempting connection to {0} using DCOM." -f $Server) -Verbose
                    New-CimSession @Params
                }
                catch
                {
                    $errorMessage = "{0}: {1}" -f $Error[0], $Error[0].InvocationInfo.PositionMessage
                    Write-Error -Message $errorMessage -ErrorAction Continue
                }
                $Params.Remove('SessionOption')
            }
            
            $Params.Remove('ComputerName')
        }
        
    }
    end
    {
        
    }
}