Public/New-RemotePSSession.TempPoint.ps1

function New-RemotePSSession
{
<#
    .SYNOPSIS
        Create new PowerShell session
     
    .DESCRIPTION
        This function will establish a new PowerShell WinRM session to the specified computer. Note: PSRemoting and WSMan
        configurations must be enabled prior to using this function.
     
    .PARAMETER ComputerName
        Provide the FQDN of the computer you wish to create a remoting session with
     
    .PARAMETER Credential
        PowerShell credential object
     
    .PARAMETER EnableNetworkAccess
        Add authentication to proxy credential
     
    .PARAMETER RequiresProxy
        Require proxy server credentials
     
    .PARAMETER ComputerName
        Computer fully qualified domain name
     
    .EXAMPLE
        $s = New-RemoteSession -ComputerName myserver.mydomain.com -Credential $myCreds -PassThru
     
    .OUTPUTS
        System.Management.Automation.Runspaces.PSSession
     
    .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([System.Management.Automation.Runspaces.PSSession])]
    param
    (
        [Parameter(Mandatory = $true,
                 HelpMessage = 'Provide the FQDN of the computer you wish to create a remoting session with')]
        [ValidateScript({
                if ((Test-NetConnection -ComputerName $_ -CommonTCPPort WINRM -InformationLevel Quiet).TcpTestSucceeded -eq $true)
                {
                    $true
                }
                else
                {
                    throw "Cannot connect to $_ on the default WinRM port from this computer."
                }
            })]
        [ValidateNotNullOrEmpty()]
        [string]$ComputerName,
        [Parameter(Mandatory = $false,
                 ValueFromPipeline = $false,
                 HelpMessage = 'Enter username. You will be prompted for Password')]
        [ValidateNotNull()]
        [System.Management.Automation.PSCredential]$Credential = [System.Management.Automation.PSCredential]::Empty,
        [Parameter(Mandatory = $false,
                 ValueFromPipeline = $false,
                 HelpMessage = 'Session requires proxy access is true.')]
        [Switch]$EnableNetworkAccess,
        [Switch]$RequiresProxy
    )
    
    #endParameterBlock
    
    begin
    {
        $Dot = $index.IndexOf('.')
        $Object = [pscustomobject]@{
            Hostname = $ComputerName.Substring(0, $Dot)
            FQDN     = $ComputerName
            Domain   = $ComputerName.Substring($Dot + 1)
        }
        
        $params = @{
            ComputerName = $ComputerName
            Name            = $Object.HostName
            ErrorAction  = 'Stop'
        }
        
        if ($PSBoundParameters.ContainsKey('Credential'))
        {
            $params.Add('Credential', $Credential)
        }
        
        if ($PSBoundParameters.ContainsKey('RequiresProxy'))
        {
            $option = New-PSSessionOption -ProxyAccessType NoProxyServer
            $params.Add('SessionOption', $Option)
        }
        
        if ($PSBoundParameters.ContainsKey('EnableNetworkAccess'))
        {
            $params.Add('EnableNetworkAccess', $true)
        }
    }
    process
    {
        if ($PSCmdlet.ShouldProcess($ComputerName, "Creating new PS Session to $ComputerName"))
        {
            
            try
            {
                
                $s = New-PSSession @params
            }
            catch
            {
                switch -Wildcard ($_.Exception.Message)
                {
                    "*2150858770*"                       { $ErrorMessage = 'Offline' }
                    "*server name cannot be resolved*"   { $ErrorMessage = 'ServerName cannot be resolved' }
                    "*2150859046*"                       { $ErrorMessage = 'PS Connect Failed' }
                    "*2150859193*"                       { $ErrorMessage = 'Asset Not Found' }
                    "*Access is denied*"                 { $ErrorMessage = 'Access Denied' }
                    "*specified computer name is valid*" { $ErrorMessage = 'Server Offline' }
                    "*winrm quickconfig*"                { $ErrorMessage = 'PsRemoting Not Enabled' }
                    "*firewall exception*"               { $ErrorMessage = 'PsRemoting Not Enabled' }
                    Default                              { $ErrorMessage = 'PS connect Error' }
                }
                $s = $ErrorMessage
            }
            
        }
        
    }
    end
    {
        return $s
    }
}#End function New-RemotePSSession