Creating a profile for a remote session


 

This has been updated, check out the newer post!

 

I’ve been trying to find a way to create a profile for when I remote in to servers with winrm, and even though its documented, it doesn’t seem to be possible.

http://technet.microsoft.com/en-us/library/dd315384.aspx

"– When you use Enter-PSSession, your user profile on the remote computer is used for the interactive session. The commands in the remote user profile, including commands to add Windows PowerShell snap-ins and to change the command prompt, run before the remote prompt is displayed."

 

So, apparently they forgot to add a few bits of code… I’ve posted a bug report on this (you should go vote on this) but for the time being, I’ve come up with a fairly simple way around this.

In your profile on your local machine we’re going to create a function called connect which will do this for us. In this example I’ll only modify the prompt, but obviously you can add more in.

function connect ($hname){
    $session = new-pssession $hname
    icm -session $session -scriptblock{
    #remote profile script

        function prompt
        {
            Write-Host $(Get-Date -Format [HH:mm:ss])  -NoNewline -ForegroundColor Blue
            write-host $(get-location) -nonewline -foregroundcolor green
            return ">"
        }

    }
    enter-pssession $session
}

 

This creates the session via New-PSSession and then uses the invoke command to push your profile (that script block) to it and then enters the session for interactive use.

There is one problem with this and that is when you do Enter-PSSession from the prompt, and then Exit-PSSession from the remote session, it closes out the session, when you do it this way, the Exit-PSSession keeps the session open.

To visualize this try this code bit

gsn
etsn $server
Exit
gsn
nsn $server | etsn
exit
gsn

You’ll notice the first gsn (Get-PSSession) returns nothing because the session was closed, the second gsn shows that the session is still open..

I’m going to work on a better Connect function, but for the time being, this will work, just don’t forget the connections are staying open!

Advertisements

About jrich

I am the Solutions Architect for Apex Learning in Seattle WA. I've been working with computers since I was 13. Started programming when I was 14. Had my first IT job as tech support at an ISP at the age of 15 and became a network admin at the age of 17. Since then I've worked at a variety of small to mid size companies supporting, maintaining and developing all aspects of IT. Mostly working with Windows based networks but have recently been working with Solaris system as well. I created this blog mostly as a place for me to take my own notes, but also share things that I had a hard time finding the info for.

Posted on July 8, 2010, in WMF (Powershell/WinRM) and tagged , . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: