Set-OMEConnection.ps1

Function Set-OMEConnection
{

    <#
    .SYNOPSIS
        Set Session parameters for the connection to OME server.
    .DESCRIPTION
        Set Session parameters for the connection to OME server.
        Returns Name of the session and shows warning message about user role and server version.
    .PARAMETER Server
        Specifies OME server to be connected.
    .PARAMETER port
        Specifies port of OME server.
    .PARAMETER IgnoreSSLErrors
        Option for ignoring SSL errors (very helpful).
    .PARAMETER Credentials
        Specified username and password for the OME connection.
    .PARAMETER Name
        Specifies Name of the Session.
 
    .EXAMPLE
        #Open default session with name "OMEConnection.DefaultOMESession" and current user credentials
        Set-OMEConnection -Server OMEserver.example.com
         
        WARNING: Version: 2.2.0(2056)
        WARNING: User: John.Doe Role: OMEAdministrator
        OMEConnection.DefaultOMESession
         
    .EXAMPLE
        $OME=Set-OMEConnection -Name "OME" -Server OMEserver.example.com -IgnoreSSLErrors -Credentials (Get-Credential)
 
        cmdlet Get-Credential at command pipeline position 1
        Supply values for the following parameters:
        Credential
        WARNING: Version: 2.2.0(2056)
        WARNING: User: John.Doe Role: OMEAdministrator
 
    .NOTES
        Author: Mike Khar
    .LINK
        http://www.dell.com/support/home/us/en/04/product-support/product/dell-openmanage-essentials-v2.2/research
        https://$Server:$Port/api/OME.svc/
    #>
  

    [CmdletBinding(
    )]
    Param(
        [parameter(Mandatory=$true)]
        [String]$Server,
        [String]$port=2607,
        [switch]$IgnoreSSLErrors=$false,
        [System.Management.Automation.PSCredential]$Credentials,
        [String]$Name="OMEConnection.DefaultOMESession"
    )
    
    Begin
    {
    }
    
    Process
    {
        $Value=New-Object -TypeName PSObject
        $Value | Add-Member  -MemberType NoteProperty -Name Server -Value $Server
        $Value | Add-Member  -MemberType NoteProperty -Name port -Value $port
        $Value | Add-Member  -MemberType NoteProperty -Name IgnoreSSLErrors -Value $IgnoreSSLErrors
        $Value | Add-Member  -MemberType NoteProperty -Name Credentials -Value $Credentials
        $Value.PsObject.TypeNames.Insert(0,"OME.Connection")
        If ($Name -ne "OMEConnection.DefaultOMESession") {$Name="OMEConnection.$Name"}
        if((Get-Variable -Scope Global -Name $Name -ErrorAction SilentlyContinue) -eq $null)
        {
            Write-Verbose "Create global variable: $Name"
            New-Variable -Scope Global -Name $Name -Value $Value -Description "OME.Connection"
        }
        else
        {
            Write-Verbose "Set global variable: $Name"
            Set-Variable -Scope Global -Name $Name -Value $Value -Description "OME.Connection"
        }
        If ($($Value).IgnoreSSLErrors -eq $true) {
            Ignore-SSLErrors
        }
        $user=Get-OMECurrentUser -Session $Name
        $SrvInfo=Get-OMEApplicationInfo -Session $Name
        If ($SrvInfo) {
            #write-warning $SrvInfo
            Write-Warning "Version: $($SrvInfo.Version)($($SrvInfo.BuildNumber))"
            Write-Warning "User: $($user.UserName) Role: $($user.UserType)"
            return $Name
        }
        else {
            Out-Exception -Exception $_
        }
    }
    
    End{}
}