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.

$newtab = $psise.powershelltabs.Add() 1 $newtab = $psise . powershelltabs . Add ( )

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

$newtab.Invoke("Enter-PSSession -computername chi-dc01") 1 $newtab . Invoke ( "Enter-PSSession -computername chi-dc01" )

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

$newtab.DisplayName = "CHI-DC01" 1 $newtab . DisplayName = "CHI-DC01"

The 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.

Do { Start-Sleep -Milliseconds 10 } until ($newTab.CanInvoke) 1 2 3 Do { Start-Sleep -Milliseconds 10 } until ( $newTab . CanInvoke )

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.

$type = ([Microsoft.Windows.PowerShell.Gui.Internal.MainWindow].Assembly.GetTypes()).Where({ $_.Name -eq 'PSGInternalHost' }) $currentField = $type.GetField('current', 'NonPublic,Static') $noprofileField = $type.GetField('noProfile', 'NonPublic,Instance') $pshost = $currentField.GetValue($type) $noprofileField.SetValue($pshost,$True) 1 2 3 4 5 $type = ( [ Microsoft . Windows . PowerShell . Gui . Internal . MainWindow ] . Assembly . GetTypes ( ) ) . Where ( { $_ . Name -eq 'PSGInternalHost' } ) $currentField = $type . GetField ( 'current' , 'NonPublic,Static' ) $noprofileField = $type . GetField ( 'noProfile' , 'NonPublic,Instance' ) $pshost = $currentField . GetValue ( $type ) $noprofileField . SetValue ( $pshost , $True )

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.

"Set-Location -path 'C:\'","Clear-Host","WhoAmI",'$PSVersionTable' | foreach { Write-Verbose "[$($newTab.Displayname)] Invoking $_" $newTab.Invoke($_) #wait for command to complete Do { Start-Sleep -Milliseconds 10 } until ($newTab.CanInvoke) } #foreach command 1 2 3 4 5 6 7 8 "Set-Location -path 'C:\'" , "Clear-Host" , "WhoAmI" , '$PSVersionTable' | foreach { Write-Verbose "[$($newTab.Displayname)] Invoking $_" $newTab . Invoke ( $_ ) #wait for command to complete Do { Start-Sleep -Milliseconds 10 } until ( $newTab . CanInvoke ) } #foreach command

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 credential (Image Credit: Jeff Hicks)

And this is the result:

The 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.

New-ISERemoteTab chi-fp02,chi-web02,chi-dc04 1 New-ISERemoteTab chi -fp02 , chi -web02 , chi -dc04

Creating 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.

Like this: Like Loading...