PowerShell ISE Remote Possibilities

Normally, I think of the PowerShell ISE as a script development tool. I typically don’t think of it as a daily management tool, primarily because everything shares the same scope and if you are running scripts and aren’t careful can sometimes lead to unexpected results. But the ISE feature I really like is the ability to open a new tab with a remote connection to another server. You can even specify alternate credentials.

Normally you can accomplish this by going to File – New Remote PowerShell tab or using the keyboard shortcut Ctrl+Shift+R. But if you want to open multiple remote tabs, say to key servers you want to manage, this gets a little tedious. Or perhaps you’d like to open multiple remote tabs in your PowerShell ISE profile script? We need a better way.

Fortunately, the PowerShell ISE has a scriptable object model. A remote tab is simply another PowerShell tab that has executed Enter-PSSession. Creating a new tab is pretty simple.

Once the tab is open you can use the Invoke() method to run a command in it.

If you need alternate credentials adjust the command. You can even change the tab’s display name.

The new remote tabThe new remote tab (Image Credit: Jeff Hicks)

But there are a few things to watch out for. First, it takes time to open a new tab and you have to wait for it to be ready to invoke commands. You might need to use a short Do loop.

And every time you open a new PowerShell tab, you are opening a new PowerShell session which means your profile scripts run as well. If you are like me, this adds a little overhead that is completely unnecessary since the remote tab won’t be using anything in my profile. So I need a way to open the tab without running any profiles. Fortunately, this can be done, but it is not something exposed in the ISE. But thanks to fellow PowerShell MVP Tobias Weltner, I now have some code that digs into the ISE internals and turns off the option to load profiles.

To re-enable, I can run the last line and set the value to $False.

And of course, I’ve tried to make this easier for you to use by creating a function for the PowerShell ISE called New-ISERemoteTab. You can find the source code on GitHub.

In addition to creating a new remote tab, my function also runs a few commands after the remote connection is established. I prefer a clean slate so I like to at least run Clear-Host. You can invoke commands in the remote tab from new tab object you created.

The other concession I had to make is that if you specify a credential, I have to temporarily export it to disk. Because the new tab is a new PowerShell session there’s no way that I can find to pass variables between session without writing them to disk. But at the end of the process the file is deleted.

Creating a remote tab with credentialCreating a remote tab with credential (Image Credit: Jeff Hicks)

And this is the result:

The new remote tabThe new remote tab (Image Credit: Jeff Hicks)

Feel free to modify the new tab commands. If you specify multiple computers and a credential, the same credential will be used for all connections.

But I also have an option to prompt for credentials. This will not write anything to disk and if you specify multiple computers, you can enter a different credential for different computers. I thought that could come in handy if you need to connect to workgroup-based servers.

The end result is that I can now easily create multiple remote tabs with a single command.

Creating multiple remote tabsCreating multiple remote tabs (Image Credit: Jeff Hicks)

There are a few other examples in command help.

I’ve tried to annotate the function so you can understand how it works but feel free to post questions or problem in GitHub. I’d also like to hear from you in the comments if you find this useful.

Enjoy!

UPDATE: I’m starting to see the benefit of GitHub. Since I published the original earlier today, someone has already improved it and I’ve incorporated those changes into the project.

A Spooky PowerShell Halloween

I shared some code yesterday on Twitter and Facebook, but you may have missed it and I wanted to have a more permanent record so this is it. In the spirit of the holiday I thought it would spooky to have a little fun with the PowerShell ISE.

A spooky Halloween theme for the ISEA spooky Halloween theme for the ISE (Image Credit: Jeff Hicks)

How did I get this? Well first, I recommend you save your existing settings. Run this command in the PowerShell ISE.

This will save current values for settings you are about to change to a CSV file. Then run these commands to change ISE options.

Scary easy, right? These settings will remain even if you restart the PowerShell ISE. To restore your original settings and sanity you can import the saved CSV:

Or go to Tools – Options – Manage Themes and select the default, or whatever you are using.

Restoring the ISE themeRestoring the ISE theme (Image Credit: Jeff Hicks)

Happy Trick or Treating!

PowerShell Summit Europe 2015 Presentations

