NIC 2017 Slides and Demos

Nic2017-logoEarlier this month I had the pleasure of presenting at the Nordic Infrastructure Conference (NIC). This is still a relatively young conference as these things go, but you couldn’t tell based on my experiences.  Given the demise of TechEd Europe, conferences like this are filling the void, and doing a fantastic job.

The conference draws top speakers who really pack them in.

February 03, 2017 at 0609AM

I gave 2 PowerShell related sessions which from my perspective were well attended.

February 02, 2017 at 0501AM

My presentations were on PowerShell Web Access and how to do more with remoting.  The latter was titled “Secrets of PowerShell Remoting”, but really only secret in the sense that you may not have seen or heard of a few techniques or concepts.

If you’ve attended any of my conference sessions in the past you know I tend to minimize slides and maximize demo time. So even though I’m including a PDF of my slide material it probably won’t have much meaning.

You can download a zip file with my session material.

Secrets of PowerShell Remoting

Managing from the Beach with PowerShell Web Access

The sessions were recorded and at some point should be available on the conference’s YouTube channel.

Remote PowerShell ISE Connections in Action

I’ve written a few times about my PowerShell module that makes it easier to create remote tabs in the PowerShell ISE. The module, ISERemoteTab, is available in the PowerShell gallery. I’ve also created a short video that demonstrates how to use it, especially the WPF form.

I have a few more ideas for this project when I get some free time. In the mean time this should keep you busy. Enjoy!

Formal Remote ISE Connections

I have updated my PowerShell tools that make it easier to open up a remote tab in the PowerShell ISE.  The standard approach is limited and doesn’t allow for things like using SSL or different ports.  I have had the ISERemoteTab project up on GitHub for awhile.

I recently added a WPF-based form to make it easier to make remote connections.

remoteIsetabYou can enter in a single computer or multiple computers.  Although, if you use multiple computers, the same settings will apply to all of them. But once I hit connect, I get new remote tabs.

remotetabsIn this example I am using the remote profile script feature to run a set of PowerShell commands after making the remote connection.

In my ISE profile script I added these lines to create a new Add-On shortcut.

All of this has also been published to the PowerShell Gallery. If you are running PowerShell 5 you can use Install-Module to install  ISERemoteTab.

I hope you’ll try it out and let me know what you think. If you run into bugs or have feature requests, please post them on the Github project site.

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.


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.

The PowerShell Night Shift

A few days ago I got a question on Twitter on how to push a long running command to the background but would also survive a user logoff. This is a pretty standard practice in the Linux world. In PowerShell we’ve had similar operations using the Start-Job cmdlet. With Start-Job you can run a scriptblock or script in a background PowerShell process and get the results later. However, if you close your PowerShell session, you lose the job so that’s not an option here.

The best solution in this case is to take advantage of disconnected sessions. This feature was added in PowerShell 3.0 where Microsoft moved the maintaining the remoting session from the client to the server. This meant that if you had a long running command you wanted to run on a remote server, you could kick it off with Invoke-Command.

The session gets created remotely and the command kicks off. On the client you can logoff, shut down, go home, or even go to another computer. You can reconnect the session as long as you use the same credentials. One thing you can’t do though is be in the middle of an interactive session and decide to disconnect.

It turns out this same idea works locally as well. Here’s how I can kick it off

Kicking off a disconnected commandKicking off a disconnected command (Image Credit: Jeff Hicks)

Remember this is on my machine. Essentially I’ve created a remote session to myself. I can now close the PowerShell session. I can even log off and go home. BUT, I can’t shut down the computer. If I do that, then I will lose the remoting session. For the sake of my demo, I’ll log out, re-login and open a new PowerShell session.

Once back in I need to get the disconnected session. You might think to simply run Get-PSSession.

Attempting to reconnect the sessionAttempting to reconnect the session (Image Credit: Jeff Hicks)

But this is only trying to show connected sessions. Because the disconnected session is technically remote, I need to specify the computername.

Viewing the disconnected sessionViewing the disconnected session (Image Credit: Jeff Hicks)

There it is. In order to use it I need to connect to it.

Re-connecting the sessionRe-connecting the session (Image Credit: Jeff Hicks)

See the availability state of Busy? Even though I am connected to the session I can’t get anything out of it until it stops being busy.

Session must be opened and availableSession must be opened and available (Image Credit: Jeff Hicks)

During the course of my testing, and I don’t know if this is something related to running this in basically a loopback, but the session never seems to stop being busy, even though if it is simply running my command it should be finished. But if I enter the session interactive, that seems to be enough to give it a bump and get things moving.

Re-entering the disconnected sessionRe-entering the disconnected session (Image Credit: Jeff Hicks)

But I’m good with this. I can kick off a long running PowerShell task, logout, and get the results the next time I log in. I can re-use the session for as long as it is open.
An open loopback remoting session (Image Credit: Jeff Hicks)

The session will exist until I delete it. Or I can disconnect it again.

disconnecting again

disconnecting again (Image Credit: Jeff Hicks)

If you find this little trick useful, I hope you’ll let me know. And if you want to learn more about PowerShell remoting, keep an eye on the Pluralsight course catalog as I am currently working on a PowerShell Remoting Fundamentals course.