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.
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
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()
Once the tab is open you can use the Invoke() method to run a command in it.
$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"
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)
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)
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
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
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.
Very nice. It’s good to see more involved examples of scripting the ISE!
Aaaah… the wonderful $psise and all the wonderful remoting capabilities of ISE!! I really want to like other editors… But for sys-admin’ing, ISE is my favorite!!
Has VS Code anything similar to $newtab.Invoke(“Enter-PSSession -computername chi-dc01”)?
what I wanted to ask was.. does VS Code have anything remotely like $newtab.Invoke(โEnter-PSSession -computername chi-dc01โ)?… see what I did there??? ๐
Not that I am aware of. VS Code is a just an editor. Last I checked it doesn’t have any console capabilities.
I have been advocating since PoSH v3 for admins and even devs to take advantage of the PoSH ISE this way.
As well as getting folks to wrap their head around using the built-in PSEDIT feature. for example
psedit $profile
or
psedit SomeOtherTextFilename
Use PoSH ISE simultaneously as your.
1 – Multi server management tool (tab for each server)
2 – Mulit-Server log viewing / filtering tool (tab for each log)
3 – As you multi-Server install, config and troubleshooting tool.
add-in remote snapins fro say Exchange, Lync, etc…
4 – Really understand and add a common set of .Net Assemblies to you code template or at minimum add them to your snippit library. For dialog and forms work and more.
5 – Use it to pipe out all you session output to a file for later review and use.
6 – Install a common set of PoSH add-ons, ScriptAnalyzer/Browser, ProjectExplorer, ModuleBrowser, CIMExplorer, ISERegEx for example
7 – Multi-file note pad editor – instead of notepad (or other)
8 – for taking all your notes about PoSH and samples about PoSH in one place.
9 – Know that you can bounce out to the PoSH console host from directly in the ISE.
When I deliver a PoSH Session or a normal consulting engagement where I fire up PoSH, I always cover those last three before beginning any other topic.
Long live the PoSH ISE 8^)
But…
For that VSCode thing.
You can setup a task to execute your file via powershell.exe. It will return the results in the VSCode editor in a console-like Window to the right of you code window.
You have to edit the task.json file located in the .vscode folder to make this happen though.
Basically comment out other active example tasks in the default file and add an entry like this to the file, then open a .ps1 or start a new one, then run the task.:
// A task runner that call PowerShell
// runs the current working file
{
“version”: “0.1.0”,
“command”: “PowerShell.exe”,
“isShellCommand”: true,
“args”: [
“${file}”
],
“tasks”: [
{
“taskName”: “Build”,
“isBuildCommand”: true,
“showOutput”: “always”
},
{
“taskName”: “Test”,
“isTestCommand”: true,
“showOutput”: “always”
},
{
“taskName”: “Build, Test”,
“showOutput”: “always”
}
]
}
Know that this task.json is unique per folder where your code lives.
That about as far as you can get for now.
Also note the latest version of VSCode now allow for extension, and one of those is a PoSH extension, though it is only for coding and the intelli-sense with that. Though it is kind of flaky still and no where near as good as what the ISE provides. Yet it does provide intelli-sense hints about aspects of your code that the ISE does not.
Personally, for IT Pros, I’m not sure why I would need to use VS Code when I have the PowerShell ISE.
Multiple cursors is probably my favorite VS Code / Sublime Text / Visual Studio voodoo.
But the integration with projects and whatnot is pretty stellar too. Personally, in practice I actually do most of my work and development from within ISE. If I could get multiple cursors in ISE I might not ever look too much at the others again, at least for Powershell.
Can you share any resources that talk about how ISE Powershell tabs behave/interact? What they share vs what they don’t? For example, multiple tabs will share imported modules, but don’t (seem to) share (even global) variables.
However, I’m using a cmdlet (connect type) that appears to set a global variable that when executed in another Powershell tab, seems to modify something in the first tab context that “breaks” the connection in the first tab.
Just trying to find some good resources on ISE that explains any of this and you seem to be one of the few resources I’ve found that seems to have gone deep into ISE tabs. Thank you.
Sadly there does not appear to be a lot of official documentation on the ISE beyond what you can find in the app.