Last week I gave two presentations at the European edition of the PowerShell Summit held in beautiful Stockholm, Sweden. If you weren’t able to attend, you can still enjoy the sessions because everything was recorded. Session recordings for the entire conference can be found on PowerShell.org’s YouTube channel.

I thought I would post something here about my sessions including my demos and slides. One of my talks was on customizing the PowerShell ISE. I spend a lot of time in the ISE and have developed a lot of tricks and shortcuts to save myself time and frustration. My presentation explained how to create your own shortcuts and demonstrated a lot of my tips and tricks.

You can download a zip file of my material here.

My other presentation was about techniques and tips around logging in PowerShell. We often want to include a logging mechanism into our scripts and modules and there are a number of techniques you can use. Additionally, there may be a need for logging an entire PowerShell session. I talked about ways to accomplish that including a peak at some new PowerShell v5 features.

Demo files and slides can be downloaded here.

Hopefully the videos and material will be of some use to you, although I have to tell you that the real benefit of attending the Summit is the interaction with other attendees and speakers. It is the exchange of information between sessions that is extremely valuable and impossible to capture unless you attend.

Seats for the next North American event in Bellevue, WA will be limited so keep an eye on the calendar and act fast when tickets go on sale.

What Were You Working On?

toolboxIt probably comes as no surprise that I write a lot of PowerShell code. Like you, I’m usually working on several projects at the same time, most often using the PowerShell ISE. When I fire up the PowerShell ISE I often go to most recently edited files and re-open the files I was last working on. But sometimes my list of current projects gets pushed aside by other files I might open and edit. Because I like to be lazy (I mean efficient) I decided to come up with a solution to make it easy to open files I’m actively developing.

In the PowerShell ISE you can easily load a file into the editor with the PSEDIT command.

So all I need is a way to store my active files. It is simple enough to use a text file. All the text file needs is a list of full file paths. Opening all the files can be as easy as this:

But how can I easily add a file to the list? I can use the PSISE object model to get the path for the currently active script file and add it to the file.

To remove a file from the list, it is probably easiest to simply open the current work list using PSEDIT and manually delete what I no longer need. I took these core commands and wrapped them in a set of functions. And of course, I don’t want to have to type any more than I have to so it would be handy to add some shortcuts to the ISE Add-On menu.

I ended up creating a set of functions for these key operations and incorporated them into my ISE Scripting Geek module. The module exports a global variable for my work list.

Now when I open the ISE I can press Ctrl+Alt+I and have immediate access to everything I’m currently working on. This has already saved me a lot of time and frustration.

I have moved the ISE Scripting Geek module to GitHub so check it out here. Feel free to pick and choose what you want from the module or take my commands here and create your own solution for loading your active work files. The functions for this particular feature are in CurrentProjects.ps1.

Enjoy and let me know if this starts saving you some time.

Friday Fun: A Better PSEdit

In the PowerShell ISE, there is a built-in function called PSEdit. You can use this function to easily load a file in to the ISE directly from the ISE command prompt.

You can also load multiple files, but not as easily as you might like. I find myself wanting to do this:

As you can see isn’t what I’m expecting. I can get PSEdit to open multiple files, but I need to use a command like this:

I finally tired of this so I looked at the code for the PSEdit function.

I am assuming based on what I see that this was written a long time ago. So I decided to update it. Here’s my version:

The major difference is that my version works in the pipeline making it easier, for me at least, to open multiple files at once.

I also added some verbose messages for troubleshooting. This is my common practice when creating new PowerShell tools. You’ll also notice that I replaced the aliases in the original function with complete cmdlet and parameter names.

The last “feature” is my customized ValidateScript attribute. I wanted to verify that any path pointed to a legitimate file. I could have simply used this:

But if the path failed the test, PowerShell displays a long error message that isn’t always helpful. So I added some logic. Validation tests have to return either True or False. When it is false, PowerShell throws the exception. So I wrote my own exception message.

I get a similar error with the original psedit.

So perhaps I haven’t improved on it that much. But I could have written an even longer message and I wanted to demonstrate this technique in case you wanted to use it.

One last word on my version of PSEdit. I didn’t use a standard name, I guess because the original function doesn’t use one. And I’m ok with that. This is one of the situations where the function is a “cheater” command with a simple, alias-like, name. If you want to replace the original PSEdit function, add mine to your ISE profile script and rename it to PSEdit.

Enjoy.