Home Dashboard Directory Help
Search

For *-PSSession cmdlets, add -InitializationScript ScriptBlock parameter by Josh Gavant [MSFT]


Status: 

Closed
 as Postponed Help for as Postponed


24
0
Sign in
to vote
Type: Suggestion
ID: 693031
Opened: 10/4/2011 8:59:49 AM
Access Restriction: Public
1
Workaround(s)
view

Description

When opening a new PSSession, whether via New-PSSession or Enter-PSSesion, or via implicit remoting with modules created by Import/Export-PSSession, it's often necessary to run an initialization script to add snapins or modules on the remote side. Start-Job has an -InitializationScript parameter for just such a scenario.

An -InitializationScript parameter should be added to all the *-PSSession cmdlets to allow adding initial functions, snapins, and modules to new sessions. It should also be added into Import/Export-PSSession as a custom ScriptBlock automatically run when a new internal PSSession is dynamically created by the exported module.
Details
Sign in to post a comment.
Sign in to post a workaround.
Posted by Josh Gavant [MSFT] on 10/4/2011 at 9:05 AM
Modify exported module and add initialization script within Get-PSImplicitRemotingSession function.
For example, add the section between the "###"'s after the first "###"'s in the exported module. Then add the section ##Custom Addition within the Get-PSImplicitRemotingSession function as shown below.



##########################################

# Custom function to preconfigure session with additional snapin
# Called within Get-PSImplicitRemotingSession
function Configure-PSImplicitRemotingSession {
    if (($script:PSSession -ne $null) -and ($script:PSSession.Runspace.RunspaceStateInfo.State -eq "Opened")) {
        Invoke-Command -Session $script:PSSession -ScriptBlock {Add-PSSnapin Microsoft.SharePoint.PowerShell}
    }
}

##########################################

function Get-PSImplicitRemotingSession
{
    param(
        [Parameter(Mandatory = $true, Position = 0)]
        [string]
        $commandName
    )

    $savedImplicitRemotingHash = ''

    if (($script:PSSession -eq $null) -or ($script:PSSession.Runspace.RunspaceStateInfo.State -ne 'Opened'))
    {
        Set-PSImplicitRemotingSession `
            (& $script:GetPSSession `
                -InstanceId 90acafe6-de7b-432e-b190-37f0b729a947 `
                -ErrorAction SilentlyContinue )
    }
    if (($script:PSSession -eq $null) -or ($script:PSSession.Runspace.RunspaceStateInfo.State -ne 'Opened'))
    {
        Write-PSImplicitRemotingMessage ('Creating a new session for implicit remoting of "{0}" command...' -f $commandName)

        Set-PSImplicitRemotingSession `
            -CreatedByModule $true `
            -PSSession (
            $(
                & $script:NewPSSession `
                    -ComputerName 'server07' `
                    -ApplicationName '/wsman'    -ConfigurationName 'Microsoft.PowerShell' `
                    -SessionOption (Get-PSImplicitRemotingSessionOption) `
                    -Credential ( $host.UI.PromptForCredential( 'Windows PowerShell Credential Request', 'Enter your credentials for http://server07/wsman.', 'domain\user', 'server07' ) ) `
                     `
                    -Authentication Credssp `
                     `
            )
            )
        ## Custom Addition
        Write-PSImplicitRemotingMessage ('Calling custom Configure-PSImplicitRemotingSession function')
        Configure-PSImplicitRemotingSession
        ## End Custom Addition

        if ($savedImplicitRemotingHash -ne '')
        {
            $newImplicitRemotingHash = [string]($script:PSSession.ApplicationPrivateData.ImplicitRemoting.Hash)
            if ($newImplicitRemotingHash -ne $savedImplicitRemotingHash)
            {
                & $script:WriteWarning -Message 'Commands available in the newly opened remote session are different than when the implicit remoting module was created. Consider recreating the module using Export-PSSession cmdlet.'
            }
        }
    }
    if (($script:PSSession -eq $null) -or ($script:PSSession.Runspace.RunspaceStateInfo.State -ne 'Opened'))
    {
        throw 'No session has been associated with this implicit remoting module'
    }
    return [Management.Automation.Runspaces.PSSession]$script:PSSession
}