New filters on the Home Feed, take a look!
view details

This post has been de-listed

It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.

5
Wrapper for New-CimSession
Post Body

Hi,

I'm currently writing a bunch of tools that require CimSessions. I noticed that I'm creating a new CimSession in each tool, doing my thing, and removing the CimSession. The controller script runs each of those tools, resulting in slow performance due to the many CimSessions being established and removed again. So I thought it's time to separate the creation of the CimSession to it's own function, and passing the session object to my tools. Since I have a bunch of old servers in the environment, there are a few that only allow Dcom. Previously, my tools always tried to use Wsman first, and if that didn't work they tried to use Dcom. So I also built that functionality into the new tool.

Any comments on the code are welcome.

A question regarding parallelism: would it be a good idea to add parallelism to this function, or would one do that in the controller script?

Function New-MXCimSession {

    [CmdletBinding()]

    Param(

        [Parameter(ValueFromPipeline=$True,
                   Mandatory=$True)]
        [string[]]$ComputerName,

        [ValidateSet('Wsman','Dcom')]
        [string]$Protocol = "Wsman",

        [System.Management.Automation.PSCredential]$Credential,

        [bool]$Fallback = $True

    )

    BEGIN {}

    PROCESS {

        foreach ($computer in $ComputerName) {

            if ($Protocol -eq 'Wsman') {

                $option = New-CimSessionOption -Protocol Wsman
            } 
            else {

                $option = New-CimSessionOption -Protocol Dcom
            }

            Write-Verbose "[$computer] Using protocol $Protocol to establish CimSession"

            if($PsBoundParameters.containskey('Credential')) {
                $params = @{
                    'ComputerName'  = $computer
                    'SessionOption' = $option
                    'Credential'    = $Credential
                    'ErrorAction'   = 'Stop'
                }

                Write-Verbose "[$computer] Using credentials of $($Credential.UserName) to establish CimSession"
            }
            else {
                $params = @{
                    'ComputerName'  = $computer
                    'SessionOption' = $option
                    'ErrorAction'   = 'Stop'
                }  

                Write-Verbose "[$computer] Using credentials of the current user to establish CimSession"       
            }

            Try {

                New-CimSession @params

                Write-Verbose "[$computer] Established CimSession using protocol $Protocol"

            }
            Catch {

                Write-Verbose ("[{0}] Failed to establish CimSession using protocol {1}: {2}" -f $computer, $Protocol, $_)

                if ($Fallback) {

                    if ($Protocol -eq 'Wsman') {

                        $Protocol = 'Dcom'

                    }
                    else {

                        $Protocol = 'Wsman'

                    }

                    if($PsBoundParameters.containskey('Credential')) {

                        $params = @{
                            'ComputerName'     = $computer
                            'Protocol'         = $Protocol
                            'Credential'       = $Credential
                            'Fallback' = $false
                        }

                    }
                    else {

                        $params = @{
                            'ComputerName'     = $computer
                            'Protocol'         = $Protocol
                            'Fallback' = $false
                        }

                    }


                    Write-Verbose "[$computer] Trying to establish CimSession with fall back protocol $Protocol"

                    New-MXCimSession @params

                } # if fallback

            } # catch

        } # foreach computer

    } # process

    END {}
}

Author
Account Strength
90%
Account Age
15 years
Verified Email
Yes
Verified Flair
No
Total Karma
4,540
Link Karma
3,347
Comment Karma
1,193
Profile updated: 4 days ago
Posts updated: 6 months ago

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
7 years ago